Merge pull request #349 from eReuse/feature/3785-add-columns-to-inform
add new colums in report
This commit is contained in:
commit
da6ca7bb7e
|
@ -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,71 @@ 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 = {}
|
||||
disks = []
|
||||
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, disks)
|
||||
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
|
||||
|
|
|
@ -620,18 +620,22 @@ 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:
|
||||
# Snapshot (Registers)
|
||||
# Snapshots (Update)
|
||||
# Snapshots (All)
|
||||
# Drives Erasure
|
||||
# Drives Erasure Uniques
|
||||
# Placeholders
|
||||
# Allocate
|
||||
# Deallocate
|
||||
# Live
|
||||
self.actions = actions
|
||||
year, month = create.split('-')
|
||||
self.disks = disks
|
||||
|
||||
self['User'] = user
|
||||
self['Year'] = year
|
||||
|
@ -640,6 +644,9 @@ class InternalStatsRow(OrderedDict):
|
|||
self['Snapshot (Registers)'] = 0
|
||||
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
|
||||
self['Lives'] = 0
|
||||
|
@ -648,13 +655,24 @@ 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
|
||||
if ac.device in self.disks:
|
||||
return ac
|
||||
self['Drives Erasure Uniques'] += 1
|
||||
self.disks.append(ac.device)
|
||||
return ac
|
||||
|
||||
def is_live(self, ac):
|
||||
if ac.type == 'Live':
|
||||
self['Lives'] += 1
|
||||
|
|
Reference in New Issue