def test_start_stop(self): start = random() * 10000 stop = start + random() * 10000 timewindow = TimeWindow(start=start, stop=stop) TimeWindow() self.assertEqual(timewindow.start(), int(start)) self.assertEqual(timewindow.stop(), int(stop))
def test_timesteps(self): timewindow = TimeWindow(start=1508420521, stop=1508506921, timezone=-120) steps = self.timeserie.timesteps(timewindow=timewindow) self.assertEqual(len(steps), 3) self.assertEqual(steps[0], 1508371200)
def _test_CRUD(self, storage): # let's play with different data_names data_ids = ['m0', 'm1'] timewindow = TimeWindow() points = [ (timewindow.start(), None), # lower bound (timewindow.stop(), 0), # upper bound (timewindow.start() - 1, 1), # outside timewindow (minus 1) (timewindow.start() + 1, 2), # inside timewindow (plus 1) (timewindow.stop() + 1, 3) # outside timewindow (plus 1) ] sorted_points = sorted(points, key=lambda point: point[0]) inserted_points = {} # starts to put points for every aggregations and periods for data_id in data_ids: inserted_points[data_id] = points # add documents storage.put(data_id=data_id, points=points) points_count_in_timewindow = len( [point for point in points if point[0] in timewindow]) # check for reading methods for data_id in data_ids: # iterate on data_ids count = storage.count(data_id=data_id) self.assertEquals(count, len(points)) count = storage.count(data_id=data_id, timewindow=timewindow) self.assertEquals(count, points_count_in_timewindow) data = storage.get(data_id=data_id) self.assertEquals(len(data), len(points)) self.assertEquals(data, sorted_points) data = storage.get(data_id=data_id, timewindow=timewindow) self.assertEquals(len(data), points_count_in_timewindow) self.assertEquals( data, [point for point in sorted_points if point[0] in timewindow]) storage.remove(data_id=data_id, timewindow=timewindow) # check for count equals 1 count = storage.count(data_id=data_id, timewindow=timewindow) self.assertEquals(count, 0) count = storage.count(data_id=data_id) self.assertEquals(count, len(points) - points_count_in_timewindow) storage.remove(data_id=data_id) # check for count equals 0 count = storage.count(data_id=data_id) self.assertEquals(count, 0)
def test_no_startstop(self): start, stop = 5, 10 intervals = Interval((start, stop)) timewindow = TimeWindow(intervals=intervals) self.assertEqual(timewindow.start(), start) self.assertEqual(timewindow.stop(), stop)
def setUp(self): self.conf = Configuration.load(TimeSerie.CONF_PATH, Ini) self.timeserie = TimeSerie(self.conf) points = [ (ts, 1) for ts in range(0, 24 * 3600, 3600) ] self.timewindow = TimeWindow(start=points[0][0], stop=points[-1][0]) self.points = points
def count_alarms_by_period( self, start, stop, subperiod=None, limit=100, query=None, ): """ Count alarms that have been opened during (stop - start) period. :param start: Beginning timestamp of period :type start: int :param stop: End timestamp of period :type stop: int :param subperiod: Cut (stop - start) in ``subperiod`` subperiods. :type subperiod: dict :param limit: Counts cannot exceed this value :type limit: int :param query: Custom mongodb filter for alarms :type query: dict :return: List in which each item contains an interval and the related count :rtype: list """ if subperiod is None: subperiod = {'day': 1} if query is None: query = {} intervals = Interval.get_intervals_by_period(start, stop, subperiod) results = [] for date in intervals: count = self.alarm_storage.count( data_ids=None, timewindow=TimeWindow(start=date['begin'], stop=date['end']), window_start_bind=True, _filter=query, ) results.append( { 'date': date, 'count': limit if count > limit else count, } ) return results
def _five_years_timewidow(self): now = time() rd = relativedelta(years=5) now = datetime.now() past = now - rd past_ts = mktime(past.timetuple()) now_ts = mktime(now.timetuple()) result = TimeWindow(start=past_ts, stop=now_ts) return result
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 test__get_period(self): timewindow = TimeWindow(start=1508420521, stop=1508506921, timezone=-120) period = self.timeserie._get_period(timewindow=timewindow) self.assertEqual(period.total_seconds(), 86400)
def setUp(self): self.timewindow = TimeWindow()