This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/commands/reports.py

97 lines
2.7 KiB
Python
Raw Permalink Normal View History

2022-07-22 13:23:49 +00:00
import csv
# import click_spinner
# import ereuse_utils.cli
from io import StringIO
from ereuse_devicehub.resources.action import models as evs
2022-09-19 09:58:56 +00:00
from ereuse_devicehub.resources.device.models import Placeholder
2022-07-22 13:23:49 +00:00
from ereuse_devicehub.resources.documents.device_row import InternalStatsRow
2022-07-22 15:49:51 +00:00
# import click
2022-07-22 13:23:49 +00:00
class Report:
def __init__(self, app) -> None:
super().__init__()
self.app = app
2022-09-19 09:58:56 +00:00
short_help = 'Creates reports devices and users.'
self.app.cli.command('report', short_help=short_help)(self.run)
2022-07-22 13:23:49 +00:00
def run(self):
stats = InternalStatsView()
stats.print()
class InternalStatsView:
def print(self):
query = evs.Action.query.filter(
2022-09-19 09:58:56 +00:00
evs.Action.type.in_(
(
'Snapshot',
'Live',
'Allocate',
'Deallocate',
'EraseBasic',
'EraseSectors',
)
)
)
2022-07-22 13:23:49 +00:00
return self.generate_post_csv(query)
def generate_post_csv(self, query):
2022-09-19 09:58:56 +00:00
data = StringIO()
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
2022-09-19 12:04:30 +00:00
cw.writerow(InternalStatsRow('', "2000-1", [], []).keys())
2022-09-19 09:58:56 +00:00
for row in self.get_rows(query):
cw.writerow(row)
return print(data.getvalue())
def get_rows(self, query):
2022-07-22 13:23:49 +00:00
d = {}
2022-09-19 09:58:56 +00:00
dd = {}
2022-09-19 12:04:30 +00:00
disks = []
2022-07-22 13:23:49 +00:00
for ac in query:
create = '{}-{}'.format(ac.created.year, ac.created.month)
user = ac.author.email
if user not in d:
d[user] = {}
2022-09-19 09:58:56 +00:00
dd[user] = {}
2022-07-22 13:23:49 +00:00
if create not in d[user]:
d[user][create] = []
2022-09-19 09:58:56 +00:00
dd[user][create] = None
2022-07-22 13:23:49 +00:00
d[user][create].append(ac)
for user, createds in d.items():
for create, actions in createds.items():
2022-09-19 12:04:30 +00:00
r = InternalStatsRow(user, create, actions, disks)
2022-09-19 09:58:56 +00:00
dd[user][create] = r
2022-07-22 13:23:49 +00:00
2022-09-19 09:58:56 +00:00
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]:
2022-09-19 12:04:30 +00:00
dd[user][create] = InternalStatsRow(user, create, [], [])
2022-09-19 09:58:56 +00:00
dd[user][create]['Placeholders'] += 1
rows = []
for user, createds in dd.items():
for create, row in createds.items():
rows.append(row.values())
return rows