def test_utilization_time_label(self): self.assertEqual( TimeLabel(10 * 3600 + 30 * 60 + 1000 * 3600 * 24).label, "1972092710") self.assertEqual( TimeLabel(00 * 3600 + 00 * 60 + 1000 * 3600 * 24).label, "1972092700") start = 2 * 3600 + 1000 * 3600 * 24 for x in range(start, start + 3600): self.assertEqual(TimeLabel(x).label, "1972092702") self.assertEqual( TimeLabel(datetime.datetime(2013, 2, 3, 4, 5, 6)).label, "2013020304") self.assertEqual( TimeLabel(datetime.date(2013, 2, 3)).label, "2013020300") t1 = TimeLabel.from_str("2013020311") t2 = TimeLabel.from_str("2013020312") t3 = TimeLabel.from_str("2013020315") self.assertEqual(t1 - t1, 0) self.assertEqual(t2 - t1, 1) self.assertEqual(t3 - t1, 4) self.assertEqual(t3 - t2, 3)
def test_utilization_time_label(self): self.assertEqual(TimeLabel(10 * 3600 + 30 * 60 + 1000 * 3600 * 24).label, "1972092710") self.assertEqual(TimeLabel(00 * 3600 + 00 * 60 + 1000 * 3600 * 24).label, "1972092700") start = 2 * 3600 + 1000 * 3600 * 24 for x in range(start, start + 3600): self.assertEqual(TimeLabel(x).label, "1972092702") self.assertEqual(TimeLabel(datetime.datetime(2013, 2, 3, 4, 5, 6)).label, "2013020304") self.assertEqual(TimeLabel(datetime.date(2013, 2, 3)).label, "2013020300") t1 = TimeLabel.from_str("2013020311") t2 = TimeLabel.from_str("2013020312") t3 = TimeLabel.from_str("2013020315") self.assertEqual(t1 - t1, 0) self.assertEqual(t2 - t1, 1) self.assertEqual(t3 - t1, 4) self.assertEqual(t3 - t2, 3)
def add_usage(self, usage): time_label = TimeLabel.from_str(usage.time_label) current_data = self.time_labels.get(time_label.label) if current_data: logbook.error("At least two records for resource {} and time_label: {}. " "Usages will be summarized. Usage: {}", self.resource_id, time_label, usage) return measure_type = self.service.measure.measure_type if self.service else Measure.QUANTITATIVE quantitative_service = measure_type == Measure.QUANTITATIVE resource_usage_class = ResourceUsageQuantity if quantitative_service else ResourceUsageTime resource_usage = resource_usage_class(time_label) self.time_labels[time_label.label] = resource_usage resource_usage.add_usage(time_label, usage) if not quantitative_service: self.merge_with_neighbor(time_label, resource_usage) self.total_cost += usage.cost or Decimal(0) self.total_usage_volume += usage.usage_volume or 0
def add_usage(self, usage): time_label = TimeLabel.from_str(usage.time_label) current_data = self.time_labels.get(time_label.label) if current_data: logbook.error( "At least two records for resource {} and time_label: {}. " "Usages will be summarized. Usage: {}", self.resource_id, time_label, usage) return measure_type = self.service.measure.measure_type if self.service else Measure.QUANTITATIVE quantitative_service = measure_type == Measure.QUANTITATIVE resource_usage_class = ResourceUsageQuantity if quantitative_service else ResourceUsageTime resource_usage = resource_usage_class(time_label) self.time_labels[time_label.label] = resource_usage resource_usage.add_usage(time_label, usage) if not quantitative_service: self.merge_with_neighbor(time_label, resource_usage) self.total_cost += usage.cost or Decimal(0) self.total_usage_volume += usage.usage_volume or 0