diff --git a/.coafile b/.coafile new file mode 100644 index 0000000..4967acf --- /dev/null +++ b/.coafile @@ -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 diff --git a/.zuul.yaml b/.zuul.yaml index d848f56..589317a 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -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" diff --git a/app.py b/app.py deleted file mode 100644 index 6875f7e..0000000 --- a/app.py +++ /dev/null @@ -1,11 +0,0 @@ -# Example Usage -import prometheus_connect -url="www.example-prom-url.com" -token='Auth_token' - -prom = prometheus_connect.PrometheusConnect(url=url, token=token) - -labels = {'pod':'prometheus-k8s-0', '_id': 'f716a8c8-5d4e-417a-a95c-742c2bbcd3d2'} -metric_data = (prom.get_metric_range_data(metric_name='up', start_time='20m', label_config=labels, chunk_size='10m', store_locally=True)) - -prom.pretty_print_metric(metric_data) diff --git a/prometheus_api_client/__init__.py b/prometheus_api_client/__init__.py index f1506a5..8d34e20 100644 --- a/prometheus_api_client/__init__.py +++ b/prometheus_api_client/__init__.py @@ -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" diff --git a/prometheus_api_client/metric.py b/prometheus_api_client/metric.py index 6a91dde..5434b31 100644 --- a/prometheus_api_client/metric.py +++ b/prometheus_api_client/metric.py @@ -1,6 +1,4 @@ -""" -A Class for metric object -""" +"""A Class for metric object.""" from copy import deepcopy import datetime import pandas @@ -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 \ @@ -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)) ): @@ -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) @@ -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)`` @@ -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: @@ -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=":") diff --git a/prometheus_api_client/metrics_list.py b/prometheus_api_client/metrics_list.py index c1f49dc..7cd2a4a 100644 --- a/prometheus_api_client/metrics_list.py +++ b/prometheus_api_client/metrics_list.py @@ -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 diff --git a/prometheus_api_client/prometheus_connect.py b/prometheus_api_client/prometheus_connect.py index 6988196..29ab3bf 100644 --- a/prometheus_api_client/prometheus_connect.py +++ b/prometheus_api_client/prometheus_connect.py @@ -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 @@ -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 @@ -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 @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 \ diff --git a/prometheus_api_client/utils.py b/prometheus_api_client/utils.py index 526214b..8c691f6 100644 --- a/prometheus_api_client/utils.py +++ b/prometheus_api_client/utils.py @@ -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) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..8980a4c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +"""init unit tests.""" diff --git a/tests/test_metric.py b/tests/test_metric.py index f593188..3a7debd 100644 --- a/tests/test_metric.py +++ b/tests/test_metric.py @@ -1,3 +1,4 @@ +"""unit tests for Metrics Class.""" import unittest import json import os @@ -6,6 +7,8 @@ class TestMetric(unittest.TestCase): + """unit tests for Metrics Class.""" + def setUp(self): """ read metrics stored as jsons in './tests/metrics' @@ -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,