Exemplo n.º 1
0
  def _bucket_time(self, event_time):
    """
    The seconds since epoch that represent a computed bucket.

    An event bucket is the time of the earliest possible event for
    that `bucket_width`.  Example: if `bucket_width =
    timedelta(minutes=10)`, bucket times will be the number of seconds
    since epoch at 12:00, 12:10, ...  on each day.
    """
    event_time = kronos_time_to_epoch_time(event_time)
    return event_time - (event_time % self._bucket_width)
Exemplo n.º 2
0
def aggregate_stream(events, aggregator_class, field, bucket_width):
  current_bucket, aggregator = None, None
  emitted = False
  for event in events:
    timestamp = kronos_time_to_epoch_time(event[TIMESTAMP_FIELD])
    bucket = timestamp - (timestamp % bucket_width)
    if bucket != current_bucket:
      if current_bucket is not None:
        yield current_bucket, aggregator.aggregate()
        emitted = True
      current_bucket = bucket
      aggregator = aggregator_class()
      emitted = False
    aggregator.update(get_property(event, field))
  if not emitted and current_bucket and aggregator:
    yield current_bucket, aggregator.aggregate()
Exemplo n.º 3
0
  def _cached_results(self, start_time, end_time):
    """
    Retrieves cached results for any bucket that has a single cache entry.

    If a bucket has two cache entries, there is a chance that two
    different writers previously computed and cached a result since
    Kronos has no transaction semantics.  While it might be safe to
    return one of the cached results if there are multiple, we
    currently do the safe thing and pretend we have no previously
    computed data for this bucket.
    """
    cached_buckets = self._bucket_events(
      self._client.get(self._scratch_stream, start_time, end_time,
                       namespace=self._scratch_namespace))
    for bucket_events in cached_buckets:
      # If we have multiple cache entries for the same bucket, pretend
      # we have no results for that bucket.
      if len(bucket_events) == 1:
        first_result = bucket_events[0]
        yield (kronos_time_to_epoch_time(first_result[TIMESTAMP_FIELD]),
               first_result[QueryCache.CACHE_KEY])