|
1 | | -from flask import Blueprint, Response |
| 1 | +from flask import Blueprint, Response, request, jsonify, render_template, flash, redirect, url_for |
2 | 2 | from prometheus_client import generate_latest |
3 | | -from src.config import app |
| 3 | + |
| 4 | +from src.config import app, db |
| 5 | +from src.models import ExternalMonitornig |
| 6 | + |
4 | 7 |
|
5 | 8 | # Define the Prometheus Blueprint |
6 | 9 | prometheus_bp = Blueprint('prometheus', __name__) |
7 | 10 |
|
8 | 11 | # Define a route to serve Prometheus metrics |
9 | 12 | @app.route('/metrics') |
10 | 13 | def metrics(): |
11 | | - return Response(generate_latest(), mimetype='text/plain') |
| 14 | + output = generate_latest() |
| 15 | + output = '\n'.join([line for line in output.decode().split('\n') if not line.startswith('#') and line]) |
| 16 | + return Response(output, mimetype='text/plain') |
| 17 | + |
| 18 | +# post request to add file path |
| 19 | +@app.route('/prometheus/add_file_path', methods=['GET', 'POST']) |
| 20 | +def add_file_path(): |
| 21 | + if request.method == 'POST': |
| 22 | + |
| 23 | + file_path = request.form.get('file_path') |
| 24 | + # save into the ExternalMonitornig table |
| 25 | + new_task = ExternalMonitornig(file_path=file_path) |
| 26 | + # commit the changes |
| 27 | + db.session.add(new_task) |
| 28 | + db.session.commit() |
| 29 | + |
| 30 | + # read_file_and_update_metric(file_path=file_path) |
| 31 | + return redirect(url_for('add_file_path')) |
| 32 | + |
| 33 | + data = ExternalMonitornig.query.all() |
| 34 | + return render_template('prometheus/add_file_path.html', data=data) |
| 35 | + |
| 36 | + |
| 37 | +# post request to delete file path |
| 38 | +@app.route('/prometheus/delete_file_path/<int:id>', methods=['POST']) |
| 39 | +def delete_file_path(id): |
| 40 | + file_path = ExternalMonitornig.query.get_or_404(id) |
| 41 | + db.session.delete(file_path) |
| 42 | + db.session.commit() |
| 43 | + flash('File path deleted successfully!', 'success') |
| 44 | + return redirect(url_for('add_file_path')) |
| 45 | + |
0 commit comments