예제 #1
0
def finalize_panel(panel, next_id):
    if panel['type'] == 'graph':
        panel = update_dict(copy.deepcopy(default_graph), panel)
        panel['yaxes'][0] = update_dict(copy.deepcopy(default_yaxis),
                                        panel['yaxes'][0])
        panel['yaxes'][1] = update_dict(copy.deepcopy(default_yaxis),
                                        panel['yaxes'][1])
    elif panel['type'] in ['text', 'markdown']:
        panel = update_dict(copy.deepcopy(default_text), panel)
        if panel['type'] == 'text':
            panel['content'] = '```\n%s\n```' % (panel['content'])
        panel['type'] = 'text'

    panel = update_dict(copy.deepcopy(default_panel), panel)
    if 'id' not in panel:
        panel['id'] = next_id()
    if 'width' in panel:
        panel['span'] = panel['width']
        del panel['width']
    if panel['type'] == 'graph':
        if 'threshold' in panel:
            threshold = panel['threshold']
            update_dict(
                panel, {
                    "grid": {
                        "threshold1": int(threshold['value']),
                        "threshold1Color": threshold['color'],
                        "thresholdLine": True,
                    },
                })
            del panel['threshold']
    return panel
예제 #2
0
def finalize_row(row, next_id):
    row = update_dict(copy.deepcopy(default_row), row)

    row['panels'] = [finalize_panel(panel, next_id) for panel in row['panels']]

    row['collapse'] = row['collapsed']
    del row['collapsed']

    return row
예제 #3
0
def finalize_template(template):
    if not isinstance(template, dict):
        if template == 'host':
            template = {
                'label': 'Hosts',
                'name': 'host',
                'query': 'hosts.*',
                'type': 'query',
            }

    if not isinstance(template, dict):
        raise RuntimeError('Unknown template spec "%s"', template)

    type = template.get('type', 'custom')
    if type == 'custom':
        values = template.get('values', [])
        if values:
            if template.get('name') == 'host':
                values = [value.split('.', 2)[0] for value in values]
            values = ['All'] + values
            current = values[1]
        else:
            current = '<empty>'

        template_obj = {
            'current': to_template_value(current),
            'hideLabel': (template.get('label', None) is None),
            'label': template.get('label', 'Label'),
            'name': template.get('name'),
            'options': [to_template_value(value) for value in values],
            'query': ','.join(values),
            'refresh': 0,
        }
    elif type == 'query':
        template_obj = {
            'current': to_template_value('All'),
            'label': template.get('label', 'Label'),
            'name': template.get('name'),
            'options': [],
            'query': template.get('query'),
            'refresh': 1,
        }
    else:
        raise RuntimeError('Unknown template type "%s"', type)

    template_obj['type'] = type
    template_obj = update_dict(copy.deepcopy(default_template), template_obj)
    return template_obj
예제 #4
0
def new(title='Untitled graph panel', width=None):
    """Prepares a new graph panel object

    Parameters
    ----------
    title: string
      The title for the graph panel

    Return
    ------
    object: The graph panel object
    """

    panel = new_panel(title=title, width=width)
    panel = update_dict(panel, copy.deepcopy(default_graph))

    return panel
예제 #5
0
def finalize_dashboard(dashboard):
    next_id = id_service()

    dashboard = generic_finalize_dashboard(dashboard)
    dashboard = update_dict(copy.deepcopy(default_dashboard), dashboard)

    dashboard['rows'] = [
        finalize_row(row, next_id) for row in dashboard['rows']
    ]

    dashboard['originalTitle'] = dashboard['title']
    dashboard['uid'] = dashboard['basename']
    del dashboard['basename']

    if 'templates' in dashboard:
        dashboard['templating'] = {
            'list': [
                finalize_template(template)
                for template in dashboard['templates']
            ]
        }
        del dashboard['templates']
    return dashboard