def charts(): if not g.id: abort(400, "no graph id given") j = TmpGraph.get(g.id) if not j: abort(400, "no such tmp_graph where id=%s" % g.id) counters = j.counters if not counters: abort(400, "no counters of %s" % g.id) counters = sorted(set(counters)) endpoints = j.endpoints if not endpoints: abort(400, "no endpoints of %s" % g.id) endpoints = sorted(set(endpoints)) chart_urls = [] chart_ids = [] p = { "id": "", "legend": g.legend, "cf": g.cf, "sum": g.sum, "graph_type": g.graph_type, "nav_header": g.nav_header, "start": g.start, "end": g.end, "comp_date": g.comp_date, } if g.graph_type == GRAPH_TYPE_KEY: for x in endpoints: id_ = TmpGraph.add([x], counters) if not id_: continue p["id"] = id_ chart_ids.append(int(id_)) src = "/chart/h?" + urllib.urlencode(p) chart_urls.append(src) elif g.graph_type == GRAPH_TYPE_HOST: for x in counters: id_ = TmpGraph.add(endpoints, [x]) if not id_: continue p["id"] = id_ chart_ids.append(int(id_)) src = "/chart/h?" + urllib.urlencode(p) chart_urls.append(src) else: id_ = TmpGraph.add(endpoints, counters) if id_: p["id"] = id_ chart_ids.append(int(id_)) src = "/chart/a?" + urllib.urlencode(p) chart_urls.append(src) return render_template("chart/multi_ng.html", **locals())
def charts(): if not g.id: abort(400, "no graph id given") j = TmpGraph.get(g.id) if not j: abort(400, "no such tmp_graph where id=%s" %g.id) counters = j.counters if not counters: abort(400, "no counters of %s" %g.id) counters = sorted(set(counters)) endpoints = j.endpoints if not endpoints: abort(400, "no endpoints of %s" %g.id) endpoints = sorted(set(endpoints)) chart_urls = [] chart_ids = [] p = { "id": "", "legend": g.legend, "cf": g.cf, "sum": g.sum, "graph_type": g.graph_type, "nav_header": g.nav_header, "start": g.start, "end": g.end, } if g.graph_type == GRAPH_TYPE_KEY: for x in endpoints: id_ = TmpGraph.add([x], counters) if not id_: continue p["id"] = id_ chart_ids.append(int(id_)) src = "/chart/h?" + urllib.urlencode(p) chart_urls.append(src) elif g.graph_type == GRAPH_TYPE_HOST: for x in counters: id_ = TmpGraph.add(endpoints, [x]) if not id_: continue p["id"] = id_ chart_ids.append(int(id_)) src = "/chart/h?" + urllib.urlencode(p) chart_urls.append(src) else: id_ = TmpGraph.add(endpoints, counters) if id_: p["id"] = id_ chart_ids.append(int(id_)) src = "/chart/a?" + urllib.urlencode(p) chart_urls.append(src) return render_template("chart/multi_ng.html", **locals())
def _generate_graph_urls(graph, counters, endpoint_list, start, end): ret_graphs = [] if graph.graph_type == 'h': for c in counters: tmp_graph_id = TmpGraph.add(endpoint_list, [c,]) if not tmp_graph_id: break new_g = copy.deepcopy(graph) new_g.counters = c if end: new_g.src = '''/chart/h?id=%s&start=%s&end=%s''' %(tmp_graph_id, start or (0-graph.timespan), end) else: new_g.src = '''/chart/h?id=%s&start=%s''' %(tmp_graph_id, start or (0-graph.timespan)) if graph.method == 'SUM': new_g.src += "&sum=on" else: new_g.src += "&cf=%s" %graph.method ret_graphs.append(new_g) elif graph.graph_type=='k': for e in endpoint_list: tmp_graph_id = TmpGraph.add([e,], counters) if not tmp_graph_id: break new_g = copy.deepcopy(graph) new_g.hosts = e if end: new_g.src = '''/chart/k?id=%s&start=%s&end=%s''' %(tmp_graph_id, start or (0-graph.timespan), end) else: new_g.src = '''/chart/k?id=%s&start=%s''' %(tmp_graph_id, start or (0-graph.timespan)) if graph.method == 'SUM': new_g.src += "&sum=on" else: new_g.src += "&cf=%s" %graph.method ret_graphs.append(new_g) else: #组合视角 tmp_graph_id = TmpGraph.add(endpoint_list, counters) if not tmp_graph_id: return [] new_g = copy.deepcopy(graph) if end: new_g.src = '''/chart/a?id=%s&start=%s&end=%s''' %(tmp_graph_id, start or (0-graph.timespan), end) else: new_g.src = '''/chart/a?id=%s&start=%s''' %(tmp_graph_id, start or (0-graph.timespan)) if graph.method == 'SUM': new_g.src += "&sum=on" else: new_g.src += "&cf=%s" %graph.method ret_graphs.append(new_g) return ret_graphs
def chart(): endpoints = request.form.getlist("endpoints[]") or [] counters = request.form.getlist("counters[]") or [] graph_type = request.form.get("graph_type") or GRAPH_TYPE_HOST id_ = TmpGraph.add(endpoints, counters) ret = { "ok": False, "id": id_, "params": { "graph_type": graph_type, }, } if id_: ret['ok'] = True return json.dumps(ret)
def multi_endpoints_chart_data(): if not g.id: abort(400, "no graph id given") j = TmpGraph.get(g.id) if not j: abort(400, "no such tmp_graph where id=%s" % g.id) counters = j.counters if not counters: abort(400, "no counters of %s" % g.id) counters = sorted(set(counters)) endpoints = j.endpoints if not endpoints: abort(400, "no endpoints of %s" % (g.id, )) endpoints = sorted(set(endpoints)) ret = {"units": "", "title": "", "series": []} c = counters[0] ret['title'] = c query_result = graph_history(endpoints, counters[:1], g.cf, g.start, g.end) series = [] for i in range(0, len(query_result)): x = query_result[i] try: xv = [(v["timestamp"] * 1000, v["value"]) for v in x["Values"]] serie = { "data": xv, "name": query_result[i]["endpoint"], "cf": g.cf, "endpoint": query_result[i]["endpoint"], "counter": query_result[i]["counter"], } series.append(serie) except: pass sum_serie = { "data": [], "name": "sum", "cf": g.cf, "endpoint": "sum", "counter": c, } if g.sum == "on" or g.sumonly == "on": sum = [] tmp_ts = [] max_size = 0 for serie in series: serie_vs = [x[1] for x in serie["data"]] if len(serie_vs) > max_size: max_size = len(serie_vs) tmp_ts = [x[0] for x in serie["data"]] sum = merge_list(sum, serie_vs) sum_serie_data = [] for i in range(0, max_size): sum_serie_data.append((tmp_ts[i], sum[i])) sum_serie['data'] = sum_serie_data series.append(sum_serie) if g.sumonly == "on": ret['series'] = [ sum_serie, ] else: ret['series'] = series return json.dumps(ret)
def multi_endpoints_chart_data(): if not g.id: abort(400, "no graph id given") tmp_graph = TmpGraph.get(g.id) if not tmp_graph: abort(404, "no graph which id is %s" % g.id) counters = tmp_graph.counters if not counters: abort(400, "no counters of %s" % g.id) counters = sorted(set(counters)) endpoints = tmp_graph.endpoints if not endpoints: abort(400, "no endpoints of %s" % (g.id, )) endpoints = sorted(set(endpoints)) ret = {"units": "", "title": "", "series": []} ret['title'] = counters[0] c = counters[0] endpoint_counters = [] for e in endpoints: endpoint_counters.append({ "endpoint": e, "counter": c, }) if g.comp_date > 0: today = datetime.datetime.today() today_start = int(time.mktime(today.date().timetuple())) today_end = int( time.mktime( datetime.datetime(today.year, today.month, today.day, 23, 59, 59).timetuple())) start = int(request.args.get("start") or 0) end = int(request.args.get("end") or 0) if start == 0 and end == 0: g.start = today_start g.end = today_end lt = time.localtime(g.start) day_start = int( time.mktime( datetime.datetime(lt.tm_year, lt.tm_mon, lt.tm_mday, 00, 00, 00).timetuple())) duration = g.start - g.comp_date - (g.start - day_start) query_result = graph_history(endpoints, counters, g.cf, g.start, g.end) series = [] for i in range(0, len(query_result)): x = query_result[i] try: xv = [(v["timestamp"] * 1000, v["value"]) for v in x["Values"]] serie = { "data": xv, "name": query_result[i]["endpoint"], "cf": g.cf, "endpoint": query_result[i]["endpoint"], "counter": query_result[i]["counter"], } if g.comp_date > 0: serie["name"] = "This-Period: " + query_result[i]["endpoint"] series.append(serie) except: pass sum_serie = { "data": [], "name": "sum", "cf": g.cf, "endpoint": "sum", "counter": c, } if g.comp_date > 0: sum_serie["name"] = "This-Period: sum" if g.sum == "on" or g.sumonly == "on": sum = [] tmp_ts = [] max_size = 0 for serie in series: serie_vs = [x[1] for x in serie["data"]] if len(serie_vs) > max_size: max_size = len(serie_vs) tmp_ts = [x[0] for x in serie["data"]] sum = merge_list(sum, serie_vs) sum_serie_data = [] for i in range(0, max_size): sum_serie_data.append((tmp_ts[i], sum[i])) sum_serie['data'] = sum_serie_data series.append(sum_serie) if g.sumonly == "on": ret['series'] = [ sum_serie, ] else: ret['series'] = series if g.comp_date > 0: series_comp = [] g.start = g.start - duration - 60 g.end = g.end - duration + 60 query_result = graph_history(endpoints, counters, g.cf, g.start, g.end) for i in range(0, len(query_result)): x = query_result[i] try: xv = [((v["timestamp"] + duration) * 1000, v["value"]) for v in x["Values"]] serie = { "data": xv, "name": "Last-Period: " + query_result[i]["endpoint"], "cf": g.cf, "endpoint": query_result[i]["endpoint"], "counter": query_result[i]["counter"], } series_comp.append(serie) except: pass sum_serie_comp = { "data": [], "name": "Last-Period: sum", "cf": g.cf, "endpoint": "sum", "counter": c, } if g.sum == "on" or g.sumonly == "on": sum = [] tmp_ts = [] max_size = 0 for serie in series_comp: serie_vs = [x[1] for x in serie["data"]] if len(serie_vs) > max_size: max_size = len(serie_vs) tmp_ts = [x[0] for x in serie["data"]] sum = merge_list(sum, serie_vs) sum_serie_data = [] for i in range(0, max_size): sum_serie_data.append((tmp_ts[i], sum[i])) sum_serie_comp['data'] = sum_serie_data series_comp.append(sum_serie_comp) if g.sumonly == "on": ret['series'] = [sum_serie, sum_serie_comp] else: series.extend(series_comp) ret['series'] = series return json.dumps(ret)
def multi_counters_chart_data(): if not g.id: abort(400, "no graph id given") j = TmpGraph.get(g.id) if not j: abort(400, "no such tmp_graph where id=%s" % g.id) counters = j.counters if not counters: abort(400, "no counters of %s" % g.id) counters = sorted(set(counters)) endpoints = j.endpoints if not endpoints: abort(400, "no endpoints of %s" % g.id) endpoints = sorted(set(endpoints)) ret = { "units": "", "title": "", "series": [] } e = endpoints[0] ret['title'] = e query_result = graph_history(endpoints[:1], counters, g.cf, g.start, g.end) series = [] for i in range(0, len(query_result)): x = query_result[i] try: xv = [(v["timestamp"] * 1000, v["value"]) for v in x["Values"]] serie = { "data": xv, "name": query_result[i]["counter"], "cf": g.cf, "endpoint": query_result[i]["endpoint"], "counter": query_result[i]["counter"], } series.append(serie) except: print(traceback.format_exc()) pass # 通过查看dashboard_grap判断是否存在环比看图 if g.dgid: dg = DashboardGraph.get(g.dgid) if dg.relativeday > 0: query_result = graph_history(endpoints[:1], counters, g.cf, g.start - dg.relativeday * 24 * 3600, g.end - dg.relativeday * 24 * 3600) for i in range(0, len(query_result)): x = query_result[i] try: xv = [((v["timestamp"] + dg.relativeday * 24 * 3600)* 1000, v["value"]) for v in x["Values"]] serie = { "data": xv, "name": u"[环比:" + str(dg.relativeday) + u"天]" + query_result[i]["counter"], "cf": g.cf, "endpoint": query_result[i]["endpoint"], "counter": query_result[i]["counter"], } series.append(serie) except: print(traceback.format_exc()) pass sum_serie = { "data": [], "name": "sum", "cf": g.cf, "endpoint": e, "counter": "sum", } if g.sum == "on" or g.sumonly == "on": sum = [] tmp_ts = [] max_size = 0 for serie in series: serie_vs = [x[1] for x in serie["data"]] if len(serie_vs) > max_size: max_size = len(serie_vs) tmp_ts = [x[0] for x in serie["data"]] sum = merge_list(sum, serie_vs) sum_serie_data = [] for i in range(0, max_size): sum_serie_data.append((tmp_ts[i], sum[i])) sum_serie['data'] = sum_serie_data series.append(sum_serie) if g.sumonly == "on": ret['series'] = [sum_serie, ] else: ret['series'] = series return json.dumps(ret)
def multi_endpoints_chart_data(): if not g.id: abort(400, "no graph id given") j = TmpGraph.get(g.id) if not j: abort(400, "no such tmp_graph where id=%s" %g.id) counters = j.counters if not counters: abort(400, "no counters of %s" %g.id) counters = sorted(set(counters)) endpoints = j.endpoints if not endpoints: abort(400, "no endpoints of %s" %(g.id,)) endpoints = sorted(set(endpoints)) ret = { "units": "", "title": "", "series": [] } c = counters[0] ret['title'] = c query_result = graph_history(endpoints, counters[:1], g.cf, g.start, g.end) series = [] for i in range(0, len(query_result)): x = query_result[i] try: xv = [(v["timestamp"]*1000, v["value"]) for v in x["Values"]] serie = { "data": xv, "name": query_result[i]["endpoint"], "cf": g.cf, "endpoint": query_result[i]["endpoint"], "counter": query_result[i]["counter"], } series.append(serie) except: pass sum_serie = { "data": [], "name": "sum", "cf": g.cf, "endpoint": "sum", "counter": c, } if g.sum == "on" or g.sumonly == "on": sum = [] tmp_ts = [] max_size = 0 for serie in series: serie_vs = [x[1] for x in serie["data"]] if len(serie_vs) > max_size: max_size = len(serie_vs) tmp_ts = [x[0] for x in serie["data"]] sum = merge_list(sum, serie_vs) sum_serie_data = [] for i in range(0, max_size): sum_serie_data.append((tmp_ts[i], sum[i])) sum_serie['data'] = sum_serie_data series.append(sum_serie) if g.sumonly == "on": ret['series'] = [sum_serie,] else: ret['series'] = series return json.dumps(ret)