Esempio n. 1
0
    def to_representation(self, cost_model_obj):
        """Add provider UUIDs to the returned model."""
        rep = super().to_representation(cost_model_obj)
        rates = rep["rates"]
        for rate in rates:
            metric = rate.get("metric", {})
            display_data = self._get_metric_display_data(
                cost_model_obj.source_type, metric.get("name"))
            try:
                metric.update({
                    "label_metric":
                    display_data["label_metric"],
                    "label_measurement":
                    display_data["label_measurement"],
                    "label_measurement_unit":
                    display_data["label_measurement_unit"],
                })
            except (KeyError, TypeError):
                LOG.error("Invalid Cost Model Metric Map", exc_info=True)
                raise CostModelMetricMapJSONException("Internal Error.")
        rep["rates"] = rates

        source_type = rep.get("source_type")
        if source_type in SOURCE_TYPE_MAP:
            source_type = SOURCE_TYPE_MAP[source_type]
        rep["source_type"] = source_type

        rep["source_uuids"] = rep.get("provider_uuids", [])
        if rep.get("provider_uuids"):
            del rep["provider_uuids"]
        cm_uuid = cost_model_obj.uuid
        source_uuids = CostModelManager(cm_uuid).get_provider_names_uuids()
        rep.update({"sources": source_uuids})
        return rep
Esempio n. 2
0
 def metric_map(self):
     """Map metrics and display names."""
     metric_map_by_source = defaultdict(dict)
     metric_map = copy.deepcopy(metric_constants.COST_MODEL_METRIC_MAP)
     for metric in metric_map:
         try:
             metric_map_by_source[metric.get("source_type")][metric.get("metric")] = metric
         except TypeError:
             LOG.error("Invalid Cost Model Metric Map", exc_info=True)
             raise CostModelMetricMapJSONException("Internal Server Error.")
     return metric_map_by_source