metrics in csv

This commit is contained in:
Cayo Puigdefabregas 2021-01-13 18:11:41 +01:00
parent 810a9b360c
commit 12f28ba76f
5 changed files with 86 additions and 9 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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']

View file

@ -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