|
| 1 | + |
| 2 | +import requests |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +from netdata_pandas.data import get_data |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import streamlit as st |
| 8 | +from collections import OrderedDict |
| 9 | +from urllib.parse import urlparse |
| 10 | +import re |
| 11 | + |
| 12 | + |
| 13 | +def parse_netdata_url(url): |
| 14 | + if url.startswith('http'): |
| 15 | + url_parsed = urlparse(url) |
| 16 | + url_dict = { |
| 17 | + 'host': url_parsed.hostname, |
| 18 | + 'port': url_parsed.port, |
| 19 | + 'host:port': f'{url_parsed.hostname}:{url_parsed.port}' if url_parsed.port else url_parsed.hostname, |
| 20 | + 'fragments': {frag.split('=')[0]: frag.split('=')[1] for frag in url_parsed.fragment.split(';') if '=' in frag} |
| 21 | + } |
| 22 | + else: |
| 23 | + url_dict = { |
| 24 | + 'fragments': {frag.split('=')[0]: frag.split('=')[1] for frag in url.split(';') if |
| 25 | + '=' in frag} |
| 26 | + } |
| 27 | + |
| 28 | + if 'after' in url_dict['fragments']: |
| 29 | + url_dict['after_long'] = int(int(url_dict['fragments']['after'])) |
| 30 | + url_dict['after'] = int(int(url_dict['fragments']['after']) / 1000) |
| 31 | + if 'before' in url_dict['fragments']: |
| 32 | + url_dict['before_long'] = int(int(url_dict['fragments']['before'])) |
| 33 | + url_dict['before'] = int(int(url_dict['fragments']['before']) / 1000) |
| 34 | + |
| 35 | + if 'highlight_after' in url_dict['fragments']: |
| 36 | + url_dict['highlight_after_long'] = int(int(url_dict['fragments']['highlight_after'])) |
| 37 | + url_dict['highlight_after'] = int(int(url_dict['fragments']['highlight_after']) / 1000) |
| 38 | + if 'highlight_before' in url_dict['fragments']: |
| 39 | + url_dict['highlight_before_long'] = int(int(url_dict['fragments']['highlight_before'])) |
| 40 | + url_dict['highlight_before'] = int(int(url_dict['fragments']['highlight_before']) / 1000) |
| 41 | + |
| 42 | + child_host = re.search('/host/(.*?)/', url) |
| 43 | + child_host = child_host.group(1) if child_host else None |
| 44 | + print(child_host) |
| 45 | + if child_host: |
| 46 | + url_dict['child_host'] = child_host |
| 47 | + url_dict['host:port'] = url_dict['host:port'] + f'/host/{child_host}' |
| 48 | + |
| 49 | + return url_dict |
| 50 | + |
| 51 | + |
| 52 | +netdata_url = st.text_input('netdata_agent_dashboard_url', value='http://london.my-netdata.io/#after=-900000;before=0;=undefined;theme=slate;utc=Europe%2FLondon') |
| 53 | + |
| 54 | + |
| 55 | +url_dict = parse_netdata_url(netdata_url) |
| 56 | +host = url_dict['host:port'] |
| 57 | +after = int(url_dict['fragments'].get('after', '-900000'))//1000 |
| 58 | +before = int(url_dict['fragments'].get('before', '0'))//1000 |
| 59 | +highlight_after = int(url_dict['fragments'].get('highlight_after', '0'))//1000 |
| 60 | +highlight_before = int(url_dict['fragments'].get('highlight_before', '0'))//1000 |
| 61 | + |
| 62 | +after = highlight_after if highlight_after > 0 else after |
| 63 | +before = highlight_before if highlight_before > 0 else before |
| 64 | + |
| 65 | +print(highlight_after) |
| 66 | +print(highlight_before) |
| 67 | +print(after) |
| 68 | +print(before) |
| 69 | + |
| 70 | +url_weights = f"http://{host}/api/v1/weights?after={after}&before={before}&options=raw" |
| 71 | +print(url_weights) |
| 72 | + |
| 73 | +weights_data = requests.get(url_weights).json() |
| 74 | + |
| 75 | +data = OrderedDict() |
| 76 | +for context in weights_data['contexts']: |
| 77 | + context_key = f"{context}: {round(weights_data['contexts'][context]['weight'],2)}" |
| 78 | + data[context_key] = dict() |
| 79 | + for chart in weights_data['contexts'][context]['charts']: |
| 80 | + chart_key = f"{chart}: {round(weights_data['contexts'][context]['charts'][chart]['weight'],2)}" |
| 81 | + data[context_key][chart_key] = dict() |
| 82 | + for dim in weights_data['contexts'][context]['charts'][chart]['dimensions']: |
| 83 | + dim_key = dim |
| 84 | + data[context_key][chart_key][dim_key] = round(weights_data['contexts'][context]['charts'][chart]['dimensions'][dim], 2) |
| 85 | + |
| 86 | +st.json( |
| 87 | + data, |
| 88 | + expanded=False |
| 89 | +) |
0 commit comments