def calculate_throughputs(taskname, timerange, interval=1): """ Calculates the throughputs for a given task for each interval over the given timerange. timerange = Range over which to calculate throughputs. Must be a tuple of two datetime.datetime or datetime.date objects. interval = How often (in seconds) to calculate throughputs. Returns a list of throughputs. Each throughput is calculated over the given interval. The throughputs collectively span the given time range. """ qargs = { 'state': 'SUCCESS', 'tstamp__range': timerange } if taskname: qargs['name'] = taskname states_in_range = DispatchedTask.objects.filter(**qargs) def rate_aggregator(seconds_span): def _rate_aggregator(seg): return seg.count() / seconds_span return _rate_aggregator interval_secs = datetime.timedelta(seconds=interval) segmentizer = Segmentizer(range_query_sequence('tstamp', timerange, interval_secs)) aggregator = rate_aggregator(float(interval)) segments = make_segments(states_in_range, segmentizer, aggregator) # segments contains tuple pairs: (label, value). We only want the value... return map(operator.itemgetter(1), segments)
def test_basic(self): fieldname = 'thefield' range = (3,8) interval_size = 2 res = segmentize.range_query_sequence(fieldname, range, interval_size) qseq = list(res) self.assertEquals(len(qseq), 3) self.assertEquals(qseq[0], (4,{'thefield__gte': 3, 'thefield__lt': 5})) self.assertEquals(qseq[1], (6,{'thefield__gte': 5, 'thefield__lt': 7})) self.assertEquals(qseq[2], (8,{'thefield__gte': 7, 'thefield__lt': 9}))
def test_dates(self): fieldname = 'date' d = datetime.date range = (d(2010,10,1),d(2010,10,20)) interval_size = datetime.timedelta(days=7) res = segmentize.range_query_sequence(fieldname, range, interval_size) qseq = list(res) self.assertEquals(len(qseq), 3) self.assertEquals(qseq[0], ( d(2010,10,4), {'date__gte': d(2010,10,1), 'date__lt': d(2010,10,8)} ) ) self.assertEquals(qseq[1], ( d(2010,10,11), {'date__gte': d(2010,10,8), 'date__lt': d(2010,10,15)} ) ) self.assertEquals(qseq[2], ( d(2010,10,18), {'date__gte': d(2010,10,15), 'date__lt': d(2010,10,22)} ) )
def test_daterange(self): from celerymanagementapp.segmentize import make_segments, Segmentizer from celerymanagementapp.segmentize import range_query_sequence from celerymanagementapp.segmentize import count_aggregator d = datetime.date seq_fieldname = 'date' range = (d(2010,1,1),d(2010,1,25)) interval_size = datetime.timedelta(days=5) qs = TestModel.objects.all() segmentizer = Segmentizer( range_query_sequence(seq_fieldname, range, interval_size) ) aggregator = count_aggregator() segs = make_segments(qs, segmentizer, aggregator) self.assertEquals(len(segs), 5) self.assertEquals(segs[0], (d(2010,1,3), 2)) self.assertEquals(segs[1], (d(2010,1,8), 0)) self.assertEquals(segs[2], (d(2010,1,13), 2)) self.assertEquals(segs[3], (d(2010,1,18), 1)) self.assertEquals(segs[4], (d(2010,1,23), 1))