From 95962a0e2c87c138e3d82967141ec5726b896d2d Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Tue, 17 Dec 2013 08:51:52 -0500 Subject: [PATCH] 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 --- elastic_recheck/results.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/elastic_recheck/results.py b/elastic_recheck/results.py index 706efbe4..c3850bab 100644 --- a/elastic_recheck/results.py +++ b/elastic_recheck/results.py @@ -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)