From c34cbb5c26c3e35b1bb2a55cac11aca4dbe9bf60 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 2 Nov 2021 12:47:23 +0100 Subject: [PATCH] fixing metrics --- ereuse_devicehub/resources/device/metrics.py | 24 ++++++++++---------- tests/test_metrics.py | 13 ++++++++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/ereuse_devicehub/resources/device/metrics.py b/ereuse_devicehub/resources/device/metrics.py index 898fc5d8..dcb1d3b8 100644 --- a/ereuse_devicehub/resources/device/metrics.py +++ b/ereuse_devicehub/resources/device/metrics.py @@ -65,26 +65,26 @@ class Metrics(MetricsMix): If exist one trade before this action, then modify the trade action else, create one row new. """ - self.status_receiver = self.act.type - self.status_supplier = '' - if self.act.author != self.act.rol_user: - # It is neccesary exist one trade action before + if not self.last_trade: + # If not exist one trade, the status is of the Receive + self.action_create_by = 'Receiver' + self.status_receiver = self.act.type + self.status_supplier = '' + row = self.get_template_row() + row['status_receiver_created'] = self.act.created + self.rows.append(row) + return + + if self.last_trade['trade_supplier'] == self.act.rol_user.email: self.last_trade['status_supplier'] = self.act.type self.last_trade['status_supplier_created'] = self.act.created return - self.action_create_by = 'Receiver' - if self.last_trade: - # if exist one trade action before + if self.last_trade['trade_receiver'] == self.act.rol_user.email: self.last_trade['status_receiver'] = self.act.type self.last_trade['status_receiver_created'] = self.act.created return - # If not exist any trade action for this device - row = self.get_template_row() - row['status_receiver_created'] = self.act.created - self.rows.append(row) - def get_snapshot(self): """ If there are one snapshot get the last lifetime for to do a calcul of time of use. diff --git a/tests/test_metrics.py b/tests/test_metrics.py index efdb26e4..e8db619d 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -156,6 +156,10 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): res=Lot, item='{}/devices'.format(lot['id']), query=devices) + + action = {'type': ma.Refurbish.t, 'devices': [snap1['device']['id']]} + user.post(action, res=ma.Action) + request_post = { 'type': 'Trade', 'devices': [snap1['device']['id'], snap2['device']['id']], @@ -169,19 +173,22 @@ def test_complet_metrics_with_trade(user: UserClient, user2: UserClient): user.post(res=ma.Action, data=request_post) - action = {'type': ma.Refurbish.t, 'devices': [snap1['device']['id']]} + action = {'type': ma.Use.t, 'devices': [snap1['device']['id']]} action_use, _ = user.post(action, res=ma.Action) csv_str, _ = user.get(res=documents.DocumentDef.t, item='actions/', accept='text/csv', query=[('filter', {'type': ['Computer']})]) - body1_lenovo = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Trade;foo@foo.com;foo2@foo.com;Supplier;False;Refurbish;Use;' + body1_lenovo = 'O48N2;desktop-lenovo-9644w8n-0169622-00:1a:6b:5e:7f:10;;Trade;foo@foo.com;' + body1_lenovo += 'foo2@foo.com;Supplier;False;Refurbish;Use;' body2_lenovo = ';;0;0;Trade;0;0\n' - body1_acer = 'J2MA2;laptop-acer-aohappy-lusea0d010038879a01601-00:26:c7:8e:cb:8c;;Trade;foo@foo.com;foo2@foo.com;Supplier;False;;Use;;;0;' + body1_acer = 'J2MA2;laptop-acer-aohappy-lusea0d010038879a01601-00:26:c7:8e:cb:8c;;Trade;' + body1_acer += 'foo@foo.com;foo2@foo.com;Supplier;False;;Use;;;0;' body2_acer = ';;0;0;Trade;0;4692.0\n' + # import pdb; pdb.set_trace() assert body1_lenovo in csv_str assert body2_lenovo in csv_str assert body1_acer in csv_str