Skip to content

Commit

Permalink
Merge pull request #335 from datosgobar/334-show-enhanced-meta-in-series
Browse files Browse the repository at this point in the history
Muestro metadatos enriquecidos en respuesta de /series
  • Loading branch information
lucaslavandeira authored Aug 6, 2018
2 parents 77b4b46 + c44ab06 commit 6ae8e18
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
25 changes: 19 additions & 6 deletions series_tiempo_ar_api/apps/api/query/query.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#! coding: utf-8
import json
from collections import OrderedDict
from typing import Union

from django.conf import settings
from django_datajsonar.models import Catalog, Dataset, Distribution, Field

from series_tiempo_ar_api.apps.api.exceptions import CollapseError
from series_tiempo_ar_api.apps.api.helpers import get_periodicity_human_format
from series_tiempo_ar_api.apps.api.query import constants
from series_tiempo_ar_api.apps.management import meta_keys
from .es_query.es_query import ESQuery

datajson_entity = Union[Catalog, Dataset, Distribution, Field]


class Query(object):
"""Encapsula la query pedida por un usuario. Tiene dos componentes
Expand Down Expand Up @@ -167,18 +171,27 @@ def _get_series_metadata(self, serie_model):

return metadata

@staticmethod
def _get_full_metadata(field):
def _get_full_metadata(self, field):
distribution = field.distribution
dataset = distribution.dataset
catalog = dataset.catalog
return {
'catalog': json.loads(catalog.metadata),
'dataset': json.loads(dataset.metadata),
'distribution': json.loads(distribution.metadata),
'field': json.loads(field.metadata)
'catalog': self._get_full_metadata_for_model(catalog),
'dataset': self._get_full_metadata_for_model(dataset),
'distribution': self._get_full_metadata_for_model(distribution),
'field': self._get_full_metadata_for_model(field),
}

def _get_full_metadata_for_model(self, model: datajson_entity):
metadata = {}
json_fields = json.loads(model.metadata)

metadata.update(json_fields)

for enhanced_meta in model.enhanced_meta.all():
metadata.update({enhanced_meta.key: enhanced_meta.value})
return metadata

def _calculate_data_frequency(self):
"""Devuelve la periodicidad de la o las series pedidas. Si son
muchas devuelve el intervalo de tiempo colapsado
Expand Down
1 change: 1 addition & 0 deletions series_tiempo_ar_api/apps/api/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def init_month_series(dataset):
)
field.save()
field.enhanced_meta.create(key=meta_keys.AVAILABLE, value='True')
field.enhanced_meta.create(key=meta_keys.PERIODICITY, value='R/P1M')

return dataset

Expand Down
20 changes: 20 additions & 0 deletions series_tiempo_ar_api/apps/api/tests/query_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.test import TestCase
from nose.tools import raises

from series_tiempo_ar_api.apps.management import meta_keys
from series_tiempo_ar_api.apps.api.exceptions import CollapseError
from django_datajsonar.models import Field
from series_tiempo_ar_api.apps.api.query.query import Query
Expand Down Expand Up @@ -200,3 +201,22 @@ def test_full_metadata_flat_response(self):
for key in meta:
for in_key in meta[key]:
self.assertEqual(meta[key][in_key], flat_meta['{}_{}'.format(key, in_key)])

def test_full_metadata_includes_enhanced_meta(self):
self.query.add_series(self.single_series, self.field)
self.query.set_metadata_config('full')
meta = self.query.get_metadata()

for enhanced_meta in self.field.enhanced_meta.all():
self.assertEqual(meta[1]['field'][enhanced_meta.key], enhanced_meta.value)

def test_full_metadata_periodicty_with_collapse(self):
self.query.add_series(self.single_series, self.field)
self.query.add_collapse('year')
self.query.set_metadata_config('full')

resp = self.query.run()

self.assertEqual(resp['meta'][0]['frequency'], 'year')
self.assertEqual(resp['meta'][1]['field'][meta_keys.PERIODICITY],
meta_keys.get(self.field, meta_keys.PERIODICITY))

0 comments on commit 6ae8e18

Please sign in to comment.