def aggregate(self, report_id): logbook.info("Get customer usage aggregation for {}", report_id) customer = Customer.get_by_id(report_id.customer_id) if not customer: raise Exception("Customer %s not found" % report_id.customer_id) with timed("get_usage simple"): aggregated_usage = ServiceUsage.get_usage(customer, report_id.start, report_id.end) tariffs = {} services = set() for usage in aggregated_usage: service_id, tariff_id, cost, usage_volume = usage services.add(service_id) if not tariff_id: logbook.error("ServiceUsage {} is not completed. Tariff is not filled", usage) continue tariff = Tariff.get_by_id(tariff_id) tariff_report = tariffs.get(tariff_id) if tariff_report is None: tariff_report = self.tariff_report_type(tariff, customer) tariffs[tariff_id] = tariff_report tariff_report.add_usage(usage) total = Counter() for tariff_id, tariff in tariffs.items(): total_tariff, currency = tariff.aggregate() total[currency] += total_tariff for t, value in total.items(): total[t] = decimal_to_string(value) logbook.info("Aggregated {} for {}. Services: {}", total, customer, services) return self.prepare_result(list(tariffs.values()), total, customer, report_id.start, report_id.end)
def test_update_public_flavor_service(self): tariff = self.admin_client.tariff.create( as_json=True, **self.tariff_example("Tariff for update")) self.admin_client.tariff.immutable(tariff["tariff_id"]) tariff = Tariff.get_by_id(tariff["tariff_id"]) tariff.services.append(ServicePrice(self.service_nano_id, 0, True)) db.session.commit() updated = self.admin_client.tariff.update( resource_id=tariff.tariff_id, as_json=True, services=[{ "service_id": str(self.service_nano_id), "price": "11.11" }, { "service_id": str(self.service_medium_id), "price": "100" }]) db.session.add(tariff) self.assertEqual( tariff.services_as_dict()[str(self.service_nano_id)].need_changing, False) self.assertEqual( tariff.services_as_dict()[str(self.service_nano_id)].price, Decimal('11.11')) self.assertEqual( tariff.services_as_dict()[str(self.service_medium_id)].price, Decimal('23.45'))
def aggregate(self, report_id): logbook.info("Get detailed customer usage aggregation for {}", report_id) customer = Customer.get_by_id(report_id.customer_id) if not customer: raise Exception("Customer %s not found" % report_id.customer_id) with timed("get_usage simple"): aggregated_usage = ServiceUsage.get_detailed_usage( customer, report_id.start, report_id.end) tariffs = {} services = set() for usage in aggregated_usage: tariff = Tariff.get_by_id(usage.tariff_id) tariff_report = tariffs.get(usage.tariff_id) if tariff_report is None: tariff_report = self.tariff_report_type(tariff, customer) tariffs[usage.tariff_id] = tariff_report tariff_report.add_usage(usage) total = Counter() for tariff_id, tariff in tariffs.items(): total_tariff, currency = tariff.aggregate() total[currency] += total_tariff for t, value in total.items(): total[t] = decimal_to_string(value) logbook.info("Aggregated {} for {}. Services: {}", total, customer, services) return self.prepare_result(list(tariffs.values()), total, customer, report_id.start, report_id.end)
def test_tariff_add_service(self): t = Tariff.create_tariff(self.localized_name("tariff_and_services"), "Test tariff with services", "RUB", None) t.update(services=[{"service_id": "m1.small", "price": "12.3456"}]) db.session.commit() tt = Tariff.get_by_id(t.tariff_id) self.assertEqual(len(tt.services), 1) t.update(services=[{"service_id": "m1.small", "price": "23.1"}]) db.session.commit() tt = Tariff.get_by_id(t.tariff_id) self.assertEqual(tt.services_as_dict()["m1.small"].price, Decimal("23.1")) t.update(services=[{"service_id": "m1.micro", "price": "231.333333"}]) db.session.commit() tt = Tariff.get_by_id(t.tariff_id) self.assertEqual(tt.services_as_dict()["m1.micro"].price, Decimal("231.333333")) self.assertEqual(len(tt.services), 1) # m1.small was removed
def test_tariff_update(self): lname = self.localized_name("tariff_update") tariff = Tariff.create_tariff(lname, "Tariff Update", "RUB", None) db.session.commit() tariff.update({"tr": "Tariffo"}) db.session.commit() tariff.update({"en": "Super Tariff"}) tariff.update(description="new description") db.session.commit() t = Tariff.get_by_id(tariff.tariff_id) localizations = {l.language: l for l in t.localized_name} self.assertEqual(localizations["en"].localized_name, "Super Tariff") self.assertEqual(localizations["tr"].localized_name, "Tariffo") self.assertEqual(t.description, "new description")
def update_tariff(self, new_tariff_id, user_id, comment=None): from model import Tariff, CustomerHistory from task.customer import change_flavors if self.deleted: raise errors.CustomerRemoved() new_tariff = Tariff.get_by_id(new_tariff_id) if new_tariff.mutable: raise errors.AssignMutableTariff() if new_tariff.deleted: raise errors.RemovedTariff() if self.tariff.currency != new_tariff.currency: # TODO implement logic pass logbook.info("Change tariff from {} to {} for customer {}", self.tariff, new_tariff, self) self.tariff_id = new_tariff_id if self.os_tenant_id: change_flavors.delay(self.os_tenant_id, new_tariff.flavors()) CustomerHistory.tariff_changed(self, user_id, comment)
def new_tariff(self, token, localized_name, description, currency, services=None, parent_id=None, all_parameters=None): """ Create new tariff. Parameters must be sent as json object. :param LocalizedName localized_name: Dict with name localization. en is mandatory key ``{"en": "Name", "ru": "\u0418\u043c\u044f"}`` :param str description: Tariff description :param str currency: Currency code :param TariffId parent_id: Parent tariff id :param list services: List services and its prices **Example of list**:: services: [ { "service_id": "m1.small", "price": "12.23" }, { "service_id": "m1.medium", "price": "21.32" } ] } :return dict tariff_info: Dict as returned by :obj:`GET /0/tariff/\<tariff\>/ <view.GET /0/tariff/\<tariff\>>` """ if parent_id: parent = Tariff.get_by_id(parent_id) if not parent: raise errors.TariffNotFound() if parent.currency != currency: raise errors.ParentTariffCurrency() created = Tariff.create_tariff(**all_parameters) TariffHistory.create(TariffHistory.EVENT_CREATE, token.user_id, created) return {"tariff_info": display(created)}
def test_update_public_flavor_service(self): tariff = self.admin_client.tariff.create(as_json=True, **self.tariff_example("Tariff for update")) self.admin_client.tariff.immutable(tariff["tariff_id"]) tariff = Tariff.get_by_id(tariff["tariff_id"]) tariff.services.append(ServicePrice(self.service_nano_id, 0, True)) db.session.commit() updated = self.admin_client.tariff.update( resource_id=tariff.tariff_id, as_json=True, services=[ {"service_id": str(self.service_nano_id), "price": "11.11"}, {"service_id": str(self.service_medium_id), "price": "100"} ] ) db.session.add(tariff) self.assertEqual(tariff.services_as_dict()[str(self.service_nano_id)].need_changing, False) self.assertEqual(tariff.services_as_dict()[str(self.service_nano_id)].price, Decimal('11.11')) self.assertEqual(tariff.services_as_dict()[str(self.service_medium_id)].price, Decimal('23.45'))