This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/metric/definitions.py

64 lines
1.9 KiB
Python
Raw Normal View History

2020-11-23 17:03:06 +00:00
from flask import request, g, jsonify
from ereuse_devicehub.resources.action import schemas
from teal.resource import Resource, View
2020-11-24 20:15:49 +00:00
from ereuse_devicehub.resources.action.models import Allocate, Live, Action, ToRepair, ToPrepare
2020-11-23 17:03:06 +00:00
from ereuse_devicehub.resources.device import models as m
from ereuse_devicehub.resources.metric.schema import Metric
class MetricsView(View):
def find(self, args: dict):
2020-11-24 20:15:49 +00:00
self.params = dict(request.args)
unvalid = self.schema.validate(self.params)
if unvalid:
res = jsonify(unvalid)
res.status = 404
return res
2020-11-23 17:03:06 +00:00
metrics = {
"allocateds": self.allocated(),
2020-11-24 20:15:49 +00:00
"live": self.live(),
"null": self.nulls(),
2020-11-23 17:03:06 +00:00
}
return jsonify(metrics)
def allocated(self):
return Allocate.query.filter(
2020-11-24 20:15:49 +00:00
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
2020-11-23 17:03:06 +00:00
).count()
2020-11-24 20:15:49 +00:00
return to_repair + to_prepare
2020-11-23 17:03:06 +00:00
class MetricDef(Resource):
__type__ = 'Metric'
VIEW = MetricsView
SCHEMA = Metric
AUTH = True