2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 from datetime import timedelta, timezone
8 def get_metric_usage(prom, start_time, end_time, metric, resampleTo="min"):
9 from prometheus_api_client.utils import parse_datetime
10 from prometheus_api_client import PrometheusConnect, MetricsList, Metric
14 chunk_size = timedelta(days=1)
16 while start < end_time:
17 if start + chunk_size > end_time:
18 chunk_size = end_time - start
20 metric_data = prom.custom_query_range(metric,
22 end_time=(start + chunk_size),
28 if len(metric_data) == 0:
31 if "__name__" not in metric_data[0]["metric"]:
32 metric_data[0]["metric"]["__name__"] = metric
34 metric_object_list = MetricsList(metric_data)
35 my_metric_object = metric_object_list[0] # one of the metrics from the list
37 series = my_metric_object.metric_values.set_index(pandas.DatetimeIndex(my_metric_object.metric_values['ds']))
39 # Resample to 1 minute increments, fill in missing values
40 rs = series.resample(resampleTo).max(1).ffill()
44 def get_data_usage(prom, timestamp, cluster):
45 from prometheus_api_client import PrometheusConnect, MetricsList, Metric
47 metric_data = prom.get_current_metric_value(metric_name='arvados_keep_total_bytes',
48 label_config={"cluster": cluster},
49 params={"time": timestamp.timestamp()})
51 metric_object_list = MetricsList(metric_data)
53 if len(metric_data) == 0:
56 my_metric_object = metric_object_list[0] # one of the metrics from the list
57 value = my_metric_object.metric_values.iloc[0]["y"]
59 metric_data = prom.get_current_metric_value(metric_name='arvados_keep_dedup_byte_ratio',
60 label_config={"cluster": cluster},
61 params={"time": timestamp.timestamp()})
63 if len(metric_data) == 0:
66 my_metric_object = MetricsList(metric_data)[0]
67 dedup_ratio = my_metric_object.metric_values.iloc[0]["y"]
69 return value, dedup_ratio