From 7336fcec55e4ae11e181f0ed210db0c86f0d71f6 Mon Sep 17 00:00:00 2001 From: Oriol Tauleria Date: Sun, 17 Nov 2019 17:28:12 +0100 Subject: [PATCH] Repair tests (#185) * Update deprecated convert to converter * Solve deprecated use of yAxis tuple * Change deprecated imp to importlib * Change Deprecated assoc to evolve * Change Deprecated cmp to eq and sort --- README.rst | 4 +- docs/example-elasticsearch.dashboard.py | 7 +- docs/example.dashboard.py | 144 ++++++++++++------------ grafanalib/_gen.py | 4 +- grafanalib/core.py | 8 +- grafanalib/tests/test_grafanalib.py | 8 +- grafanalib/tests/test_opentsdb.py | 4 +- grafanalib/tests/test_validators.py | 4 +- grafanalib/tests/test_zabbix.py | 4 +- grafanalib/weave.py | 18 +-- 10 files changed, 104 insertions(+), 101 deletions(-) diff --git a/README.rst b/README.rst index 8d9d680..027daa8 100644 --- a/README.rst +++ b/README.rst @@ -57,10 +57,10 @@ percentile latency: refId='E', ), ], - yAxes=[ + yAxes=G.YAxes( YAxis(format=OPS_FORMAT), YAxis(format=SHORT_FORMAT), - ], + ), alert=Alert( name="Too many 500s on Nginx", message="More than 5 QPS of 500s on Nginx for 5 minutes", diff --git a/docs/example-elasticsearch.dashboard.py b/docs/example-elasticsearch.dashboard.py index 1615c98..8c72cde 100644 --- a/docs/example-elasticsearch.dashboard.py +++ b/docs/example-elasticsearch.dashboard.py @@ -38,7 +38,8 @@ g = Graph( dataSource="elasticsearch", targets=tgts, lines=False, - legend=Legend(alignAsTable=True, rightSide=True, total=True, current=True, max=True), + legend=Legend(alignAsTable=True, rightSide=True, + total=True, current=True, max=True), lineWidth=1, nullPointMode=NULL_AS_NULL, seriesOverrides=[ @@ -68,7 +69,7 @@ g = Graph( "color": "#447EBC" }, ], - yAxes=[ + yAxes=G.YAxes( YAxis( label="Count", format=SHORT_FORMAT, @@ -79,7 +80,7 @@ g = Graph( format=SECONDS_FORMAT, decimals=2 ), - ], + ), transparent=True, span=12, ) diff --git a/docs/example.dashboard.py b/docs/example.dashboard.py index 2142156..da25ef5 100644 --- a/docs/example.dashboard.py +++ b/docs/example.dashboard.py @@ -2,78 +2,78 @@ from grafanalib.core import * dashboard = Dashboard( - title="Frontend Stats", - rows=[ - Row(panels=[ - Graph( - title="Frontend QPS", - dataSource='My Prometheus', - targets=[ - Target( - expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"1.."}[1m]))', - legendFormat="1xx", - refId='A', - ), - Target( - expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"2.."}[1m]))', - legendFormat="2xx", - refId='B', - ), - Target( - expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"3.."}[1m]))', - legendFormat="3xx", - refId='C', - ), - Target( - expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"4.."}[1m]))', - legendFormat="4xx", - refId='D', - ), - Target( - expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))', - legendFormat="5xx", - refId='E', - ), - ], - yAxes=[ - YAxis(format=OPS_FORMAT), - YAxis(format=SHORT_FORMAT), - ], - alert=Alert( - name="Too many 500s on Nginx", - message="More than 5 QPS of 500s on Nginx for 5 minutes", - alertConditions=[ - AlertCondition( - Target( - expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))', - legendFormat="5xx", - refId='A', - ), - timeRange=TimeRange("5m", "now"), - evaluator=GreaterThan(5), - operator=OP_AND, - reducerType=RTYPE_SUM, + title="Frontend Stats", + rows=[ + Row(panels=[ + Graph( + title="Frontend QPS", + dataSource='My Prometheus', + targets=[ + Target( + expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"1.."}[1m]))', + legendFormat="1xx", + refId='A', + ), + Target( + expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"2.."}[1m]))', + legendFormat="2xx", + refId='B', + ), + Target( + expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"3.."}[1m]))', + legendFormat="3xx", + refId='C', + ), + Target( + expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"4.."}[1m]))', + legendFormat="4xx", + refId='D', + ), + Target( + expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))', + legendFormat="5xx", + refId='E', + ), + ], + yAxes=G.YAxes( + YAxis(format=OPS_FORMAT), + YAxis(format=SHORT_FORMAT), ), - ], - ) - ), - Graph( - title="Frontend latency", - dataSource='My Prometheus', - targets=[ - Target( - expr='histogram_quantile(0.5, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))', - legendFormat="0.5 quantile", - refId='A', + alert=Alert( + name="Too many 500s on Nginx", + message="More than 5 QPS of 500s on Nginx for 5 minutes", + alertConditions=[ + AlertCondition( + Target( + expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))', + legendFormat="5xx", + refId='A', + ), + timeRange=TimeRange("5m", "now"), + evaluator=GreaterThan(5), + operator=OP_AND, + reducerType=RTYPE_SUM, + ), + ], + ) + ), + Graph( + title="Frontend latency", + dataSource='My Prometheus', + targets=[ + Target( + expr='histogram_quantile(0.5, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))', + legendFormat="0.5 quantile", + refId='A', + ), + Target( + expr='histogram_quantile(0.99, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))', + legendFormat="0.99 quantile", + refId='B', + ), + ], + yAxes=single_y_axis(format=SECONDS_FORMAT), ), - Target( - expr='histogram_quantile(0.99, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))', - legendFormat="0.99 quantile", - refId='B', - ), - ], - yAxes=single_y_axis(format=SECONDS_FORMAT), - ), - ]), - ], + ]), + ], ).auto_panel_ids() diff --git a/grafanalib/_gen.py b/grafanalib/_gen.py index fdfff74..6873054 100644 --- a/grafanalib/_gen.py +++ b/grafanalib/_gen.py @@ -1,7 +1,7 @@ """Generate JSON Grafana dashboards.""" import argparse -import imp +import importlib import json import os import sys @@ -21,7 +21,7 @@ def load_dashboard(path): ``dashboard``. :return: A ``Dashboard`` """ - module = imp.load_source("dashboard", path) + module = importlib.load_source("dashboard", path) marker = object() dashboard = getattr(module, 'dashboard', marker) if dashboard is marker: diff --git a/grafanalib/core.py b/grafanalib/core.py index 0533229..4fa1e36 100644 --- a/grafanalib/core.py +++ b/grafanalib/core.py @@ -455,7 +455,7 @@ def _balance_panels(panels): auto_span = math.ceil( (TOTAL_SPAN - allotted_spans) / (len(no_span_set) or 1)) return [ - attr.assoc(panel, span=auto_span) if panel.span is None else panel + attr.evolve(panel, span=auto_span) if panel.span is None else panel for panel in panels ] @@ -483,7 +483,7 @@ class Row(object): return iter(self.panels) def _map_panels(self, f): - return attr.assoc(self, panels=list(map(f, self.panels))) + return attr.evolve(self, panels=list(map(f, self.panels))) def to_json_data(self): showTitle = False @@ -915,7 +915,7 @@ class Dashboard(object): yield panel def _map_panels(self, f): - return attr.assoc(self, rows=[r._map_panels(f) for r in self.rows]) + return attr.evolve(self, rows=[r._map_panels(f) for r in self.rows]) def auto_panel_ids(self): """Give unique IDs all the panels without IDs. @@ -929,7 +929,7 @@ class Dashboard(object): auto_ids = (i for i in itertools.count(1) if i not in ids) def set_id(panel): - return panel if panel.id else attr.assoc(panel, id=next(auto_ids)) + return panel if panel.id else attr.evolve(panel, id=next(auto_ids)) return self._map_panels(set_id) def to_json_data(self): diff --git a/grafanalib/tests/test_grafanalib.py b/grafanalib/tests/test_grafanalib.py index 51bee96..9192a83 100644 --- a/grafanalib/tests/test_grafanalib.py +++ b/grafanalib/tests/test_grafanalib.py @@ -25,10 +25,10 @@ def test_serialization(): ), ], id=1, - yAxes=[ + yAxes=G.YAxes( G.YAxis(format=G.SHORT_FORMAT, label="CPU seconds / second"), G.YAxis(format=G.SHORT_FORMAT), - ], + ), ) stream = StringIO() _gen.write_dashboard(graph, stream) @@ -51,10 +51,10 @@ def test_auto_id(): refId='A', ), ], - yAxes=[ + yAxes=G.YAxes( G.YAxis(format=G.SHORT_FORMAT, label="CPU seconds"), G.YAxis(format=G.SHORT_FORMAT), - ], + ), ) ]), ], diff --git a/grafanalib/tests/test_opentsdb.py b/grafanalib/tests/test_opentsdb.py index f7644a6..7a1db2a 100644 --- a/grafanalib/tests/test_opentsdb.py +++ b/grafanalib/tests/test_opentsdb.py @@ -29,10 +29,10 @@ def test_serialization_opentsdb_target(): ]), ], id=1, - yAxes=[ + yAxes=G.YAxes( G.YAxis(format=G.SHORT_FORMAT, label="CPU seconds / second"), G.YAxis(format=G.SHORT_FORMAT), - ], + ), ) stream = StringIO() _gen.write_dashboard(graph, stream) diff --git a/grafanalib/tests/test_validators.py b/grafanalib/tests/test_validators.py index af4f404..254a049 100644 --- a/grafanalib/tests/test_validators.py +++ b/grafanalib/tests/test_validators.py @@ -10,7 +10,9 @@ def create_attribute(): default=None, validator=None, repr=True, - cmp=True, + cmp=None, + eq=True, + order=False, hash=True, init=True) diff --git a/grafanalib/tests/test_zabbix.py b/grafanalib/tests/test_zabbix.py index fb7ebee..5874bd8 100644 --- a/grafanalib/tests/test_zabbix.py +++ b/grafanalib/tests/test_zabbix.py @@ -27,10 +27,10 @@ def test_serialization_zabbix_target(): ]), ], id=1, - yAxes=[ + yAxes=G.YAxes( G.YAxis(format=G.SHORT_FORMAT, label="CPU seconds / second"), G.YAxis(format=G.SHORT_FORMAT), - ], + ), ) stream = StringIO() _gen.write_dashboard(graph, stream) diff --git a/grafanalib/weave.py b/grafanalib/weave.py index 24ca755..bb5ec8a 100644 --- a/grafanalib/weave.py +++ b/grafanalib/weave.py @@ -17,13 +17,13 @@ ORANGE = "#EF843C" RED = "#E24D42" ALIAS_COLORS = { - "1xx": YELLOW, - "2xx": GREEN, - "3xx": BLUE, - "4xx": ORANGE, - "5xx": RED, - "success": GREEN, - "error": RED, + "1xx": YELLOW, + "2xx": GREEN, + "3xx": BLUE, + "4xx": ORANGE, + "5xx": RED, + "success": GREEN, + "error": RED, } @@ -46,10 +46,10 @@ def QPSGraph(data_source, title, expressions, **kwargs): title=title, expressions=exprs, aliasColors=ALIAS_COLORS, - yAxes=[ + yAxes=G.YAxes( G.YAxis(format=G.OPS_FORMAT), G.YAxis(format=G.SHORT_FORMAT), - ], + ), **kwargs ))