Skip to content

Commit e9c4b1d

Browse files
committed
allow TimestampAndValuePair and PrometheusData to be json serializable
1 parent 52881da commit e9c4b1d

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

system_status/server.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class TimestampAndValuePair:
2020
timestamp: datetime.datetime
2121
value: str
2222

23+
def to_dict(self):
24+
return {
25+
"timestamp": self.timestamp.isoformat(),
26+
"value": self.value
27+
}
28+
2329

2430
@dataclass
2531
class PrometheusData:
@@ -28,6 +34,13 @@ class PrometheusData:
2834
is_up: bool
2935
values: List[TimestampAndValuePair]
3036

37+
def to_dict(self):
38+
return {
39+
"instance": self.instance,
40+
"job": self.job,
41+
"is_up": self.is_up,
42+
"values": [v.to_dict() for v in self.values]
43+
}
3144

3245
app = FastAPI()
3346

@@ -139,7 +152,7 @@ def page_generator(request: Request):
139152
fetch_time = local_datetime.strftime("%Y-%m-%d %H:%M:%S")
140153
data = get_prometheus_data()
141154
if "json" in request.query_params:
142-
return JSONResponse(content=data)
155+
return JSONResponse(content=[d.to_dict() for d in data])
143156

144157
return templates.TemplateResponse(
145158
"my_template.html", {"request": request, "data": data, "fetch_time": fetch_time}

0 commit comments

Comments
 (0)