allow facets to work at different resolutions

we need to support different histogram resolutions, this adds a
new parameter which is the number of seconds to bucket on.

Change-Id: If839c238f93a07b17240c8774e826f3217d447ef
This commit is contained in:
Sean Dague 2013-12-17 08:51:52 -05:00
parent dfc17b70eb
commit 95962a0e2c
1 changed files with 11 additions and 8 deletions

View File

@ -15,8 +15,10 @@
"""Elastic search wrapper to make handling results easier."""
import copy
from datetime import datetime
import datetime
import pprint
import time
import pyelasticsearch
@ -104,20 +106,21 @@ class FacetSet(dict):
Treat this basically like a dictionary (which it inherits from).
"""
def _histogram(self, data, facet):
def _histogram(self, data, facet, res=3600):
"""A preprocessor for data should we want to bucket it."""
if facet == "timestamp":
ts = datetime.strptime(data, "%Y-%m-%dT%H:%M:%S.%fZ")
# hour resolution
ts = datetime(ts.year, ts.month, ts.day, ts.hour)
ts = datetime.datetime.strptime(data, "%Y-%m-%dT%H:%M:%S.%fZ")
tsepoch = int(time.mktime(ts.timetuple()))
# take the floor based on resolution
ts -= datetime.timedelta(seconds=(tsepoch % res))
# ms since epoch
epoch = datetime.utcfromtimestamp(0)
epoch = datetime.datetime.utcfromtimestamp(0)
pos = int(((ts - epoch).total_seconds()) * 1000)
return pos
else:
return data
def detect_facets(self, results, facets):
def detect_facets(self, results, facets, res=3600):
if len(facets) > 0:
facet = facets.pop(0)
for hit in results:
@ -133,7 +136,7 @@ class FacetSet(dict):
newkeys = {}
for key in self:
fs = FacetSet()
fs.detect_facets(self[key], copy.deepcopy(facets))
fs.detect_facets(self[key], copy.deepcopy(facets), res=res)
newkeys[key] = fs
self.update(newkeys)