def api_timeseries_get_list(): """ Returns timeseries data for statuses. `since` is mandatory. `until` is optional - defaults to now. Allows filtering (see `apply filters` for details). Performs aggregation by resolution (default = day). """ statuses = session.query(Status) try: statuses = apply_filters(statuses, request) except: return BadRequest("Wrong query. Allowed filters: message, since, until.") statuses = statuses.order_by(Status.timestamp) resolution = request.args.get('resolution', 'day') if resolution not in RESOLUTIONS.keys(): return BadRequest("Wrong resolution. Allowed resolutions: %s" % RESOLUTIONS.keys()) since, until = extract_since_until(request) if since is None: return BadRequest("`since` is mandatory.") if until is None: until = datetime_to_timestamp(datetime.utcnow()) timeseries = make_timeseries(statuses, since, until, RESOLUTIONS[resolution]) return jsonify(timeseries)
def make_timeseries(statuses, since, until, resolution=RESOLUTIONS["day"]): """ Arrange statuses in timeseries format. Timeseries means dict with timestamp keys and counts as values. Aggregation is performed per `resolution`. """ timeseries = OrderedDict() start = since / resolution * resolution for bucket_start in range(start, until, resolution): timeseries[bucket_start] = 0 for status in statuses: bucket_start = datetime_to_timestamp(status.timestamp) / resolution * resolution timeseries[bucket_start] += 1 return timeseries
def start_timestamp(self): """ Return end date as timestamp """ return datetime_to_timestamp(self.start)
def end_timestamp(self): """ Return end date as timestamp """ return datetime_to_timestamp(self.end)
def serialize(self): return { 'id': self.id, 'message': self.message, 'timestamp': datetime_to_timestamp(self.timestamp) }
def fim_timestamp(self): return datetime_to_timestamp(self.fim)
def inicio_timestamp(self): return datetime_to_timestamp(self.inicio)
def test_raise_datetime_to_timestamp(self): self.assertRaises(TypeError, datetime_to_timestamp(self.now), type(self.now) != datetime.datetime, self.timestamp)
def test_datetime_to_timestamp(self): self.assertEqual(self.timestamp, datetime_to_timestamp(self.now))