Ejemplo n.º 1
0
  def _get_timeframe_bounds(self, timeframe, bucket_width):
    """
    Get a `bucket_width` aligned `start_time` and `end_time` from a
    `timeframe` dict
    """
    if bucket_width:
      bucket_width_seconds = bucket_width
      bucket_width = epoch_time_to_kronos_time(bucket_width)

    # TODO(derek): Potential optimization by setting the end_time equal to the
    # untrusted_time if end_time > untrusted_time and the results are not being
    # output to the user (only for caching)
    if timeframe['mode'] == 'recent':
      # Set end_time equal to now and align to bucket width
      end_time = datetime_to_kronos_time(datetime.datetime.now())
      original_end_time = end_time
      duration = get_seconds(timeframe['value'], timeframe['scale'])
      duration = epoch_time_to_kronos_time(duration)
      start_time = original_end_time - duration

      if bucket_width:
        # Align values to the bucket width
        # TODO(derek): Warn the user that the timeframe has been altered to fit
        # the bucket width
        if (end_time % bucket_width) != 0:
          end_time += bucket_width - (end_time % bucket_width)

        if (start_time % bucket_width) != 0:
          start_time -= (start_time % bucket_width)

      start = kronos_time_to_datetime(start_time)
      end = kronos_time_to_datetime(end_time)
    elif timeframe['mode'] == 'range':
      end = datetime.datetime.strptime(timeframe['to'], DT_FORMAT)
      end_seconds = datetime_to_epoch_time(end)

      start = datetime.datetime.strptime(timeframe['from'], DT_FORMAT)
      start_seconds = datetime_to_epoch_time(start)

      if bucket_width:
        # Align values to the bucket width
        # TODO(derek): Warn the user that the timeframe has been altered to fit
        # the bucket width
        start_bump = start_seconds % bucket_width_seconds
        start -= datetime.timedelta(seconds=start_bump)
        if (end_seconds % bucket_width_seconds) != 0:
          end_bump = bucket_width_seconds - (end_seconds % bucket_width_seconds)
          end += datetime.timedelta(seconds=end_bump)
    else:
      raise ValueError("Timeframe mode must be 'recent' or 'range'")

    return start, end
Ejemplo n.º 2
0
def process_args():
  parser = argparse.ArgumentParser()
  parser.add_argument(
    '--kronos-url1',
    required=True,
    help='The first Kronos server to retrieve data from')
  parser.add_argument(
    '--kronos-url2',
    required=True,
    help='The second Kronos server to retrieve data from')
  parser.add_argument(
    '--namespace1',
    help='The namespace to read from the first Kronos server (optional)')
  parser.add_argument(
    '--namespace2',
    help='The namespace to read from the second Kronos server (optional)')
  parser.add_argument(
    '--stream1',
    help='The stream to read from the first Kronos server')
  parser.add_argument(
    '--stream2',
    help='The stream to read from the second Kronos server')
  parser.add_argument(
    '--streams-file',
    help='The name of the file with a stream name per line to copy')
  parser.add_argument(
    '--start',
    required=True,
    help='When to start retreiving? (format: 2003-09-25T10:49:41.5-03:00)')
  parser.add_argument(
    '--end',
    required=True,
    help='When to end retreiving? (format: 2003-09-25T10:49:41.5-03:00)')
  group = parser.add_argument_group(title='sampling arguments',
                                    description=('Only compare samples of '
                                                 'events.'))
  group.add_argument('--num-samples',
                     type=int,
                     help=('Number of samples to compare?'))
  group.add_argument('--sample-interval',
                     type=int,
                     default=3600,
                     help=('Interval of the sample (in seconds). Defaults to '
                           '1 hour (3600) (optional)'))
  args = parser.parse_args()

  if not bool(args.streams_file) ^ bool(args.stream1 and args.stream2):
    print 'Please specify either `stream-file` or both `stream1 and stream2`.'
    sys.exit(1)

  args.start = datetime_to_kronos_time(parse(args.start))
  args.end = datetime_to_kronos_time(parse(args.end))
  args.sample_interval = epoch_time_to_kronos_time(args.sample_interval)
  
  return args
Ejemplo n.º 3
0
  def _compute_bucket(self, bucket, untrusted_time=None, cache=False):
    bucket_start = kronos_time_to_datetime(
      epoch_time_to_kronos_time(bucket))
    bucket_end = kronos_time_to_datetime(
      epoch_time_to_kronos_time(bucket + self._bucket_width))
    bucket_events = list(self._query_function(bucket_start, bucket_end,
                                              *self._query_function_args,
                                              **self._query_function_kwargs))

    if cache:
      # If all events in the bucket happened before the untrusted
      # time, cache the query results.
      if not untrusted_time or bucket_end < untrusted_time:
        caching_event = {TIMESTAMP_FIELD: bucket_start,
                         QueryCache.CACHE_KEY: bucket_events}
        self._client.delete(self._scratch_stream, bucket_start,
                            bucket_start + timedelta(milliseconds=1),
                            namespace=self._scratch_namespace)
        self._client.put({self._scratch_stream: [caching_event]},
                         namespace=self._scratch_namespace)
    return bucket_events
Ejemplo n.º 4
0
def process_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--kronos-url1',
                        required=True,
                        help='The first Kronos server to retrieve data from')
    parser.add_argument('--kronos-url2',
                        required=True,
                        help='The second Kronos server to retrieve data from')
    parser.add_argument(
        '--namespace1',
        help='The namespace to read from the first Kronos server (optional)')
    parser.add_argument(
        '--namespace2',
        help='The namespace to read from the second Kronos server (optional)')
    parser.add_argument('--stream1',
                        help='The stream to read from the first Kronos server')
    parser.add_argument(
        '--stream2', help='The stream to read from the second Kronos server')
    parser.add_argument(
        '--streams-file',
        help='The name of the file with a stream name per line to copy')
    parser.add_argument(
        '--start',
        required=True,
        help='When to start retreiving? (format: 2003-09-25T10:49:41.5-03:00)')
    parser.add_argument(
        '--end',
        required=True,
        help='When to end retreiving? (format: 2003-09-25T10:49:41.5-03:00)')
    group = parser.add_argument_group(title='sampling arguments',
                                      description=('Only compare samples of '
                                                   'events.'))
    group.add_argument('--num-samples',
                       type=int,
                       help=('Number of samples to compare?'))
    group.add_argument(
        '--sample-interval',
        type=int,
        default=3600,
        help=('Interval of the sample (in seconds). Defaults to '
              '1 hour (3600) (optional)'))
    args = parser.parse_args()

    if not bool(args.streams_file) ^ bool(args.stream1 and args.stream2):
        print 'Please specify either `stream-file` or both `stream1 and stream2`.'
        sys.exit(1)

    args.start = datetime_to_kronos_time(parse(args.start))
    args.end = datetime_to_kronos_time(parse(args.end))
    args.sample_interval = epoch_time_to_kronos_time(args.sample_interval)

    return args