Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.10-slim

WORKDIR /app

COPY . .

RUN pip install --no-cache-dir -U pip \
&& pip install --no-cache-dir -r requirements.txt \
&& pip install --no-cache-dir pytest

CMD ["pytest", "-q"]
61 changes: 16 additions & 45 deletions prometheus_api_client/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Metric:

# only for the first item in the list
my_metric_object = Metric(metric_data[0], datetime.timedelta(days=10))

"""

def __init__(self, metric, oldest_data_datetime=None):
Expand Down Expand Up @@ -86,33 +85,16 @@ def __eq__(self, other):

Check whether two metrics are the same (are the same time-series regardless of their data)

Example Usage:
.. code-block:: python

metric_1 = Metric(metric_data_1)

metric_2 = Metric(metric_data_2)

print(metric_1 == metric_2) # will print True if they belong to the same time-series

:return: (bool) If two Metric objects belong to the same time-series,
i.e. same name and label config, it will return True, else False
:return: (bool) True if they belong to the same time-series, else False
"""
return bool(
(self.metric_name == other.metric_name) and (self.label_config == other.label_config)
(self.metric_name == other.metric_name)
and (self.label_config == other.label_config)
)

def __str__(self):
"""
Make it print in a cleaner way when print function is used on a Metric object.

Example Usage:
.. code-block:: python

metric_1 = Metric(metric_data_1)

print(metric_1) # will print the name, labels and the head of the dataframe

"""
name = "metric_name: " + repr(self.metric_name) + "\n"
labels = "label_config: " + repr(self.label_config) + "\n"
Expand All @@ -124,46 +106,35 @@ def __add__(self, other):
r"""
Overloading operator ``+``.

Add two metric objects for the same time-series

Example Usage:
.. code-block:: python

metric_1 = Metric(metric_data_1)
metric_2 = Metric(metric_data_2)
metric_12 = metric_1 + metric_2 # will add the data in ``metric_2`` to ``metric_1``
# so if any other parameters are set in ``metric_1``
# will also be set in ``metric_12``
# (like ``oldest_data_datetime``)

:return: (`Metric`) Returns a `Metric` object with the combined metric data
of the two added metrics

:raises: (TypeError) Raises an exception when two metrics being added are
from different metric time-series
Add two metric objects for the same time-series.
"""
if self == other:
new_metric = deepcopy(self)
new_metric.metric_values = new_metric.metric_values.append(
other.metric_values, ignore_index=True

# pandas.DataFrame.append was removed in pandas 2.0+
# Use pandas.concat instead
new_metric.metric_values = pandas.concat(
[new_metric.metric_values, other.metric_values],
ignore_index=True,
)

new_metric.metric_values = new_metric.metric_values.dropna()
new_metric.metric_values = (
new_metric.metric_values.drop_duplicates("ds")
.sort_values(by=["ds"])
.reset_index(drop=True)
)

# if oldest_data_datetime is set, trim the dataframe and only keep the newer data
if new_metric.oldest_data_datetime:
if isinstance(new_metric.oldest_data_datetime, datetime.timedelta):
# create a time range mask
mask = new_metric.metric_values["ds"] >= (
new_metric.metric_values.iloc[-1, 0] - abs(new_metric.oldest_data_datetime)
new_metric.metric_values.iloc[-1, 0]
- abs(new_metric.oldest_data_datetime)
)
else:
# create a time range mask
mask = new_metric.metric_values["ds"] >= new_metric.oldest_data_datetime
# truncate the df within the mask

new_metric.metric_values = new_metric.metric_values.loc[mask]

# Update the metric start time and the metric end time for the new Metric
Expand All @@ -176,6 +147,7 @@ def __add__(self, other):
error_string = "Different metric names"
else:
error_string = "Different metric labels"

raise TypeError("Cannot Add different metric types. " + error_string)

def plot(self):
Expand All @@ -184,6 +156,5 @@ def plot(self):
fig, axis = plt.subplots()
axis.plot_date(self.metric_values.ds, self.metric_values.y, linestyle=":")
fig.autofmt_xdate()
# if matplotlib was not imported
else:
raise ImportError("matplotlib was not found")
Loading