def update_cost_model_costs(request): """Update report summary tables in the database.""" params = request.query_params provider_uuid = params.get("provider_uuid") schema_name = params.get("schema") default_start_date = DateHelper().this_month_start.strftime("%Y-%m-%d") default_end_date = DateHelper().today.strftime("%Y-%m-%d") start_date = params.get("start_date", default=default_start_date) end_date = params.get("end_date", default=default_end_date) queue_name = params.get("queue") or PRIORITY_QUEUE if provider_uuid is None or schema_name is None: errmsg = "provider_uuid and schema_name are required parameters." return Response({"Error": errmsg}, status=status.HTTP_400_BAD_REQUEST) if queue_name not in QUEUE_LIST: errmsg = f"'queue' must be one of {QUEUE_LIST}." return Response({"Error": errmsg}, status=status.HTTP_400_BAD_REQUEST) try: provider = Provider.objects.get(uuid=provider_uuid) except Provider.DoesNotExist: return Response({"Error": "Provider does not exist."}, status=status.HTTP_400_BAD_REQUEST) LOG.info("Calling update_cost_model_costs async task.") async_result = chain( cost_task.s(schema_name, provider_uuid, start_date, end_date, queue_name=queue_name).set(queue=queue_name), refresh_materialized_views.si( schema_name, provider.type, provider_uuid=provider_uuid, queue_name=queue_name ).set(queue=queue_name), ).apply_async() return Response({"Update Cost Model Cost Task ID": str(async_result)})
def update_provider_uuids(self, provider_uuids): """Update rate with new provider uuids.""" current_providers_for_instance = [] for rate_map_instance in CostModelMap.objects.filter( cost_model=self._model): current_providers_for_instance.append( str(rate_map_instance.provider_uuid)) providers_to_delete = set(current_providers_for_instance).difference( provider_uuids) providers_to_create = set(provider_uuids).difference( current_providers_for_instance) all_providers = set(current_providers_for_instance).union( provider_uuids) for provider_uuid in providers_to_delete: CostModelMap.objects.filter(provider_uuid=provider_uuid, cost_model=self._model).delete() for provider_uuid in providers_to_create: # Raise exception if source is already associated with another cost model. existing_cost_model = CostModelMap.objects.filter( provider_uuid=provider_uuid) if existing_cost_model.exists(): cost_model_uuid = existing_cost_model.first().cost_model.uuid log_msg = f"Source {provider_uuid} is already associated with cost model: {cost_model_uuid}." LOG.warning(log_msg) raise CostModelException(log_msg) CostModelMap.objects.create(cost_model=self._model, provider_uuid=provider_uuid) start_date = DateHelper().this_month_start.strftime("%Y-%m-%d") end_date = DateHelper().today.strftime("%Y-%m-%d") for provider_uuid in all_providers: # Update cost-model costs for each provider, on every PUT/DELETE try: provider = Provider.objects.get(uuid=provider_uuid) except Provider.DoesNotExist: LOG.info( f"Provider {provider_uuid} does not exist. Skipping cost-model update." ) else: schema_name = provider.customer.schema_name # Because this is triggered from the UI, we use the priority queue chain( update_cost_model_costs.s( schema_name, provider.uuid, start_date, end_date, queue_name=PRIORITY_QUEUE).set(queue=PRIORITY_QUEUE), refresh_materialized_views.si( schema_name, provider.type, provider_uuid=provider.uuid, queue_name=PRIORITY_QUEUE).set(queue=PRIORITY_QUEUE), ).apply_async()