From 12f28ba76fa0ec584db3e00af15f01bbf5480ffd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 13 Jan 2021 18:11:41 +0100 Subject: [PATCH] metrics in csv --- ereuse_devicehub/resources/action/models.py | 24 ++++++++- ereuse_devicehub/resources/action/views.py | 4 +- ereuse_devicehub/resources/device/models.py | 49 +++++++++++++++++++ .../resources/documents/device_row.py | 14 ++++-- .../resources/documents/documents.py | 4 +- 5 files changed, 86 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index af5f1047..7dce355b 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -32,8 +32,8 @@ from sqlalchemy.ext.orderinglist import ordering_list from sqlalchemy.orm import backref, relationship, validates from sqlalchemy.orm.events import AttributeEvents as Events from sqlalchemy.util import OrderedSet -from teal.db import CASCADE_OWN, INHERIT_COND, IP, POLYMORPHIC_ID, \ - POLYMORPHIC_ON, StrictVersionType, URL, check_lower, check_range +from teal.db import (CASCADE_OWN, INHERIT_COND, IP, POLYMORPHIC_ID, + POLYMORPHIC_ON, StrictVersionType, URL, check_lower, check_range, ResourceNotFound) from teal.enums import Country, Currency, Subdivision from teal.marshmallow import ValidationError from teal.resource import url_for_resource @@ -543,6 +543,26 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): of time it took to complete. """ + def get_last_lifetimes(self): + """We get the lifetime and serial_number of the first disk""" + hdds = [] + components = copy.copy(self.components) + components.sort(key=lambda x: x.created) + for hd in components: + data = {'serial_number': None, 'lifetime': 0} + if not isinstance(hd, DataStorage): + continue + + data['serial_number'] = hd.serial_number + for act in hd.actions: + if not act.type == "TestDataStorage": + continue + data['lifetime'] = act.lifetime + break + hdds.append(data) + + return hdds + def __str__(self) -> str: return '{}. {} version {}.'.format(self.severity, self.software, self.version) diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index f1e496dd..01295028 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -124,7 +124,9 @@ class LiveView(View): """We get the liftime and serial_number of the disk""" usage_time_hdd = None serial_number = None - for hd in snapshot['components']: + components = [c for c in snapshot['components']] + components.sort(key=lambda x: x.created) + for hd in components: if not isinstance(hd, DataStorage): continue diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 87f673ce..a3f825e4 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -1,4 +1,5 @@ import pathlib +import copy from contextlib import suppress from fractions import Fraction from itertools import chain @@ -317,6 +318,54 @@ class Device(Thing): def _warning_actions(self, actions): return sorted(ev for ev in actions if ev.severity >= Severity.Warning) + def get_metrics(self): + """ + This method get a list of values for calculate a metrics from a spreadsheet + """ + actions = copy.copy(self.actions) + actions.sort(key=lambda x: x.created) + allocates = [] + from ereuse_devicehub.resources.action.models import Snapshot, Allocate, Deallocate, Live + for act in actions: + if isinstance(act, Snapshot): + snapshot = act + lifestimes = snapshot.get_last_lifetimes() + lifetime = 0 + if lifestimes: + lifetime = lifestimes[0]['lifetime'] + + if isinstance(act, Allocate): + allo = {'type': 'Allocate', + 'userCode': act.final_user_code, + 'numUsers': act.end_users, + 'hid': self.hid, + 'liveCreate': 0, + 'liveLifetime': 0, + 'start': act.start_time, + 'allocateLifetime': lifetime} + allocates.append(allo) + + if isinstance(act, Live): + allocate = copy.copy(allo) + lifetime = act.usage_time_hdd + allocate['type'] = 'Live' + allocate['liveCreate'] = act.created + allocate['liveLifetime'] = lifetime + allocates.append(allocate) + + if isinstance(act, Deallocate): + deallo = {'type': 'Deallocate', + 'userCode': '', + 'numUsers': '', + 'hid': self.hid, + 'liveCreate': 0, + 'liveLifetime': lifetime, + 'start': act.start_time, + 'allocateLifetime': 0} + allocates.append(deallo) + + return allocates + def __lt__(self, other): return self.id < other.id diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index f53ea13d..82f0c628 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -365,8 +365,14 @@ def get_action(component, action): class ActionRow(OrderedDict): - def __init__(self, action: da.Action) -> None: + def __init__(self, allocate): super().__init__() - self.action = action - # General information about action - self['type'] = action.type + # General information about allocates, deallocate and lives + self['Hid'] = allocate['hid'] + self['Start'] = allocate['start'] + self['UserCode'] = allocate['userCode'] + self['NumUsers'] = allocate['numUsers'] + self['AllocateLifetime'] = allocate['allocateLifetime'] + self['Type'] = allocate['type'] + self['LiveCreate'] = allocate['liveCreate'] + self['LiveLifetime'] = allocate['liveLifetime'] diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index b9b214da..f1da06a6 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -145,8 +145,8 @@ class ActionsDocumentView(DeviceView): cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') first = True for device in query: - for action in device.actions: - d = ActionRow(action) + for allocate in device.get_metrics(): + d = ActionRow(allocate) if first: cw.writerow(d.keys()) first = False