From b597fdd3727a42a4e5bcc7579b8ef6a460596f71 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 19 Sep 2022 11:58:56 +0200 Subject: [PATCH 1/3] add new colums in report --- ereuse_devicehub/commands/reports.py | 63 ++++++++++++++++--- .../resources/documents/device_row.py | 14 ++++- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/ereuse_devicehub/commands/reports.py b/ereuse_devicehub/commands/reports.py index 8d020e7d..c6f864d5 100644 --- a/ereuse_devicehub/commands/reports.py +++ b/ereuse_devicehub/commands/reports.py @@ -5,19 +5,18 @@ import csv from io import StringIO from ereuse_devicehub.resources.action import models as evs +from ereuse_devicehub.resources.device.models import Placeholder from ereuse_devicehub.resources.documents.device_row import InternalStatsRow # import click class Report: - def __init__(self, app) -> None: super().__init__() self.app = app - self.app.cli.command('report', short_help='Creates reports devices and users.')( - self.run - ) + short_help = 'Creates reports devices and users.' + self.app.cli.command('report', short_help=short_help)(self.run) def run(self): stats = InternalStatsView() @@ -27,26 +26,70 @@ class Report: class InternalStatsView: def print(self): query = evs.Action.query.filter( - evs.Action.type.in_(('Snapshot', 'Live', 'Allocate', 'Deallocate'))) + evs.Action.type.in_( + ( + 'Snapshot', + 'Live', + 'Allocate', + 'Deallocate', + 'EraseBasic', + 'EraseSectors', + ) + ) + ) return self.generate_post_csv(query) def generate_post_csv(self, query): + data = StringIO() + cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') + cw.writerow(InternalStatsRow('', "2000-1", []).keys()) + + for row in self.get_rows(query): + cw.writerow(row) + + return print(data.getvalue()) + + def get_rows(self, query): d = {} + dd = {} for ac in query: create = '{}-{}'.format(ac.created.year, ac.created.month) user = ac.author.email if user not in d: d[user] = {} + dd[user] = {} if create not in d[user]: d[user][create] = [] + dd[user][create] = None d[user][create].append(ac) - data = StringIO() - cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') - cw.writerow(InternalStatsRow('', "2000-1", []).keys()) for user, createds in d.items(): for create, actions in createds.items(): - cw.writerow(InternalStatsRow(user, create, actions).values()) + r = InternalStatsRow(user, create, actions) + dd[user][create] = r - return print(data.getvalue()) + return self.get_placeholders(dd) + + def get_placeholders(self, dd): + + for p in Placeholder.query.all(): + create = '{}-{}'.format(p.created.year, p.created.month) + user = p.owner.email + + if user not in dd: + dd[user] = {} + + if create not in dd[user]: + dd[user][create] = None + + if not dd[user][create]: + dd[user][create] = InternalStatsRow(user, create, []) + + dd[user][create]['Placeholders'] += 1 + + rows = [] + for user, createds in dd.items(): + for create, row in createds.items(): + rows.append(row.values()) + return rows diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index 887e1aa3..d22ee8f1 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -1,7 +1,6 @@ """ This file frame a correct row for csv report """ from collections import OrderedDict - from flask import url_for from ereuse_devicehub.resources.action.models import RateComputer @@ -627,6 +626,8 @@ class InternalStatsRow(OrderedDict): # Snapshot (Registers) # Snapshots (Update) # Snapshots (All) + # Drives Erasure + # Placeholders # Allocate # Deallocate # Live @@ -640,6 +641,8 @@ class InternalStatsRow(OrderedDict): self['Snapshot (Registers)'] = 0 self['Snapshot (Update)'] = 0 self['Snapshot (All)'] = 0 + self['Drives Erasure'] = 0 + self['Placeholders'] = 0 self['Allocates'] = 0 self['Deallocates'] = 0 self['Lives'] = 0 @@ -648,13 +651,20 @@ class InternalStatsRow(OrderedDict): def count_actions(self): for ac in self.actions: - self.is_snapshot(self.is_deallocate(self.is_live(self.is_allocate(ac)))) + self.is_snapshot( + self.is_deallocate(self.is_live(self.is_allocate(self.is_erase(ac)))) + ) def is_allocate(self, ac): if ac.type == 'Allocate': self['Allocates'] += 1 return ac + def is_erase(self, ac): + if ac.type in ['EraseBasic', 'EraseSectors']: + self['Drives Erasure'] += 1 + return ac + def is_live(self, ac): if ac.type == 'Live': self['Lives'] += 1 From b118edb0a9d83ccfb0423a72447bf644dcd1495a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 19 Sep 2022 12:10:34 +0200 Subject: [PATCH 2/3] erasure only one time for row and disk --- ereuse_devicehub/resources/documents/device_row.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index d22ee8f1..ef2f5628 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -1,6 +1,7 @@ """ This file frame a correct row for csv report """ from collections import OrderedDict + from flask import url_for from ereuse_devicehub.resources.action.models import RateComputer @@ -633,6 +634,7 @@ class InternalStatsRow(OrderedDict): # Live self.actions = actions year, month = create.split('-') + self.disks = [] self['User'] = user self['Year'] = year @@ -662,7 +664,10 @@ class InternalStatsRow(OrderedDict): def is_erase(self, ac): if ac.type in ['EraseBasic', 'EraseSectors']: + if ac.device in self.disks: + return ac self['Drives Erasure'] += 1 + self.disks.append(ac.device) return ac def is_live(self, ac): From 1ec0f83d28156a1a7734b984d38adcad6ce4ef2c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 19 Sep 2022 14:04:30 +0200 Subject: [PATCH 3/3] add erasure total counts --- ereuse_devicehub/commands/reports.py | 7 ++++--- ereuse_devicehub/resources/documents/device_row.py | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/commands/reports.py b/ereuse_devicehub/commands/reports.py index c6f864d5..ae8bd501 100644 --- a/ereuse_devicehub/commands/reports.py +++ b/ereuse_devicehub/commands/reports.py @@ -42,7 +42,7 @@ class InternalStatsView: def generate_post_csv(self, query): data = StringIO() cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"') - cw.writerow(InternalStatsRow('', "2000-1", []).keys()) + cw.writerow(InternalStatsRow('', "2000-1", [], []).keys()) for row in self.get_rows(query): cw.writerow(row) @@ -52,6 +52,7 @@ class InternalStatsView: def get_rows(self, query): d = {} dd = {} + disks = [] for ac in query: create = '{}-{}'.format(ac.created.year, ac.created.month) user = ac.author.email @@ -66,7 +67,7 @@ class InternalStatsView: for user, createds in d.items(): for create, actions in createds.items(): - r = InternalStatsRow(user, create, actions) + r = InternalStatsRow(user, create, actions, disks) dd[user][create] = r return self.get_placeholders(dd) @@ -84,7 +85,7 @@ class InternalStatsView: dd[user][create] = None if not dd[user][create]: - dd[user][create] = InternalStatsRow(user, create, []) + dd[user][create] = InternalStatsRow(user, create, [], []) dd[user][create]['Placeholders'] += 1 diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index ef2f5628..ac520582 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -620,7 +620,7 @@ class ActionRow(OrderedDict): class InternalStatsRow(OrderedDict): - def __init__(self, user, create, actions): + def __init__(self, user, create, actions, disks): super().__init__() # General information about all internal stats # user, quart, month, year: @@ -628,13 +628,14 @@ class InternalStatsRow(OrderedDict): # Snapshots (Update) # Snapshots (All) # Drives Erasure + # Drives Erasure Uniques # Placeholders # Allocate # Deallocate # Live self.actions = actions year, month = create.split('-') - self.disks = [] + self.disks = disks self['User'] = user self['Year'] = year @@ -644,6 +645,7 @@ class InternalStatsRow(OrderedDict): self['Snapshot (Update)'] = 0 self['Snapshot (All)'] = 0 self['Drives Erasure'] = 0 + self['Drives Erasure Uniques'] = 0 self['Placeholders'] = 0 self['Allocates'] = 0 self['Deallocates'] = 0 @@ -664,9 +666,10 @@ class InternalStatsRow(OrderedDict): def is_erase(self, ac): if ac.type in ['EraseBasic', 'EraseSectors']: + self['Drives Erasure'] += 1 if ac.device in self.disks: return ac - self['Drives Erasure'] += 1 + self['Drives Erasure Uniques'] += 1 self.disks.append(ac.device) return ac