From 6334120237cdf20a2fa61f98bb2d99a94634c2a2 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 23 Nov 2020 18:03:06 +0100 Subject: [PATCH] Adding endpoint of metrics --- ereuse_devicehub/config.py | 6 ++-- ereuse_devicehub/resources/metric/__init__.py | 0 .../resources/metric/definitions.py | 32 +++++++++++++++++++ ereuse_devicehub/resources/metric/schema.py | 11 +++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 ereuse_devicehub/resources/metric/__init__.py create mode 100644 ereuse_devicehub/resources/metric/definitions.py create mode 100644 ereuse_devicehub/resources/metric/schema.py diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index 6de4a9e1..c0b1a955 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -15,6 +15,7 @@ from ereuse_devicehub.resources.documents import documents from ereuse_devicehub.resources.enums import PriceSoftware from ereuse_devicehub.resources.versions import versions from ereuse_devicehub.resources.allocate import definitions as allocate_def +from ereuse_devicehub.resources.metric import definitions as metric_def class DevicehubConfig(Config): @@ -29,8 +30,9 @@ class DevicehubConfig(Config): import_resource(documents), import_resource(inventory), import_resource(versions), - import_resource(allocate_def)), - ) + import_resource(allocate_def), + import_resource(metric_def), + ),) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] DB_USER = config('DB_USER', 'dhub') DB_PASSWORD = config('DB_PASSWORD', 'ereuse') diff --git a/ereuse_devicehub/resources/metric/__init__.py b/ereuse_devicehub/resources/metric/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/metric/definitions.py b/ereuse_devicehub/resources/metric/definitions.py new file mode 100644 index 00000000..70901e2b --- /dev/null +++ b/ereuse_devicehub/resources/metric/definitions.py @@ -0,0 +1,32 @@ +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.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() + metrics = { + "allocateds": self.allocated(), + "live": 0, + "null": 0, + } + return jsonify(metrics) + + def allocated(self): + return Allocate.query.filter( + Allocate.start_time.between( + self.params['start_time'], self.params['end_time'] + ) + ).count() + + +class MetricDef(Resource): + __type__ = 'Metric' + VIEW = MetricsView + SCHEMA = Metric + AUTH = True diff --git a/ereuse_devicehub/resources/metric/schema.py b/ereuse_devicehub/resources/metric/schema.py new file mode 100644 index 00000000..8bee7c81 --- /dev/null +++ b/ereuse_devicehub/resources/metric/schema.py @@ -0,0 +1,11 @@ +from teal.resource import Schema +from marshmallow.fields import DateTime + +class Metric(Schema): + """ + This schema filter dates for search the metrics + """ + start_time = DateTime(data_key='start_time', required=True, + description="Start date for search metrics") + end_time = DateTime(data_key='end_time', required=True, + description="End date for search metrics")