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

51 lines
1.5 KiB
Python
Raw Permalink Normal View History

2020-11-25 16:57:43 +00:00
from contextlib import suppress
2023-03-21 11:08:13 +00:00
from flask import g, jsonify, request
2020-11-25 12:58:53 +00:00
2020-11-25 16:57:43 +00:00
from ereuse_devicehub.resources.action import schemas
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.resources.action.models import (
Action,
Allocate,
Live,
ToPrepare,
ToRepair,
)
2020-11-25 12:58:53 +00:00
from ereuse_devicehub.resources.device import models as m
from ereuse_devicehub.resources.metric.schema import Metric
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.teal.resource import View
2020-11-25 12:58:53 +00:00
class MetricsView(View):
def find(self, args: dict):
metrics = {
2023-03-21 11:08:13 +00:00
"allocateds": self.allocated(),
"live": self.live(),
2020-11-25 12:58:53 +00:00
}
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()
2023-03-21 11:08:13 +00:00
return m.Device.query.filter(m.Device.allocated == True).count()
2020-11-25 12:58:53 +00:00
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)
2023-03-21 11:08:13 +00:00
devices = m.Device.query.filter(m.Device.allocated == True)
2020-11-25 12:58:53 +00:00
count = 0
for dev in devices:
2020-11-25 16:57:43 +00:00
live = allocate = None
with suppress(LookupError):
live = dev.last_action_of(Live)
with suppress(LookupError):
allocate = dev.last_action_of(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