clean metrics

This commit is contained in:
Cayo Puigdefabregas 2020-11-25 13:58:32 +01:00
parent 413ce7d76c
commit 4657a3bcf9
2 changed files with 4 additions and 57 deletions

View File

@ -1,59 +1,6 @@
from flask import request, g, jsonify
from ereuse_devicehub.resources.action import schemas
from teal.resource import Resource, View
from ereuse_devicehub.resources.action.models import Allocate, Live, Action, ToRepair, ToPrepare
from ereuse_devicehub.resources.device import models as m
from teal.resource import Resource
from ereuse_devicehub.resources.metric.schema import Metric
class MetricsView(View):
def find(self, args: dict):
self.params = dict(request.args)
unvalid = self.schema.validate(self.params)
if unvalid:
res = jsonify(unvalid)
res.status = 404
return res
metrics = {
"allocateds": self.allocated(),
"live": self.live(),
"null": self.nulls(),
}
return jsonify(metrics)
def allocated(self):
return Allocate.query.filter(
Allocate.start_time.between(
self.params['start_time'], self.params['end_time']
),
Action.author==g.user
).count()
def live(self):
return Live.query.filter(
Live.created.between(
self.params['start_time'], self.params['end_time']
),
Action.author==g.user
).distinct(Live.serial_number).count()
def nulls(self):
to_repair = ToRepair.query.filter(
ToRepair.created.between(
self.params['start_time'], self.params['end_time']
),
Action.author==g.user
).count()
to_prepare = ToPrepare.query.filter(
ToPrepare.created.between(
self.params['start_time'], self.params['end_time']
),
Action.author==g.user
).count()
return to_repair + to_prepare
from ereuse_devicehub.resources.metric.views import MetricsView
class MetricDef(Resource):

View File

@ -52,7 +52,7 @@ def test_simple_metrics(user: UserClient):
data = {"start_time": today-delta,
"end_time": today+delta
}
metrics = {'allocateds': 1, 'live': 1, 'null': 0}
metrics = {'allocateds': 1, 'live': 1}
res, _ = user.get("/metrics/", query_string=data)
assert res == metrics
@ -98,7 +98,7 @@ def test_second_hdd_metrics(user: UserClient):
data = {"start_time": today-delta,
"end_time": today+delta
}
metrics = {'allocateds': 1, 'live': 2, 'null': 0}
metrics = {'allocateds': 1, 'live': 2}
res, _ = user.get("/metrics/", query_string=data)
assert res == metrics