Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def start_timestamp(self):
     """
     Return end date as timestamp
     """
     return datetime_to_timestamp(self.start)
Ejemplo n.º 4
0
 def end_timestamp(self):
     """
     Return end date as timestamp
     """
     return datetime_to_timestamp(self.end)
Ejemplo n.º 5
0
 def serialize(self):
     return {
         'id': self.id,
         'message': self.message,
         'timestamp': datetime_to_timestamp(self.timestamp)
     }
Ejemplo n.º 6
0
 def fim_timestamp(self):
     return datetime_to_timestamp(self.fim)
Ejemplo n.º 7
0
 def inicio_timestamp(self):
     return datetime_to_timestamp(self.inicio)
Ejemplo n.º 8
0
 def test_raise_datetime_to_timestamp(self):
     self.assertRaises(TypeError, datetime_to_timestamp(self.now),
                       type(self.now) != datetime.datetime, self.timestamp)
Ejemplo n.º 9
0
 def test_datetime_to_timestamp(self):
     self.assertEqual(self.timestamp, datetime_to_timestamp(self.now))