Skip to content

Commit 5aef69d

Browse files
web page improved
1 parent a0def2a commit 5aef69d

4 files changed

Lines changed: 112 additions & 42 deletions

File tree

src/routes/prometheus.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
from flask import Blueprint, Response, request, jsonify, render_template, flash, redirect, url_for
1+
from flask import Blueprint, Response, request, render_template, flash, redirect, url_for
22
from prometheus_client import generate_latest
3+
import os
34

45
from src.config import app, db
56
from src.models import ExternalMonitornig
67

7-
88
# Define the Prometheus Blueprint
99
prometheus_bp = Blueprint('prometheus', __name__)
1010

11+
12+
def is_valid_file(file_path: str) -> bool:
13+
"""Checks if a file is valid and have key-value pairs separated by a colon."""
14+
with open(file_path, 'r') as file:
15+
for line in file:
16+
if not line.strip():
17+
continue
18+
19+
if ':' not in line:
20+
return False
21+
22+
return True
23+
1124
# Define a route to serve Prometheus metrics
1225
@app.route('/metrics')
1326
def metrics():
@@ -16,22 +29,32 @@ def metrics():
1629
return Response(output, mimetype='text/plain')
1730

1831
# post request to add file path
19-
@app.route('/prometheus/add_file_path', methods=['GET', 'POST'])
20-
def add_file_path():
32+
@app.route('/prometheus/external_monitoring', methods=['GET', 'POST'])
33+
def external_monitoring():
2134
if request.method == 'POST':
2235

2336
file_path = request.form.get('file_path')
37+
38+
if not os.path.exists(file_path):
39+
flash('File path does not exist', 'danger')
40+
return redirect(url_for('external_monitoring'))
41+
42+
# check file path and is_valid
43+
if not is_valid_file(file_path):
44+
flash('Invalid file format. File should have key-value pairs separated by a colon.', 'danger')
45+
return redirect(url_for('external_monitoring'))
46+
2447
# save into the ExternalMonitornig table
2548
new_task = ExternalMonitornig(file_path=file_path)
2649
# commit the changes
2750
db.session.add(new_task)
2851
db.session.commit()
2952

3053
# read_file_and_update_metric(file_path=file_path)
31-
return redirect(url_for('add_file_path'))
54+
return redirect(url_for('external_monitoring'))
3255

3356
data = ExternalMonitornig.query.all()
34-
return render_template('prometheus/add_file_path.html', data=data)
57+
return render_template('prometheus/external_monitoring.html', data=data)
3558

3659

3760
# post request to delete file path
@@ -41,5 +64,5 @@ def delete_file_path(id):
4164
db.session.delete(file_path)
4265
db.session.commit()
4366
flash('File path deleted successfully!', 'success')
44-
return redirect(url_for('add_file_path'))
67+
return redirect(url_for('external_monitoring'))
4568

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
body {
2+
background-color: #f8f9fa;
3+
}
4+
5+
.container {
6+
max-width: 600px;
7+
margin: auto;
8+
}
9+
10+
.card {
11+
border: 1px solid #ced4da;
12+
border-radius: 0.5rem;
13+
}
14+
15+
.card-title {
16+
font-weight: bold;
17+
}
18+
19+
.message {
20+
color: #28a745; /* Success message color */
21+
}
22+
23+
.list-group-item {
24+
background-color: #ffffff;
25+
border: 1px solid #e9ecef;
26+
border-radius: 0.5rem;
27+
}
28+
29+
.list-group-item:hover {
30+
background-color: #f1f1f1;
31+
}
32+
33+
.btn-primary {
34+
background-color: #007bff;
35+
border: none;
36+
}
37+
38+
.btn-danger {
39+
background-color: #dc3545;
40+
border: none;
41+
}

src/templates/prometheus/add_file_path.html

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{% extends "base/base.html" %}
2+
{% block title %}External Monitoring{% endblock %}
3+
{% block extra_head %}
4+
<link rel="stylesheet" href="{{ url_for('static', filename='css/external_monitoring.css') }}">
5+
{% endblock %}
6+
{% block content %}
7+
<body>
8+
<div class="container mt-5">
9+
<h1 class="text-center">External Monitoring</h1>
10+
{% include 'ext/message.html' %}
11+
<div class="card mt-4">
12+
<div class="card-body">
13+
<form method="POST" action="{{ url_for('external_monitoring') }}">
14+
<div class="mb-3">
15+
<label for="file-path" class="form-label">Enter File Path for Monitoring</label>
16+
<input type="text" id="file-path" name="file_path" class="form-control" placeholder="Enter file path" required>
17+
</div>
18+
<button type="submit" class="btn btn-primary">Submit</button>
19+
</form>
20+
<div class="message mt-3" id="message"></div>
21+
</div>
22+
</div>
23+
24+
{% if data %}
25+
<div class="mt-4">
26+
<h5>Monitoring File Paths</h5>
27+
{% for item in data %}
28+
<div class="list-group mb-2">
29+
<div class="list-group-item d-flex justify-content-between align-items-center">
30+
<span>{{ item.file_path }}</span>
31+
<form method="POST" action="{{ url_for('delete_file_path', id=item.id) }}">
32+
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
33+
</form>
34+
</div>
35+
</div>
36+
{% endfor %}
37+
</div>
38+
{% endif %}
39+
</div>
40+
</body>
41+
{% endblock %}

0 commit comments

Comments
 (0)