added getters of averages
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2021-12-09 20:15:20 +01:00
parent 35c3643b1a
commit f5edd93ee1

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3
from flask import request, current_app, Response
from flask import request, current_app, Response, jsonify
from flask_classful import FlaskView, route
from utils import json_required, influxdb_instance
from influxdb_client import Point
@ -7,9 +7,11 @@ from influxdb_client import Point
class ReportView(FlaskView):
# LINK
@route("/link", methods=['POST'])
@json_required
def link(self):
def link_post(self):
data = request.json
points = []
@ -26,9 +28,52 @@ class ReportView(FlaskView):
influxdb_instance.connection.write_api().write("alma", current_app.config['INFLUXDB_V2_ORG'], points)
return Response(status=201)
@route("/link", methods=['GET'])
def link_get(self):
q = influxdb_instance.connection.query_api()
response = {}
# Query latency
latency_tables = q.query(f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "link_latency")
|> timedMovingAverage(every: 30s, period: 1m)
|> last()
|> yield()""")
for table in latency_tables:
for value in table.values:
key = value['client'] + ":" + value['site']
if key in response:
response[key]['latency'] = value['_value']
else:
response[key] = {'latency': value['_value']}
# Query rate
rate_tables = q.query(f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "link_rate")
|> timedMovingAverage(every: 30s, period: 1m)
|> last()
|> yield()""")
for table in rate_tables:
for value in table.values:
key = value['client'] + ":" + value['site']
if key in response:
response[key]['rate'] = value['_value']
else:
response[key] = {'rate': value['_value']}
return jsonify(response)
# CLIENT
@route("/client", methods=['POST'])
@json_required
def client(self):
def client_post(self):
data = request.json
points = []
@ -44,9 +89,35 @@ class ReportView(FlaskView):
influxdb_instance.connection.write_api().write("alma", current_app.config['INFLUXDB_V2_ORG'], points)
return Response(status=201)
@route("/client", methods=['GET'])
def client_get(self):
q = influxdb_instance.connection.query_api()
response = {}
# Query queue
latency_tables = q.query(f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "client_queue")
|> timedMovingAverage(every: 30s, period: 1m)
|> last()
|> yield()""")
for table in latency_tables:
for value in table.values:
key = value['client']
if key in response:
response[key]['queue'] = value['_value']
else:
response[key] = {'queue': value['_value']}
return jsonify(response)
# SITE
@route("/site", methods=['POST'])
@json_required
def site(self):
def site_post(self):
data = request.json
points = []
@ -61,3 +132,27 @@ class ReportView(FlaskView):
influxdb_instance.connection.write_api().write("alma", current_app.config['INFLUXDB_V2_ORG'], points)
return Response(status=201)
@route("/site", methods=['GET'])
def site_get(self):
q = influxdb_instance.connection.query_api()
response = {}
# Query queue
latency_tables = q.query(f"""from(bucket: "alma")
|> range(start: -2m, stop: now())
|> filter(fn: (r) => r["_measurement"] == "site_queue")
|> timedMovingAverage(every: 30s, period: 1m)
|> last()
|> yield()""")
for table in latency_tables:
for value in table.values:
key = value['site']
if key in response:
response[key]['queue'] = value['_value']
else:
response[key] = {'queue': value['_value']}
return jsonify(response)