From 810a9b360c08e13305e1d5fc01367dab11c31e63 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 8 Jan 2021 17:37:52 +0100 Subject: [PATCH] adding new endpoit for download csv of metrics --- CHANGELOG.md | 1 + .../resources/documents/device_row.py | 10 ++++++ .../resources/documents/documents.py | 34 ++++++++++++++++++- tests/test_basic.py | 1 + tests/test_documents.py | 26 +++++++++++++- 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bf7df40..93602dea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ ml). - [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 get all licences in one query +- [addend] #102 adding endpoint for download metrics - [bugfix] #100 fixing bug of scheme live - [bugfix] #101 fixing bug when 2 users have one device and launch one live diff --git a/ereuse_devicehub/resources/documents/device_row.py b/ereuse_devicehub/resources/documents/device_row.py index 70e7bfda..f53ea13d 100644 --- a/ereuse_devicehub/resources/documents/device_row.py +++ b/ereuse_devicehub/resources/documents/device_row.py @@ -5,6 +5,7 @@ from flask import url_for from ereuse_devicehub.resources.enums import Severity 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, TestDataStorage) @@ -360,3 +361,12 @@ def get_action(component, action): """ Filter one action from a component or return None """ result = [a for a in component.actions if a.type == action] 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 diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index a7db64c6..b9b214da 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -20,7 +20,7 @@ from ereuse_devicehub.db import db from ereuse_devicehub.resources.action import models as evs from ereuse_devicehub.resources.device import models as devs 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.models import Lot from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash @@ -133,6 +133,32 @@ class DevicesDocumentView(DeviceView): 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): def find(self, args: dict): 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) 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) diff --git a/tests/test_basic.py b/tests/test_basic.py index 978948af..31208608 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -49,6 +49,7 @@ def test_api_docs(client: Client): '/displays/{dev1_id}/merge/{dev2_id}', '/diy-and-gardenings/{dev1_id}/merge/{dev2_id}', '/documents/devices/', + '/documents/actions/', '/documents/erasures/', '/documents/lots/', '/documents/static/{filename}', diff --git a/tests/test_documents.py b/tests/test_documents.py index eecee5ad..bb7d802b 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -85,7 +85,7 @@ def test_erasure_certificate_wrong_id(client: Client): @pytest.mark.mvp 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) csv_user, _ = user.get(res=documents.DocumentDef.t, item='devices/', @@ -107,6 +107,30 @@ def test_export_csv_permitions(user: UserClient, user2: UserClient, client: Clie 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 def test_export_basic_snapshot(user: UserClient): """Test export device information in a csv file."""