Skip to content

Commit 2d68173

Browse files
chore: api end point added for multiple target
1 parent 7374dc6 commit 2d68173

1 file changed

Lines changed: 99 additions & 1 deletion

File tree

src/routes/api.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def graph_data_api():
234234
# # Handle and log the error for debugging purposes
235235
# return jsonify({'error': 'An error occurred while fetching the graph data', 'details': str(e)}), 500
236236

237-
@app.route('/api/v3/prometheus/graphs_data', methods=['GET'])
237+
@app.route('/api/v1/prometheus/graphs_data', methods=['GET'])
238238
@login_required
239239
def graph_data_api_v3():
240240
try:
@@ -319,6 +319,104 @@ def graph_data_api_v3():
319319
# Handle and log the error for debugging purposes
320320
return jsonify({'error': 'An error occurred while fetching the graph data', 'details': str(e)}), 500
321321

322+
@app.route('/api/v1/prometheus/graphs_data/targets', methods=['GET'])
323+
@login_required
324+
def graph_data_api_v3_():
325+
try:
326+
current_time = datetime.now()
327+
328+
# Get the time filter from query parameters
329+
time_filter = request.args.get('filter', default='1 day')
330+
331+
# Determine the start time based on the filter
332+
time_deltas = {
333+
'5 minutes': 5 * 60,
334+
'15 minutes': 15 * 60,
335+
'30 minutes': 30 * 60,
336+
'1 hour': 60 * 60,
337+
'3 hours': 3 * 60 * 60,
338+
'6 hours': 6 * 60 * 60,
339+
'12 hours': 12 * 60 * 60,
340+
'1 day': 24 * 60 * 60,
341+
'2 days': 2 * 24 * 60 * 60,
342+
'3 days': 3 * 24 * 60 * 60,
343+
'1 week': 7 * 24 * 60 * 60,
344+
'1 month': 30 * 24 * 60 * 60,
345+
'3 months': 90 * 24 * 60 * 60,
346+
}
347+
348+
# Get the time range in seconds
349+
time_range_seconds = time_deltas.get(time_filter, 24 * 60 * 60)
350+
351+
# Prepare time parameters for the Prometheus query
352+
end_time = int(current_time.timestamp())
353+
start_time = end_time - time_range_seconds
354+
step = '10s'
355+
356+
# Initialize lists for the data
357+
time_data = []
358+
metric_data = {}
359+
360+
# Fetch data for each metric from Prometheus
361+
for metric, prometheus_query in PROMETHEUS_METRICS.items():
362+
# Prepare Prometheus API query parameters
363+
params = {
364+
'query': prometheus_query,
365+
'start': start_time,
366+
'end': end_time,
367+
'step': step
368+
}
369+
370+
# Send the query to Prometheus
371+
response = requests.get(PROMETHEUS_URL, params=params)
372+
373+
# Check if the request was successful
374+
if response.status_code == 200:
375+
result = response.json().get('data', {}).get('result', [])
376+
377+
if result:
378+
# Initialize a dictionary to hold time series data for this metric
379+
metric_data[metric] = []
380+
381+
for series in result:
382+
# Create a new list for the time series data of this particular series
383+
series_data = {
384+
"metric": series.get("metric"),
385+
"values": []
386+
}
387+
388+
# Iterate over the values for this series
389+
for value in series.get("values", []):
390+
timestamp = datetime.fromtimestamp(float(value[0]), tz=timezone.utc).isoformat()
391+
if timestamp not in time_data:
392+
time_data.append(timestamp)
393+
series_data["values"].append(value[1])
394+
395+
# Append the series data to the metric
396+
metric_data[metric].append(series_data)
397+
else:
398+
print(f"No data for metric: {metric}")
399+
else:
400+
raise Exception(f"Failed to fetch data for {metric} from Prometheus: {response.text}")
401+
402+
# Ensure all metric data has the same length as time_data
403+
for metric, series_list in metric_data.items():
404+
for series in series_list:
405+
while len(series["values"]) < len(time_data):
406+
series["values"].append(None)
407+
408+
# Return the data as JSON
409+
response_data = {
410+
"time": time_data,
411+
**metric_data,
412+
"current_time": current_time
413+
}
414+
415+
return jsonify(response_data), 200
416+
417+
except Exception as e:
418+
# Handle and log the error for debugging purposes
419+
return jsonify({'error': 'An error occurred while fetching the graph data', 'details': str(e)}), 500
322420

323421
@app.route('/api/v1/refresh-interval', methods=['GET', 'POST'])
324422
@login_required

0 commit comments

Comments
 (0)