# AUTOGENERATED! DO NOT EDIT! File to edit: notebooks/data.utils.ipynb (unless otherwise specified).

__all__ = ['log', 'generate_data', 'get_data_dict', 'get_chart_list', 'read_json_file', 'payload_to_dict',
           'get_chart_map', 'dicts_to_payload']

# Cell

import logging
import requests
import json

import numpy as np
import pandas as pd

log = logging.getLogger(__name__)

# Cell


def generate_data(n=100, spec={'chart.a':['dim.a1','dim.a2'],'chart.b':['dim.b1','dim.b2']}):
    """Generate some dummy data.
    """
    colnames = [f'{c}|{d}' for c in spec for d in spec[c]]
    arr_baseline = pd.DataFrame(np.random.rand(n,len(colnames)), columns=colnames).values
    arr_highlight = pd.DataFrame(np.random.rand(n,len(colnames)), columns=colnames).values
    return colnames, arr_baseline, arr_highlight

# Cell


def get_data_dict(after=-120,before=0,host='london.my-netdata.io',charts=['system.cpu','system.load']):
    data_dict = {}
    for chart in charts:
        data_dict[chart] = {}
        url = f'https://{host}/api/v1/data?chart={chart}&after={after}&before={before}&format=json'
        r = requests.get(url)
        r_json = r.json()
        data = np.array(r_json['data'])
        labels = r_json['labels']
        for i in range(1,len(labels)):
            data_dict[chart][labels[i]] = data[:,i].tolist()
    return data_dict



# Cell


def get_chart_list(host='london.my-netdata.io'):
    url = f'https://{host}/api/v1/charts'
    r = requests.get(url)
    r_json = r.json()
    charts = list(r_json['charts'].keys())
    return charts



# Cell


def read_json_file(file_path):
    with open(file_path, 'r') as f:
        data=f.read()
    return json.loads(data)



# Cell


def payload_to_dict(payload_data: list) -> dict:
    """Convert the payload data into a more simple dictionary expected by the netdata-insights models.
    """
    data = {}
    for chart_dict in payload_data:
        chart_name = list(chart_dict.keys())[0]
        data[chart_name] = {dim['id']: [x for x in dim['data']] for dim in chart_dict[chart_name]['dimensions']}
    return data



# Cell


def get_chart_map(dict_baseline, dict_highlight):
    baseline_map = {chart: [dim for dim in dict_baseline[chart]] for chart in dict_baseline}
    highlight_map = {chart: [dim for dim in dict_highlight[chart]] for chart in dict_highlight}
    charts = set(list(baseline_map.keys()) + list(highlight_map.keys()))
    chart_map = {}
    for chart in charts:
        if chart in baseline_map:
            baseline_dims = baseline_map[chart]
        else:
            baseline_dims = []
        if chart in highlight_map:
            highlight_dims = highlight_map[chart]
        else:
            highlight_dims = []
        chart_dims = list(set(baseline_dims + highlight_dims))
        chart_map[chart] = chart_dims
    return chart_map



# Cell


def dicts_to_payload(dict_baseline, dict_highlight):
    payload = {}
    baseline_data = [{chart: {"dimensions": [{"id": dim, "data": dict_baseline[chart][dim]} for dim in dict_baseline[chart]]}} for chart in dict_baseline]
    highlight_data = [{chart: {"dimensions": [{"id": dim, "data": dict_highlight[chart][dim]} for dim in dict_highlight[chart]]}} for chart in dict_highlight]
    payload['baseline_data'] = baseline_data
    payload['highlight_data'] = highlight_data
    return payload

