Skip to content
Merged
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
24 changes: 24 additions & 0 deletions .coafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[all]
bears = LineCountBear, FilenameBear
files = **.py, **.yml, **.rst, **.md
ignore = **/__pycache__/**, **/__pycache__, __pycache__, __pycache__/**, **/*.pyc, *.pyc
max_lines_per_file = 1000
max_line_length = 120

[all.python]
bears = PycodestyleBear, PyDocStyleBear
files = **.py
language = Python
editor = vim
ignore = setup.py, docs/*, tests/*

[all.yaml]
bears = YAMLLintBear
files = **.yaml, **.yml
ignore = .zuul.yaml
max_line_length = 120

[zuul.yaml]
bears = YAMLLintBear
files = .zuul.yaml
max_line_length = 180
9 changes: 8 additions & 1 deletion .zuul.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
---
- project:
check:
jobs:
- "thoth-coala"
- "thoth-pytest"
gate:
queue: "aicoe/prometheus"
jobs:
- "thoth-coala"
- "thoth-pytest"
kebechet-auto-gate:
queue: "aicoe/prometheus"
jobs:
- "thoth-coala"
- "thoth-pytest"
11 changes: 0 additions & 11 deletions app.py

This file was deleted.

2 changes: 1 addition & 1 deletion prometheus_api_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" A collection of tools to collect and manipulate prometheus metrics"""
"""A collection of tools to collect and manipulate prometheus metrics."""

__title__ = "prometheus-connect"
__version__ = "0.0.2b4"
Expand Down
27 changes: 10 additions & 17 deletions prometheus_api_client/metric.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
A Class for metric object
"""
"""A Class for metric object."""
from copy import deepcopy
import datetime
import pandas
Expand All @@ -16,8 +14,8 @@


class Metric:
"""
A Class for `Metric` object
r"""
A Class for `Metric` object.

:param metric: (dict) A metric item from the list of metrics received from prometheus
:param oldest_data_datetime: (datetime|timedelta) Any metric values in the dataframe that are \
Expand Down Expand Up @@ -46,11 +44,7 @@ class Metric:
"""

def __init__(self, metric, oldest_data_datetime=None):
"""
Constructor for the Metric object

"""

"""Constructor for the Metric object."""
if not isinstance(
oldest_data_datetime, (datetime.datetime, datetime.timedelta, type(None))
):
Expand Down Expand Up @@ -86,7 +80,7 @@ def __init__(self, metric, oldest_data_datetime=None):

def __eq__(self, other):
"""
overloading operator ``=``
Overloading operator ``=``.

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

Expand All @@ -106,7 +100,7 @@ def __eq__(self, other):

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

Example Usage:
``metric_1 = Metric(metric_data_1)``
Expand All @@ -121,8 +115,9 @@ def __str__(self):
return "{" + "\n" + name + labels + values + "\n" + "}"

def __add__(self, other):
"""
overloading operator ``+``,
r"""
Overloading operator ``+``.

Add two metric objects for the same time-series

Example Usage:
Expand Down Expand Up @@ -178,9 +173,7 @@ def __add__(self, other):
raise TypeError("Cannot Add different metric types. " + error_string)

def plot(self):
"""
Plot a very simple line graph for the metric time-series
"""
"""Plot a very simple line graph for the metric time-series."""
if _MPL_FOUND:
fig, axis = plt.subplots()
axis.plot_date(self.metric_values.ds, self.metric_values.y, linestyle=":")
Expand Down
1 change: 1 addition & 0 deletions prometheus_api_client/metrics_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MetricsList(list):
"""

def __init__(self, metric_data_list):
"""Class MetricsList constructor."""
# if the input is not a list
if not isinstance(metric_data_list, list):
# make it into a list
Expand Down
30 changes: 12 additions & 18 deletions prometheus_api_client/prometheus_connect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
A Class for collection of metrics from a Prometheus Host.
"""
"""A Class for collection of metrics from a Prometheus Host."""
from urllib.parse import urlparse
import bz2
import os
Expand All @@ -22,7 +20,7 @@

class PrometheusConnect:
"""
A Class for collection of metrics from a Prometheus Host
A Class for collection of metrics from a Prometheus Host.

:param url: (str) url for the prometheus host
:param headers: (dict) A dictionary of http headers to be used to communicate with
Expand All @@ -34,9 +32,7 @@ class PrometheusConnect:
def __init__(
self, url: str = "http://127.0.0.1:9090", headers: dict = None, disable_ssl: bool = False
):
"""
Constructor for the class PrometheusConnect
"""
"""Constructor for the class PrometheusConnect."""
self.headers = headers
self.url = url
self.prometheus_host = urlparse(self.url).netloc
Expand All @@ -46,7 +42,7 @@ def __init__(
@retry(stop_max_attempt_number=MAX_REQUEST_RETRIES, wait_fixed=CONNECTION_RETRY_WAIT_TIME)
def all_metrics(self, params: dict = None):
"""
Get the list of all the metrics that the prometheus host scrapes
Get the list of all the metrics that the prometheus host scrapes.

:param params: (dict) Optional dictionary containing GET parameters to be
sent along with the API request, such as "time"
Expand Down Expand Up @@ -74,9 +70,8 @@ def all_metrics(self, params: dict = None):
def get_current_metric_value(
self, metric_name: str, label_config: dict = None, params: dict = None
):
"""
A method to get the current metric value for the specified metric
and label configuration.
r"""
A method to get the current metric value for the specified metric and label configuration.

:param metric_name: (str) The name of the metric
:param label_config: (dict) A dictionary that specifies metric labels and their
Expand Down Expand Up @@ -128,9 +123,8 @@ def get_metric_range_data(
store_locally: bool = False,
params: dict = None,
):
"""
A method to get the current metric value for the specified metric
and label configuration.
r"""
A method to get the current metric value for the specified metric and label configuration.

:param metric_name: (str) The name of the metric.
:param label_config: (dict) A dictionary specifying metric labels and their
Expand Down Expand Up @@ -212,8 +206,8 @@ def get_metric_range_data(
return data

def _store_metric_values_local(self, metric_name, values, end_timestamp, compressed=False):
"""
Store metrics on the local filesystem, optionally with bz2 compression
r"""
Store metrics on the local filesystem, optionally with bz2 compression.

:param metric_name: (str) the name of the metric being saved
:param values: (str) metric data in JSON string format
Expand Down Expand Up @@ -241,8 +235,8 @@ def _store_metric_values_local(self, metric_name, values, end_timestamp, compres
return file_path

def _metric_filename(self, metric_name: str, end_timestamp: int):
"""
Adds a timestamp to the filename before it is stored
r"""
Add a timestamp to the filename before it is stored.

:param metric_name: (str) the name of the metric being saved
:param end_timestamp: (int) timestamp in any format understood by \
Expand Down
14 changes: 3 additions & 11 deletions prometheus_api_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
"""
Some helpful functions used in the API
"""
"""Some helpful functions used in the API."""
import json
import dateparser


def parse_datetime(date_string: str, settings: dict = None):
"""
A wrapper for dateparser.parse, but the default settings are set
to {"DATE_ORDER": "YMD"}
"""

"""A wrapper for dateparser.parse, but the default settings are set to {"DATE_ORDER": "YMD"}."""
settings = settings or {"DATE_ORDER": "YMD"}
return dateparser.parse(str(date_string), settings=settings)


def parse_timedelta(time_a: str = "now", time_b: str = "1d"):
"""
returns timedelta for time_a - time_b
"""
"""Return timedelta for time_a - time_b."""
return parse_datetime(time_a) - parse_datetime(time_b)


Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""init unit tests."""
4 changes: 4 additions & 0 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""unit tests for Metrics Class."""
import unittest
import json
import os
Expand All @@ -6,6 +7,8 @@


class TestMetric(unittest.TestCase):
"""unit tests for Metrics Class."""

def setUp(self):
"""
read metrics stored as jsons in './tests/metrics'
Expand Down Expand Up @@ -63,6 +66,7 @@ def test_metric_addition(self):
_ = Metric(self.raw_metrics_list[0][0]) + Metric(self.raw_metrics_list[0][1])

sum_metric = Metric(self.raw_metrics_list[0][0]) + Metric(self.raw_metrics_list[1][0])
print(sum_metric)
self.assertIsInstance(sum_metric, Metric, msg="The sum is not a Metric")
self.assertEqual(
sum_metric.start_time,
Expand Down