Skip to content

Commit db7358f

Browse files
author
Francisco Santiago
committed
Create project base structure
1 parent 87cc4ba commit db7358f

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

elasticparser/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22

3-
from .parser import agg_to_df
3+
from .parser import flatten_agg

elasticparser/elasticparser.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ignore = [ 'aggs', 'aggregations' ]
2+
13
class ElasticParser():
24

35
def cardinality(agg):
@@ -10,4 +12,7 @@ def date_histogram(agg):
1012
pass
1113

1214
def flatten(agg):
15+
"""Converts a nested elasticsearch response object to a
16+
flat list of dictionaries.
17+
"""
1318
pass

elasticparser/parsers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from .elasticparser import ElasticParser
22

3-
def agg_to_df(agg):
3+
def flatten_agg(agg):
44
"""Converts a nested elasticsearch response object to a
55
flat list of dictionaries.
66
"""
77

8-
return ElasticParser().flatten(agg)
8+
return ElasticParser().flatten(agg)

tests/test_elasticsearch.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def test_agg_to_df_date_hist_max(self):
88
- max
99
"""
1010

11-
query = {"aggs":{"daily":{"date_histogram":{"field":"timestamp","interval":"day"},"aggs":{"temperature":{"max":{"field":"temperature"}},"rain":{"max":{"field":"rain"}},"wind_speed":{"max":{"field":"wind_speed"}}}}},"query":{"bool":{"filter":{"range":{"timestamp":{"gte":1496002400,"lte":1496302400}}}}},"size":0}
11+
query = {"aggs":{"daily":{"date_histogram":{"field":"timestamp","interval":"day"},"aggs":{"temperature":{"max":{"field":"temperature"}},"rain_volume":{"max":{"field":"rain_volume"}},"wind_speed":{"max":{"field":"wind_speed"}}}}},"query":{"bool":{"filter":{"range":{"timestamp":{"gte":1496002400,"lte":1496302400}}}}},"size":0}
1212

1313
result = {
1414
"took": 7,
@@ -29,54 +29,54 @@ def test_agg_to_df_date_hist_max(self):
2929
"key_as_string": "1495929600",
3030
"key": 1495929600000,
3131
"doc_count": 450,
32-
"observed_tp_replaytv": { "value": 32317308 },
33-
"observed_tp_vod": { "value": 37417283 },
34-
"observed_tp_livetv": { "value": 77495254 }
32+
"temperature": { "value": 32317308 },
33+
"rain_volume": { "value": 37417283 },
34+
"wind_speed": { "value": 77495254 }
3535
},
3636
{
3737
"key_as_string": "1496016000",
3838
"key": 1496016000000,
3939
"doc_count": 325,
40-
"observed_tp_replaytv": { "value": None },
41-
"observed_tp_vod": { "value": 418968 },
42-
"observed_tp_livetv": { "value": 3986292 }
40+
"temperature": { "value": None },
41+
"rain_volume": { "value": 418968 },
42+
"wind_speed": { "value": 3986292 }
4343
},
4444
{
4545
"key_as_string": "1496102400",
4646
"key": 1496102400000,
4747
"doc_count": 2621,
48-
"observed_tp_replaytv": { "value": 2966713 },
49-
"observed_tp_vod": { "value": 3328655 },
50-
"observed_tp_livetv": { "value": 6485277 }
48+
"temperature": { "value": 2966713 },
49+
"rain_volume": { "value": 3328655 },
50+
"wind_speed": { "value": 6485277 }
5151
}
5252
]
5353
}
5454
}
5555
}
5656

57-
expected = pd.DataFrame.from_records([
57+
expected = [
5858
{
5959
"key": 1495929600000,
6060
"doc_count": 450,
61-
"observed_tp_replaytv": "32317308",
62-
"observed_tp_vod": "37417283",
63-
"observed_tp_livetv": "77495254",
61+
"temperature": 32317308,
62+
"rain_volume": 37417283,
63+
"wind_speed": 77495254,
6464
},
6565
{
6666
"key": 1496016000000,
6767
"doc_count": 325,
68-
"observed_tp_replaytv": None,
69-
"observed_tp_vod": "418968",
70-
"observed_tp_livetv": "3986292",
68+
"temperature": None,
69+
"rain_volume": 418968,
70+
"wind_speed": 3986292,
7171
},
7272
{
7373
"key": 1496102400000,
7474
"doc_count": 2621,
75-
"observed_tp_replaytv": "2966713",
76-
"observed_tp_vod": "3328655",
77-
"observed_tp_livetv": "6485277",
75+
"temperature": 2966713,
76+
"rain_volume": 3328655,
77+
"wind_speed": 6485277,
7878
},
79-
])
79+
]
8080

8181
def test_agg_to_df_date_hist_cardinality(self):
8282
"""Tests the conversion of an aggregation to data frame.

0 commit comments

Comments
 (0)