Skip to content

Commit 14d5b1b

Browse files
author
Dario Varotto
committed
Dataset.json use dataset frequency as the default one
1 parent 1c897c1 commit 14d5b1b

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

ravenpackapi/core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ravenpackapi.utils.constants import JSON_AVAILABLE_FIELDS
1515

1616
_VALID_METHODS = ('get', 'post', 'put', 'delete')
17-
VERSION = '1.0.20'
17+
VERSION = '1.0.21'
1818

1919
logger = logging.getLogger("ravenpack.core")
2020

@@ -64,7 +64,8 @@ def request(self, endpoint, data=None, params=None, method='get'):
6464
logger.error("Error calling the API, we tried: %s" % to_curl(response.request))
6565
raise APIException(
6666
'Got an error {status}: body was \'{error_message}\''.format(
67-
status=response.status_code, error_message=response.text
67+
status=response.status_code,
68+
error_message=response.text
6869
), response=response)
6970
return response
7071

ravenpackapi/models/dataset.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ def __getattr__(self, field):
5757
if field not in self._data and 'uuid' in self._data:
5858
# get the missing fields
5959
self.get_from_server()
60-
if field == 'uuid' and field not in self._data:
61-
# we can't get the uuid of a new dataset
62-
return None
60+
if field not in self._data:
61+
if field == 'uuid': # we can't get the uuid of a new dataset
62+
return None
63+
elif field == 'fields':
64+
return None
65+
elif field == 'frequency':
66+
return None
6367
return self._data[field]
6468
else:
6569
return self.__getattribute__(field)
@@ -133,7 +137,7 @@ def json(self,
133137
end_date,
134138
fields=None,
135139
time_zone=None,
136-
frequency='granular',
140+
frequency=None,
137141
having=None,
138142
):
139143
""" Use the dataset filters to request a data
@@ -148,6 +152,7 @@ def json(self,
148152
# fields are required, if it's not provided we use
149153
# the dataset ones
150154
fields = self.fields
155+
frequency = frequency or self.frequency
151156

152157
# let's build the body, with all the defined fields
153158
body = {}

ravenpackapi/tests/test_dataset_json.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ravenpackapi import RPApi
1+
from ravenpackapi import RPApi, Dataset
22
from ravenpackapi.models.results import Results
33

44

@@ -13,3 +13,39 @@ def test_known_swiss(self):
1313
)
1414
assert isinstance(data, Results)
1515
assert len(data) > 500, 'We should have more data in 1 day of swiss20'
16+
17+
def test_indicator_dataset(self):
18+
indicator_dataset = Dataset(
19+
name='Test-indicator-dataset',
20+
filters={"$and": [{"rp_entity_id": {"$in": ["D8442A"]}}]},
21+
fields=[{"average": {"avg": {"field": "EVENT_SENTIMENT_SCORE"}}}],
22+
frequency='daily',
23+
)
24+
indicator_dataset = self.api.create_dataset(indicator_dataset)
25+
try:
26+
27+
# ask the indicator dataset for its data
28+
response = indicator_dataset.json('2018-01-01 00:00', '2018-01-02 00:00')
29+
assert len(response) == 2 # we should get 2 rows
30+
assert {r['rp_entity_id'] for r in response} == {'D8442A', 'ROLLUP'}
31+
32+
# do a request overriding fields and frequency to see the underlying data
33+
response = indicator_dataset.json('2018-01-01 00:00', '2018-01-02 00:00',
34+
fields=['rp_story_id', 'rp_entity_id'],
35+
frequency='granular')
36+
assert len(response) > 200, "We should have many granular analytics rows"
37+
assert {r['rp_entity_id'] for r in response} == {'D8442A'}, "All rows should be D8442A"
38+
finally:
39+
indicator_dataset.delete()
40+
41+
def test_granular_dataset(self):
42+
self.api.log_curl_commands = True
43+
granular_dataset = Dataset(
44+
name='Test-granular-dataset',
45+
filters={"$and": [{"rp_entity_id": {"$in": ["D8442A"]}}, {"relevance": 90}]},
46+
)
47+
granular_dataset = self.api.create_dataset(granular_dataset)
48+
try:
49+
granular_dataset.json('2018-01-01 00:00', '2018-01-02 00:00')
50+
finally:
51+
granular_dataset.delete()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0.20'
3+
VERSION = '1.0.21'
44

55
with open('README.rst') as readme_file:
66
readme = readme_file.read()

0 commit comments

Comments
 (0)