From 413ce7d76c9714ba22cf36af6d479eb81c0adc37 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 24 Nov 2020 21:15:49 +0100 Subject: [PATCH] adding metrics --- .../resources/metric/definitions.py | 45 ++++++-- tests/test_metrics.py | 104 ++++++++++++++++++ 2 files changed, 142 insertions(+), 7 deletions(-) create mode 100644 tests/test_metrics.py diff --git a/ereuse_devicehub/resources/metric/definitions.py b/ereuse_devicehub/resources/metric/definitions.py index 70901e2b..134db628 100644 --- a/ereuse_devicehub/resources/metric/definitions.py +++ b/ereuse_devicehub/resources/metric/definitions.py @@ -2,28 +2,59 @@ from flask import request, g, jsonify from ereuse_devicehub.resources.action import schemas from teal.resource import Resource, View -from ereuse_devicehub.resources.action.models import Allocate +from ereuse_devicehub.resources.action.models import Allocate, Live, Action, ToRepair, ToPrepare from ereuse_devicehub.resources.device import models as m from ereuse_devicehub.resources.metric.schema import Metric class MetricsView(View): def find(self, args: dict): - self.params = request.get_json() + + self.params = dict(request.args) + unvalid = self.schema.validate(self.params) + if unvalid: + res = jsonify(unvalid) + res.status = 404 + return res + metrics = { "allocateds": self.allocated(), - "live": 0, - "null": 0, + "live": self.live(), + "null": self.nulls(), } return jsonify(metrics) def allocated(self): return Allocate.query.filter( - Allocate.start_time.between( - self.params['start_time'], self.params['end_time'] - ) + Allocate.start_time.between( + self.params['start_time'], self.params['end_time'] + ), + Action.author==g.user ).count() + def live(self): + return Live.query.filter( + Live.created.between( + self.params['start_time'], self.params['end_time'] + ), + Action.author==g.user + ).distinct(Live.serial_number).count() + + def nulls(self): + to_repair = ToRepair.query.filter( + ToRepair.created.between( + self.params['start_time'], self.params['end_time'] + ), + Action.author==g.user + ).count() + to_prepare = ToPrepare.query.filter( + ToPrepare.created.between( + self.params['start_time'], self.params['end_time'] + ), + Action.author==g.user + ).count() + return to_repair + to_prepare + class MetricDef(Resource): __type__ = 'Metric' diff --git a/tests/test_metrics.py b/tests/test_metrics.py new file mode 100644 index 00000000..5806afc7 --- /dev/null +++ b/tests/test_metrics.py @@ -0,0 +1,104 @@ +import pytest +from datetime import timedelta +from datetime import datetime + +from ereuse_devicehub.client import UserClient +from ereuse_devicehub.resources.action import models as ma +from tests import conftest +from tests.conftest import file + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_simple_metrics(user: UserClient): + """ Checks one standard query of metrics """ + # Insert computer + lenovo = file('desktop-9644w8n-lenovo-0169622.snapshot') + acer = file('acer.happy.battery.snapshot') + user.post(lenovo, res=ma.Snapshot) + snapshot, _ = user.post(acer, res=ma.Snapshot) + device_id = snapshot['device']['id'] + post_request = {"Transaction": "ccc", "name": "John", "end_users": 1, + "devices": [device_id], "description": "aaa", + "start_time": "2020-11-01T02:00:00+00:00", + "end_time": "2020-12-01T02:00:00+00:00" + } + + # Create Allocate + user.post(res=ma.Allocate, data=post_request) + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3" + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0] + hdd_action['powerCycleCount'] += 1000 + user.post(acer, res=ma.Snapshot) + + # Create a live + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec4" + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0] + hdd_action['powerCycleCount'] += 1000 + user.post(acer, res=ma.Snapshot) + + # Create an other live + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec5" + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0] + hdd_action['powerCycleCount'] += 1000 + user.post(acer, res=ma.Snapshot) + + # Check metrics + today = datetime.now() + delta = timedelta(days=30) + data = {"start_time": today-delta, + "end_time": today+delta + } + metrics = {'allocateds': 1, 'live': 1, 'null': 0} + res, _ = user.get("/metrics/", query_string=data) + assert res == metrics + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_second_hdd_metrics(user: UserClient): + """ Checks one standard query of metrics """ + # Insert computer + acer = file('acer.happy.battery.snapshot') + snapshot, _ = user.post(acer, res=ma.Snapshot) + device_id = snapshot['device']['id'] + post_request = {"Transaction": "ccc", "name": "John", "end_users": 1, + "devices": [device_id], "description": "aaa", + "start_time": "2020-11-01T02:00:00+00:00", + "end_time": "2020-12-01T02:00:00+00:00" + } + + # Create Allocate + user.post(res=ma.Allocate, data=post_request) + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec3" + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0] + hdd_action['powerCycleCount'] += 1000 + user.post(acer, res=ma.Snapshot) + + # Create a live + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec4" + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd_action = [a for a in hdd['actions'] if a['type'] == 'TestDataStorage'][0] + hdd_action['powerCycleCount'] += 1000 + user.post(acer, res=ma.Snapshot) + + # Create a second device + acer['uuid'] = "490fb8c0-81a1-42e9-95e0-5e7db7038ec5" + hdd = [c for c in acer['components'] if c['type'] == 'HardDrive'][0] + hdd['serialNumber'] = 'WD-WX11A80W7440' + user.post(acer, res=ma.Snapshot) + + # Check metrics + today = datetime.now() + delta = timedelta(days=30) + data = {"start_time": today-delta, + "end_time": today+delta + } + metrics = {'allocateds': 1, 'live': 2, 'null': 0} + res, _ = user.get("/metrics/", query_string=data) + assert res == metrics +