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/views.py

45 lines
1.5 KiB
Python
Raw Normal View History

2020-11-25 12:58:53 +00:00
from flask import request, g, jsonify
from ereuse_devicehub.resources.action import schemas
from teal.resource import View
from ereuse_devicehub.resources.action.models import Allocate, Live, Action, ToRepair, ToPrepare
from ereuse_devicehub.resources.device import models as m
from ereuse_devicehub.resources.metric.schema import Metric
2020-11-25 16:46:58 +00:00
def last_action(dev, action):
act = [e for e in reversed(dev.actions) if isinstance(e, action)]
return act[0] if act else None
2020-11-25 12:58:53 +00:00
class MetricsView(View):
def find(self, args: dict):
metrics = {
"allocateds": self.allocated(),
"live": self.live(),
}
return jsonify(metrics)
def allocated(self):
# TODO @cayop we need uncomment when the pr/83 is approved
# return m.Device.query.filter(m.Device.allocated==True, owner==g.user).count()
return m.Device.query.filter(m.Device.allocated==True).count()
def live(self):
# TODO @cayop we need uncomment when the pr/83 is approved
# devices = m.Device.query.filter(m.Device.allocated==True, owner==g.user)
devices = m.Device.query.filter(m.Device.allocated==True)
count = 0
for dev in devices:
2020-11-25 16:46:58 +00:00
live = last_action(dev, Live)
allocate = last_action(dev, Allocate)
2020-11-25 12:58:53 +00:00
if not live:
continue
if allocate and allocate.created > live.created:
continue
count += 1
return count