File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
2531class 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
3245app = 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 }
You can’t perform that action at this time.
0 commit comments