From 78f6cc89005cd00b420adb7a6db6a8b691d7bdeb 2016-08-22 15:46:49 From: Marcin Lulek Date: 2016-08-22 15:46:49 Subject: [PATCH] metrics: allow indexing items with dates in past if permanent storage is enabled --- diff --git a/backend/src/appenlight/validators.py b/backend/src/appenlight/validators.py index 263fcdc..811a0fa 100644 --- a/backend/src/appenlight/validators.py +++ b/backend/src/appenlight/validators.py @@ -492,11 +492,24 @@ class GeneralMetricSchema(colander.MappingSchema): tags = TagSchemaList(missing=colander.required) +class GeneralMetricPermanentSchema(GeneralMetricSchema): + """ + Validates universal metric schema + + """ + timestamp = colander.SchemaNode(NonTZDate(), missing=deferred_utcnow) + + class GeneralMetricsListSchema(colander.SequenceSchema): metric = GeneralMetricSchema() validator = colander.Length(1) +class GeneralMetricsPermanentListSchema(colander.SequenceSchema): + metric = GeneralMetricPermanentSchema() + validator = colander.Length(1) + + class MetricsListSchema(colander.SequenceSchema): """ Validates list of metrics objects ie: diff --git a/backend/src/appenlight/views/api.py b/backend/src/appenlight/views/api.py index cf5bd12..2e5e4a3 100644 --- a/backend/src/appenlight/views/api.py +++ b/backend/src/appenlight/views/api.py @@ -42,7 +42,9 @@ from appenlight.lib.request import JSONException from appenlight.validators import (LogListSchema, MetricsListSchema, GeneralMetricsListSchema, + GeneralMetricsPermanentListSchema, GeneralMetricSchema, + GeneralMetricPermanentSchema, LogListPermanentSchema, ReportListSchema_0_5, LogSchema, @@ -235,10 +237,19 @@ def general_metrics_create(request): payload = request.unsafe_json_body sequence_accepted = request.matched_route.name == 'api_general_metrics' if sequence_accepted: - schema = GeneralMetricsListSchema().bind( - utcnow=datetime.datetime.utcnow()) + if application.allow_permanent_storage: + schema = GeneralMetricsPermanentListSchema().bind( + utcnow=datetime.datetime.utcnow()) + else: + schema = GeneralMetricsListSchema().bind( + utcnow=datetime.datetime.utcnow()) else: - schema = GeneralMetricSchema().bind(utcnow=datetime.datetime.utcnow()) + if application.allow_permanent_storage: + schema = GeneralMetricPermanentSchema().bind( + utcnow=datetime.datetime.utcnow()) + else: + schema = GeneralMetricSchema().bind( + utcnow=datetime.datetime.utcnow()) deserialized_metrics = schema.deserialize(payload) if sequence_accepted is False: