add new colums in report

This commit is contained in:
Cayo Puigdefabregas 2022-09-19 11:58:56 +02:00
parent f100eb2ecb
commit b597fdd372
2 changed files with 65 additions and 12 deletions

View File

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

View File

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