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")