def _getMetricCollectionTimeSliceForAutostackMetric(cls, period): """ Determine metric data collection time range, truncated at the time indicated by the current time window. :param period: Metric period in seconds; must be multiple of 60 :type period: integer :returns: time range for collecting metrics for the current time window. :rtype: htm.it.app.runtime.aggregator_utils.TimeRange """ now = datetime.utcnow().replace(second=0, microsecond=0) endTime = now - timedelta( seconds=cloudwatch_utils.getMetricCollectionBackoffSeconds(period)) startTime = endTime - timedelta(seconds=period) return TimeRange(start=startTime, end=endTime)
def testNormalizeMetricCollectionTimeRangeWithFixedStartAndDefaultEnd(self): fakeUtcnow = datetime(2015, 11, 1, 12, 0, 0) fixedStartTime = datetime(2015, 11, 1, 9, 0, 0) period = 300 datetimePatch = patch("datetime.datetime", new=Mock(wraps=datetime, utcnow=Mock(side_effect=[fakeUtcnow]))) with datetimePatch as datetimeMock: rangeStart, rangeEnd = ( cloudwatch_utils.normalizeMetricCollectionTimeRange( startTime=fixedStartTime, endTime=None, period=period) ) # Fixed start should be honored self.assertEqual(rangeStart, fixedStartTime) # Default end should be at an integral number of periods, not to exceed # the empirically-determine backoff from now and also limited by # CLOUDWATCH_MAX_DATA_RECORDS self.assertEqual(datetimeMock.utcnow.call_count, 1) rangeEndLimit = ( fakeUtcnow - timedelta(seconds=cloudwatch_utils.getMetricCollectionBackoffSeconds( period)) ) expectedRangeEnd = min( (fixedStartTime + timedelta(seconds=(rangeEndLimit - fixedStartTime).total_seconds() // period * period)), (fixedStartTime + timedelta(seconds=cloudwatch_utils.CLOUDWATCH_MAX_DATA_RECORDS * period)) ) self.assertEqual(rangeEnd, expectedRangeEnd)