diff --git a/ocw/lib/dump_state.py b/ocw/lib/dump_state.py index f9a1e60c..27b3a5bf 100644 --- a/ocw/lib/dump_state.py +++ b/ocw/lib/dump_state.py @@ -9,14 +9,36 @@ def dump_state(): - for namespace in PCWConfig.get_namespaces_for('influxdb'): + for namespace in PCWConfig.get_namespaces_for("influxdb"): try: - providers = PCWConfig.get_providers_for('influxdb', namespace) - logger.info("[%s] Dump state %s", namespace, ','.join(providers)) + providers = PCWConfig.get_providers_for("influxdb", namespace) + logger.info("[%s] Dump state %s", namespace, ",".join(providers)) if ProviderChoice.AZURE in providers: - Influx().dump_resource(ProviderChoice.AZURE.value, Influx.VMS_QUANTITY, Azure(namespace).list_instances) - Influx().dump_resource(ProviderChoice.AZURE.value, Influx.IMAGES_QUANTITY, Azure(namespace).list_images) - Influx().dump_resource(ProviderChoice.AZURE.value, Influx.DISK_QUANTITY, Azure(namespace).list_disks) - Influx().dump_resource(ProviderChoice.AZURE.value, Influx.IMAGE_VERSION_QUANTITY, Azure(namespace).get_img_versions_count) + Influx().dump_resource( + ProviderChoice.AZURE.value, + Influx.VMS_QUANTITY, + namespace, + Azure(namespace).list_instances, + ) + Influx().dump_resource( + ProviderChoice.AZURE.value, + Influx.IMAGES_QUANTITY, + namespace, + Azure(namespace).list_images, + ) + Influx().dump_resource( + ProviderChoice.AZURE.value, + Influx.DISK_QUANTITY, + namespace, + Azure(namespace).list_disks, + ) + Influx().dump_resource( + ProviderChoice.AZURE.value, + Influx.IMAGE_VERSION_QUANTITY, + namespace, + Azure(namespace).get_img_versions_count, + ) except Exception: - logger.exception("[%s] Dump state failed!: \n %s", namespace, traceback.format_exc()) + logger.exception( + "[%s] Dump state failed!: \n %s", namespace, traceback.format_exc() + ) diff --git a/ocw/lib/influx.py b/ocw/lib/influx.py index ce654384..774ebb50 100644 --- a/ocw/lib/influx.py +++ b/ocw/lib/influx.py @@ -18,6 +18,7 @@ class Influx: IMAGES_QUANTITY: str = "images_quantity" DISK_QUANTITY: str = "disk_quantity" IMAGE_VERSION_QUANTITY: str = "img_version_quantity" + NAMESPACE_TAG: str = "namespace" def __init__(self) -> None: if self.__client is None: @@ -39,15 +40,15 @@ def __new__(cls: type["Influx"]) -> "Influx": cls.instance = super(Influx, cls).__new__(cls) return cls.instance - def write(self, measurement: str, field: str, value: int) -> None: + def write(self, measurement: str, field: str, value: int, namespace: str) -> None: if self.__client: - point = Point(measurement).field(field, value) + point = Point(measurement).field(field, value).tag(Influx.NAMESPACE_TAG, namespace) try: self.__client.write(bucket=self.bucket, org=self.org, record=point) except (InfluxDBError, HTTPError) as exception: logger.warning("Failed to write to influxdb(record=%s): %s", point, exception) - def dump_resource(self, provider: str, field: str, dump_method: Callable) -> None: + def dump_resource(self, provider: str, field: str, namespace: str, dump_method: Callable) -> None: return_value = dump_method() if isinstance(return_value, list): items_cnt = len(return_value) @@ -56,4 +57,4 @@ def dump_resource(self, provider: str, field: str, dump_method: Callable) -> Non else: raise ValueError(f"{dump_method} returned unsupported type {type(return_value)}") logger.debug("%s=%d for %s", field, items_cnt, provider) - self.write(provider, field, items_cnt) + self.write(provider, field, items_cnt, namespace=namespace)