birb_latency_collector/birb_latency_collector/views/report_view.py

149 lines
4.7 KiB
Python
Raw Normal View History

2021-11-16 20:40:17 +01:00
#!/usr/bin/env python3
2021-12-09 20:15:20 +01:00
from flask import request, current_app, Response, jsonify
2021-12-02 23:08:25 +01:00
from flask_classful import FlaskView, route
2021-11-17 23:07:46 +01:00
from utils import json_required, influxdb_instance
from influxdb_client import Point
2021-11-16 20:40:17 +01:00
2021-12-12 01:50:41 +01:00
class ReportBuilder:
def __init__(self, tags: tuple):
self._tags = tags
self._report = {}
def update(self, query_result, type_: str, aggregator: str):
for table in query_result:
for row in table.records:
key = ":".join(row.values[tag] for tag in self._tags)
if key not in self._report:
self._report[key] = {}
if type_ not in self._report[key]:
self._report[key][type_] = {}
self._report[key][type_][aggregator] = row.values['_value']
def get_report(self) -> dict:
return self._report
2021-11-16 20:40:17 +01:00
class ReportView(FlaskView):
2021-11-17 23:07:46 +01:00
2021-12-09 20:15:20 +01:00
# LINK
2021-12-02 23:08:25 +01:00
@route("/link", methods=['POST'])
2021-11-17 23:07:46 +01:00
@json_required
2021-12-09 20:15:20 +01:00
def link_post(self):
2021-11-17 23:07:46 +01:00
data = request.json
points = []
2021-12-02 23:08:25 +01:00
for type_ in ['latency', 'rate']:
2021-11-17 23:07:46 +01:00
if type_ in data['measurements']:
points.append(
2021-12-02 23:08:25 +01:00
Point(f"link_{type_}")
2021-11-17 23:07:46 +01:00
.tag("client", data['client'])
2021-12-02 23:08:25 +01:00
.tag("site", data['site'])
.field("value", data['measurements'][type_])
2021-11-17 23:07:46 +01:00
)
2021-12-02 23:08:25 +01:00
influxdb_instance.connection.write_api().write("alma", current_app.config['INFLUXDB_V2_ORG'], points)
return Response(status=201)
2021-12-09 20:15:20 +01:00
@route("/link", methods=['GET'])
def link_get(self):
2021-12-12 01:50:41 +01:00
rep = ReportBuilder(('client', 'site'))
2021-12-09 20:15:20 +01:00
2021-12-12 01:50:41 +01:00
for type_ in ['latency', 'rate']:
for aggregator in [('mean', 'mean()'), ('derivative', 'derivative(unit: 5s)')]:
query = f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "link_{type_}")
|> {aggregator[1]}
|> yield()"""
rep.update(
influxdb_instance.query_api.query(query),
type_,
aggregator[0]
)
2021-12-09 20:15:20 +01:00
2021-12-12 01:50:41 +01:00
return jsonify(rep.get_report())
2021-12-09 20:15:20 +01:00
# CLIENT
2021-12-02 23:08:25 +01:00
@route("/client", methods=['POST'])
@json_required
2021-12-09 20:15:20 +01:00
def client_post(self):
2021-12-02 23:08:25 +01:00
data = request.json
points = []
for type_ in ['queue']:
if type_ in data['measurements']:
points.append(
Point(f"client_{type_}")
.tag("client", data['client'])
.field("value", data['measurements'][type_])
)
influxdb_instance.connection.write_api().write("alma", current_app.config['INFLUXDB_V2_ORG'], points)
return Response(status=201)
2021-12-09 20:15:20 +01:00
@route("/client", methods=['GET'])
def client_get(self):
2021-12-12 01:50:41 +01:00
rep = ReportBuilder(('client',))
2021-12-09 20:15:20 +01:00
2021-12-12 01:50:41 +01:00
for type_ in ['queue']:
for aggregator in [('mean', 'mean()'), ('derivative', 'derivative(unit: 5s)')]:
query = f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "client_{type_}")
|> {aggregator[1]}
|> yield()"""
rep.update(
influxdb_instance.query_api.query(query),
type_,
aggregator[0]
)
2021-12-09 20:15:20 +01:00
2021-12-12 01:50:41 +01:00
return jsonify(rep.get_report())
2021-12-09 20:15:20 +01:00
# SITE
2021-12-02 23:08:25 +01:00
@route("/site", methods=['POST'])
@json_required
2021-12-09 20:15:20 +01:00
def site_post(self):
2021-12-02 23:08:25 +01:00
data = request.json
points = []
for type_ in ['queue']:
if type_ in data['measurements']:
points.append(
Point(f"site_{type_}")
.tag("site", data['site'])
.field("value", data['measurements'][type_])
)
influxdb_instance.connection.write_api().write("alma", current_app.config['INFLUXDB_V2_ORG'], points)
2021-11-17 23:07:46 +01:00
return Response(status=201)
2021-12-09 20:15:20 +01:00
@route("/site", methods=['GET'])
def site_get(self):
2021-12-12 01:50:41 +01:00
rep = ReportBuilder(('site',))
2021-12-09 20:15:20 +01:00
2021-12-12 01:50:41 +01:00
for type_ in ['queue']:
for aggregator in [('mean', 'mean()'), ('derivative', 'derivative(unit: 5s)')]:
query = f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "site_{type_}")
|> {aggregator[1]}
|> yield()"""
rep.update(
influxdb_instance.query_api.query(query),
type_,
aggregator[0]
)
2021-12-09 20:15:20 +01:00
2021-12-12 01:50:41 +01:00
return jsonify(rep.get_report())