@@ -109,7 +109,6 @@ def get_app_data(request):
109109 )
110110
111111
112-
113112# default mx data points for prometheus
114113MAX_DATA_POINTS = 11000
115114STEP_UNITS_IN_SECONDS = {
@@ -120,16 +119,19 @@ def get_app_data(request):
120119 "w" : 604800 ,
121120}
122121
122+
123123def parse_step_to_seconds (step : str ) -> int :
124124 """
125125 Parses a Prometheus step string like '1m', '2h', '4d' into seconds.
126126 """
127127 match = re .match (r"^(\d+)([smhdw])$" , step )
128128 if not match :
129- raise ValueError ("Invalid step format. Use formats like 30s, 5m, 2h, 1d." )
129+ raise ValueError (
130+ "Invalid step format. Use formats like 30s, 5m, 2h, 1d." )
130131 value , unit = match .groups ()
131132 return int (value ) * STEP_UNITS_IN_SECONDS [unit ]
132133
134+
133135def is_valid_prometheus_query (step : str , start_ts : int , end_ts : int ) -> (bool , str ):
134136 """
135137 Validates if the number of points in a Prometheus query is within the allowed range.
@@ -138,15 +140,14 @@ def is_valid_prometheus_query(step: str, start_ts: int, end_ts: int) -> (bool, s
138140 step_seconds = parse_step_to_seconds (step )
139141 except ValueError as e :
140142 return False , str (e )
141-
143+
142144 if start_ts >= end_ts :
143145 return False , "Start timestamp must be less than end timestamp."
144-
146+
145147 total_duration = end_ts - start_ts
146148 num_points = total_duration // step_seconds
147149
148150 if num_points > MAX_DATA_POINTS :
149151 return False , f"Query returns { num_points } points, which exceeds the limit of { MAX_DATA_POINTS } . Increase the step or reduce the time range."
150-
151- return True , f"Query valid: { num_points } data points."
152152
153+ return True , f"Query valid: { num_points } data points."
0 commit comments