adding new endpoit for download csv of metrics

This commit is contained in:
Cayo Puigdefabregas 2021-01-08 17:37:52 +01:00
parent 87bb2265ee
commit 810a9b360c
5 changed files with 70 additions and 2 deletions

View File

@ -15,6 +15,7 @@ ml).
- [addend] #95 adding endpoint for check the hash of one report - [addend] #95 adding endpoint for check the hash of one report
- [addend] #98 adding endpoint for insert a new live - [addend] #98 adding endpoint for insert a new live
- [addend] #98 adding endpoint for get all licences in one query - [addend] #98 adding endpoint for get all licences in one query
- [addend] #102 adding endpoint for download metrics
- [bugfix] #100 fixing bug of scheme live - [bugfix] #100 fixing bug of scheme live
- [bugfix] #101 fixing bug when 2 users have one device and launch one live - [bugfix] #101 fixing bug when 2 users have one device and launch one live

View File

@ -5,6 +5,7 @@ from flask import url_for
from ereuse_devicehub.resources.enums import Severity from ereuse_devicehub.resources.enums import Severity
from ereuse_devicehub.resources.device import models as d, states from ereuse_devicehub.resources.device import models as d, states
from ereuse_devicehub.resources.action import models as da
from ereuse_devicehub.resources.action.models import (BenchmarkDataStorage, RateComputer, from ereuse_devicehub.resources.action.models import (BenchmarkDataStorage, RateComputer,
TestDataStorage) TestDataStorage)
@ -360,3 +361,12 @@ def get_action(component, action):
""" Filter one action from a component or return None """ """ Filter one action from a component or return None """
result = [a for a in component.actions if a.type == action] result = [a for a in component.actions if a.type == action]
return result[-1] if result else None return result[-1] if result else None
class ActionRow(OrderedDict):
def __init__(self, action: da.Action) -> None:
super().__init__()
self.action = action
# General information about action
self['type'] = action.type

View File

@ -20,7 +20,7 @@ from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action import models as evs from ereuse_devicehub.resources.action import models as evs
from ereuse_devicehub.resources.device import models as devs from ereuse_devicehub.resources.device import models as devs
from ereuse_devicehub.resources.device.views import DeviceView from ereuse_devicehub.resources.device.views import DeviceView
from ereuse_devicehub.resources.documents.device_row import DeviceRow, StockRow from ereuse_devicehub.resources.documents.device_row import DeviceRow, StockRow, ActionRow
from ereuse_devicehub.resources.lot import LotView from ereuse_devicehub.resources.lot import LotView
from ereuse_devicehub.resources.lot.models import Lot from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash
@ -133,6 +133,32 @@ class DevicesDocumentView(DeviceView):
return output return output
class ActionsDocumentView(DeviceView):
@cache(datetime.timedelta(minutes=1))
def find(self, args: dict):
query = (x for x in self.query(args) if x.owner_id == g.user.id)
return self.generate_post_csv(query)
def generate_post_csv(self, query):
"""Get device query and put information in csv format."""
data = StringIO()
cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
first = True
for device in query:
for action in device.actions:
d = ActionRow(action)
if first:
cw.writerow(d.keys())
first = False
cw.writerow(d.values())
bfile = data.getvalue().encode('utf-8')
output = make_response(bfile)
insert_hash(bfile)
output.headers['Content-Disposition'] = 'attachment; filename=actions_export.csv'
output.headers['Content-type'] = 'text/csv'
return output
class LotsDocumentView(LotView): class LotsDocumentView(LotView):
def find(self, args: dict): def find(self, args: dict):
query = (x for x in self.query(args) if x.owner_id == g.user.id) query = (x for x in self.query(args) if x.owner_id == g.user.id)
@ -256,3 +282,9 @@ class DocumentDef(Resource):
check_view = CheckView.as_view('CheckView', definition=self, auth=app.auth) check_view = CheckView.as_view('CheckView', definition=self, auth=app.auth)
self.add_url_rule('/check/', defaults={}, view_func=check_view, methods=get) self.add_url_rule('/check/', defaults={}, view_func=check_view, methods=get)
actions_view = ActionsDocumentView.as_view('ActionsDocumentView',
definition=self,
auth=app.auth)
actions_view = app.auth.requires_auth(actions_view)
self.add_url_rule('/actions/', defaults=d, view_func=actions_view, methods=get)

View File

@ -49,6 +49,7 @@ def test_api_docs(client: Client):
'/displays/{dev1_id}/merge/{dev2_id}', '/displays/{dev1_id}/merge/{dev2_id}',
'/diy-and-gardenings/{dev1_id}/merge/{dev2_id}', '/diy-and-gardenings/{dev1_id}/merge/{dev2_id}',
'/documents/devices/', '/documents/devices/',
'/documents/actions/',
'/documents/erasures/', '/documents/erasures/',
'/documents/lots/', '/documents/lots/',
'/documents/static/{filename}', '/documents/static/{filename}',

View File

@ -85,7 +85,7 @@ def test_erasure_certificate_wrong_id(client: Client):
@pytest.mark.mvp @pytest.mark.mvp
def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Client): def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Client):
"""Test export device information in a csv file with others users.""" """test export device information in a csv file with others users."""
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot) snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)
csv_user, _ = user.get(res=documents.DocumentDef.t, csv_user, _ = user.get(res=documents.DocumentDef.t,
item='devices/', item='devices/',
@ -107,6 +107,30 @@ def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Clie
assert len(csv_user2) == 0 assert len(csv_user2) == 0
@pytest.mark.mvp
def test_export_csv_actions(user: UserClient, user2: UserClient, client: Client):
"""Test export device information in a csv file with others users."""
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)
csv_user, _ = user.get(res=documents.DocumentDef.t,
item='actions/',
accept='text/csv',
query=[('filter', {'type': ['Computer']})])
csv_user2, _ = user2.get(res=documents.DocumentDef.t,
item='actions/',
accept='text/csv',
query=[('filter', {'type': ['Computer']})])
_, res = client.get(res=documents.DocumentDef.t,
item='actions/',
accept='text/csv',
query=[('filter', {'type': ['Computer']})], status=401)
assert res.status_code == 401
assert len(csv_user) > 0
assert len(csv_user2) == 0
@pytest.mark.mvp @pytest.mark.mvp
def test_export_basic_snapshot(user: UserClient): def test_export_basic_snapshot(user: UserClient):
"""Test export device information in a csv file.""" """Test export device information in a csv file."""