Skip to content

Commit b74ab05

Browse files
authored
Merge pull request #18 from 4n4nd/v0.0.2
init a Metric object from an existing Metric object
2 parents b46831f + 2bd7c18 commit b74ab05

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

prometheus_api_client/metric.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,26 @@ def __init__(self, metric, oldest_data_datetime=None):
4747
Constructor for the Metric object
4848
4949
"""
50-
self.metric_name = metric["metric"]["__name__"]
51-
self.label_config = deepcopy(metric["metric"])
52-
self.oldest_data_datetime = oldest_data_datetime
53-
del self.label_config["__name__"]
50+
if isinstance(metric, Metric):
51+
# if metric is a Metric object, just copy the object and update its parameters
52+
self.metric_name = metric.metric_name
53+
self.label_config = metric.label_config
54+
self.metric_values = metric.metric_values
55+
self.oldest_data_datetime = oldest_data_datetime
56+
else:
57+
self.metric_name = metric["metric"]["__name__"]
58+
self.label_config = deepcopy(metric["metric"])
59+
self.oldest_data_datetime = oldest_data_datetime
60+
del self.label_config["__name__"]
5461

55-
# if it is a single value metric change key name
56-
if "value" in metric:
57-
metric["values"] = [metric["value"]]
62+
# if it is a single value metric change key name
63+
if "value" in metric:
64+
metric["values"] = [metric["value"]]
5865

59-
self.metric_values = pandas.DataFrame(metric["values"], columns=["ds", "y"]).apply(
60-
pandas.to_numeric, args=({"errors": "coerce"})
61-
)
62-
self.metric_values["ds"] = pandas.to_datetime(self.metric_values["ds"], unit="s")
66+
self.metric_values = pandas.DataFrame(metric["values"], columns=["ds", "y"]).apply(
67+
pandas.to_numeric, args=({"errors": "coerce"})
68+
)
69+
self.metric_values["ds"] = pandas.to_datetime(self.metric_values["ds"], unit="s")
6370

6471
def __eq__(self, other):
6572
"""

0 commit comments

Comments
 (0)