1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| import json from bottle import route, run, template, Bottle, response import argparse import pandas as pd from plotly import utils import plotly.graph_objs as go from requests.compat import json as _json from influxdb import DataFrameClient app = Bottle() @app.hook('after_request') def enable_cors(): response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' @app.route('/data') def index(): client = DataFrameClient('127.0.0.1', '8086', database='dc') res = client.query('select sum(succ), sum(fail) from p group by host limit 10') data = [] for metric, df in res.items(): print df trace = go.Bar( x=df[df.columns[0]], y=df[df.columns[1]], name=str(metric), ) data.append(trace) layout = go.Layout() data_json = _json.dumps(data, cls=utils.PlotlyJSONEncoder) layout_json = _json.dumps(layout, cls=utils.PlotlyJSONEncoder) data_dict = json.loads(data_json) layout_dict = json.loads(layout_json) return {'records': data_dict, 'layout': layout_dict} @app.route('/pie') def pie(): client = DataFrameClient('127.0.0.1', '8086', database='dc') res = client.query('select sum(succ), sum(fail) from p group by host limit 10') data = [] labels = [] pds = [] for metric, df in res.items(): print metric, df print '---------', df.columns, df.columns.values labels.append(str(metric) + df.columns[0]) labels.append(str(metric) + df.columns[1]) pds.append(df[df.columns[0]]) pds.append(df[df.columns[1]]) trace = go.Pie( labels=labels, values=pd.concat(pds), name=str(metric), ) data.append(trace) layout = go.Layout() data_json = _json.dumps(data, cls=utils.PlotlyJSONEncoder) layout_json = _json.dumps(layout, cls=utils.PlotlyJSONEncoder) data_dict = json.loads(data_json) layout_dict = json.loads(layout_json) return {'records': data_dict, 'layout': layout_dict} @app.route('/line') def line(): client = DataFrameClient('127.0.0.1', '8086', database='dc') res = client.query('select sum(succ), sum(fail) from p where time > now() - 1d and succ > 0 group by time(1h) limit 10') data = [] for metric, df in res.items(): print metric, df print '---------', df.index for col in df.columns.values: trace = go.Scatter( x=df.index, y=df[col], name=str(metric), ) data.append(trace) layout = go.Layout() data_json = _json.dumps(data, cls=utils.PlotlyJSONEncoder) layout_json = _json.dumps(layout, cls=utils.PlotlyJSONEncoder) data_dict = json.loads(data_json) layout_dict = json.loads(layout_json) return {'records': data_dict, 'layout': layout_dict} run(app, host='localhost', port=8080, debug=True)
|