Skip to content
Draft
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased][]

### Fixed

- Avoid the deprecated `datetime.utcfromtimestamp()` call when formatting
Prometheus chart labels.

[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-reporting/compare/0.18.0...HEAD

## [0.18.0][] - 2024-12-02
Expand Down Expand Up @@ -270,4 +275,4 @@

### Added

- Initial release
- Initial release
8 changes: 5 additions & 3 deletions chaosreport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import subprocess
import tempfile
from base64 import b64encode
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from importlib.metadata import version, PackageNotFoundError
from typing import Any, Dict, List

Expand Down Expand Up @@ -470,9 +470,11 @@ def generate_chart_from_prometheus(run: Run, export_format: str):
# now we have our range of abscissa, let's map those
# timestamps to formatted strings
x = sorted(list(x))
fromts = datetime.utcfromtimestamp
chart.x_labels = [
fromts(v).strftime("%Y-%m-%d\n %H:%M:%S") for v in x
datetime.fromtimestamp(v, timezone.utc).strftime(
"%Y-%m-%d\n %H:%M:%S"
)
for v in x
]
chart.x_labels_major = chart.x_labels[::10]
chart.title = "Query - {}".format(
Expand Down
54 changes: 54 additions & 0 deletions tests/test_prometheus_charts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import chaosreport


class LineChart:
instances = []

def __init__(self, **kwargs):
self.x_labels = []
self.x_labels_major = []
self.series = []
self.title = None
LineChart.instances.append(self)

def add(self, label, values, allow_interruptions=False):
self.series.append((label, values, allow_interruptions))

def render(self, **kwargs):
return b"<svg />"


def test_generate_chart_from_prometheus_formats_labels_in_utc(monkeypatch):
LineChart.instances = []
monkeypatch.setattr(chaosreport.pygal, "Line", LineChart)

run = {
"activity": {
"provider": {
"arguments": {"query": "up"},
},
},
"output": {
"data": {
"resultType": "matrix",
"result": [
{
"metric": {"pod": "frontend"},
"values": [
(0, "1"),
(3600, "2"),
],
}
],
},
},
}

chaosreport.generate_chart_from_prometheus(run, "html")

chart = LineChart.instances[0]
assert chart.x_labels == [
"1970-01-01\n 00:00:00",
"1970-01-01\n 01:00:00",
]
assert chart.x_labels_major == ["1970-01-01\n 00:00:00"]