new version of fixed with suppress
This commit is contained in:
parent
2ed3e3f77f
commit
ab7bf75bd7
|
@ -1,17 +1,13 @@
|
||||||
from flask import request, g, jsonify
|
from flask import request, g, jsonify
|
||||||
from ereuse_devicehub.resources.action import schemas
|
from contextlib import suppress
|
||||||
from teal.resource import View
|
from teal.resource import View
|
||||||
|
|
||||||
|
from ereuse_devicehub.resources.action import schemas
|
||||||
from ereuse_devicehub.resources.action.models import Allocate, Live, Action, ToRepair, ToPrepare
|
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.device import models as m
|
||||||
from ereuse_devicehub.resources.metric.schema import Metric
|
from ereuse_devicehub.resources.metric.schema import Metric
|
||||||
|
|
||||||
|
|
||||||
def last_action(dev, action):
|
|
||||||
act = [e for e in reversed(dev.actions) if isinstance(e, action)]
|
|
||||||
return act[0] if act else None
|
|
||||||
|
|
||||||
|
|
||||||
class MetricsView(View):
|
class MetricsView(View):
|
||||||
def find(self, args: dict):
|
def find(self, args: dict):
|
||||||
|
|
||||||
|
@ -32,8 +28,12 @@ class MetricsView(View):
|
||||||
devices = m.Device.query.filter(m.Device.allocated==True)
|
devices = m.Device.query.filter(m.Device.allocated==True)
|
||||||
count = 0
|
count = 0
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
live = last_action(dev, Live)
|
live = allocate = None
|
||||||
allocate = last_action(dev, Allocate)
|
with suppress(LookupError):
|
||||||
|
live = dev.last_action_of(Live)
|
||||||
|
with suppress(LookupError):
|
||||||
|
allocate = dev.last_action_of(Allocate)
|
||||||
|
|
||||||
if not live:
|
if not live:
|
||||||
continue
|
continue
|
||||||
if allocate and allocate.created > live.created:
|
if allocate and allocate.created > live.created:
|
||||||
|
|
Reference in New Issue