def push(self, series, value, dt=None): pipeline = self._redis.pipeline() if dt is None: dt = datetime.utcnow() timestamp = dt.isoformat() recent_key = self.named_key(series, "recent") pipeline.lpush(recent_key, value) pipeline.ltrim(recent_key, 0, 60) pipeline.sadd("series", series) pipeline.hset("latest", series, value) pipeline.hset("updated", series, timestamp) for resolution, (dt_fmt, delta) in RESOLUTIONS.items(): key = self.event_key(series, dt, dt_fmt) pipeline.hset(key, timestamp, value) pipeline.expire(key, EXPIRE_AFTER) count = len(pipeline) with timer("Redis Push (commands: {0})".format(count), self.log): pipeline.execute()
def query(self, series, start, end, resolution): dt_fmt, delta = RESOLUTIONS[resolution] current = start pipeline = self._redis.pipeline() while current <= end: key = self.event_key(series, current, dt_fmt) self.log.debug("HGETALL {0}".format(key)) pipeline.hgetall(key) current += delta count = len(pipeline) with timer("Redis Query (commands: {0})".format(count), self.log): results = pipeline.execute() results = sorted(chain(*[result.items() for result in results])) return [(r[0], Decimal(r[1])) for r in results]
def post(self, query=None): if query is None or len(query) == 0: abort(405) device_id, series_id = query['device_id'], query['series_id'] key = "D-{}:S-{}".format(device_id, series_id) start = dtparse(query['start']) end = dtparse(query['end']) precision = query.get('precision', 'daily') result_set = redis_series.query(key, start, end, precision) aggregator_func = None if 'aggregator' not in query: ds = DeviceSeries.query.filter_by( device_id=device_id, series_id=series_id, ).first() if ds.series.graph: aggregator_func = graph.get_method(ds.series.graph.aggregator) else: aggregator_func = graph.get_method(query['aggregator']) if aggregator_func: with timer("Aggregation", log): results = aggregator_func(result_set) else: results = {'full': result_set} return jsonify({ 'data': { 'results': results } })