def test_round_timestamp(self): t = time() for unit in Period.UNITS: period = Period(**{unit: 1}) st = period.round_timestamp(t) self.assertEqual(t, st)
def test_intervals(self): """Test calculate on different intervals.""" now = time() # let a period of 1 day period = Period(day=1) oneday = period.total_seconds() rnow = period.round_timestamp(now) # let a timewindow of 10+1/4 days timewindow = TimeWindow(start=now - oneday, stop=now + 45/4 * oneday) nan = float('nan') points = [ # the first interval is empty (rnow, nan), # the second interval contains nan at start (rnow + oneday + 1, nan), # the third interval contains nan at start + 1 (rnow + 2 * oneday, 1), # the fourth interval contains 1 at start (rnow + 3 * oneday + 1, 1), # the fourth interval contains 1 at start + 1 (rnow + 4 * oneday, nan), (rnow + 4 * oneday + 1, 1), # the fith interval contains 1 and nan (rnow + 5 * oneday, 1), (rnow + 5 * oneday + 1, 1), # the sixth interval contains 1 and 1 (rnow + 6 * oneday, 1), (rnow + 6 * oneday, 1), # the sixth interval contains 1 and 1 at the same time (rnow + 7 * oneday, nan), (rnow + 7 * oneday, nan), # the sixth interval contains nan and nan at the same time ] timeserie = TimeSerie( config=self.conf, aggregation='sum', period=period, round_time=True ) _points = timeserie.calculate(points, timewindow) for i in [0, 1, 2, 5, 8, 9, 10, 11, 12]: self.assertEqual(_points[i][0], rnow + (i - 1) * oneday) self.assertTrue(isnan(_points[i][1])) for i in [3, 4]: self.assertEqual(_points[i][0], rnow + (i - 1) * oneday) self.assertEqual(_points[i][1], 1) for i in [6, 7]: self.assertEqual(_points[i][0], rnow + (i - 1) * oneday) self.assertEqual(_points[i][1], 2) self.assertEqual(len(_points), len(points) + 1)
def prepare_event( self, display_name, sla_measures, output, sla_state, alerts_percent, alerts_duration, avail_duration, timewindow_dict, now ): perf_data_array = [] # Compute metrics to publish for state in self.states: perf_data_array.append({ 'metric': 'cps_pct_by_{}'.format(state), 'value': round(sla_measures[state] * 100.0, 2), 'max': 100 }) availability = (1.0 - alerts_percent) * 100.0 perf_data_array.append({ 'metric': 'cps_avail', 'value': round(availability, 2), 'max': 100, SLIDING_TIME: True }) perf_data_array.append({ 'metric': 'cps_avail_duration', 'value': avail_duration, SLIDING_TIME: True }) perf_data_array.append({ 'metric': 'cps_alerts_duration', 'value': alerts_duration, SLIDING_TIME: True }) period_options = { timewindow_dict['durationType']: timewindow_dict['value'] } self.logger.debug(u'period options {}, now {}'.format( period_options, now )) period = Period(**period_options) periodic_timestamp = period.round_timestamp(now, next_period=True) self.logger.debug(u'periodic timestamp {}'.format(periodic_timestamp)) event = forger( connector='sla', connector_name='engine', event_type='sla', source_type='resource', component=display_name, resource='sla', state=sla_state, output=output, perf_data_array=perf_data_array, display_name=display_name, timestamp=periodic_timestamp ) self.logger.info(u'publishing sla {}, states {}'.format( display_name, sla_measures )) self.logger.debug(u'event : {}'.format(pp.pformat(event))) return event