mirror of
https://github.com/StefBuwalda/dashboard_test.git
synced 2025-12-18 11:27:52 +00:00
More chart experimentation. Managed to make a chart of ping history display
This commit is contained in:
14
app.py
14
app.py
@@ -7,6 +7,7 @@ from flask_migrate import upgrade, stamp
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from models import service, log
|
from models import service, log
|
||||||
from typing import Any, Optional, cast
|
from typing import Any, Optional, cast
|
||||||
|
import json
|
||||||
|
|
||||||
# Init and upgrade
|
# Init and upgrade
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
@@ -34,12 +35,21 @@ with app.app_context():
|
|||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def homepage():
|
def homepage():
|
||||||
return render_template("home.html", services=services)
|
return render_template("home.html")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/chart")
|
@app.route("/chart")
|
||||||
def chart():
|
def chart():
|
||||||
return render_template("chart.html")
|
with app.app_context():
|
||||||
|
logs = []
|
||||||
|
s = db.session.query(service).first()
|
||||||
|
if s:
|
||||||
|
logs: list[log] = s.logs.limit(60).all()
|
||||||
|
return render_template(
|
||||||
|
"chart.html",
|
||||||
|
dates=[item.dateCreated.isoformat() for item in logs],
|
||||||
|
values=json.dumps([item.ping for item in logs]),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/status")
|
@app.route("/api/status")
|
||||||
|
|||||||
@@ -1,45 +1,48 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en" data-bs-theme="dark">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" charset=" UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Chart</title>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
|
<title>Simple Line Chart</title>
|
||||||
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
<link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
|
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns"></script>
|
||||||
|
<script src="https://www.chartjs.org/docs/latest/samples/utils.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<div class="container-fluid" style="width:900px;">
|
<body>
|
||||||
<canvas id="myChart"></canvas>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
<canvas id="myChart"></canvas>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const ctx = document.getElementById('myChart');
|
|
||||||
|
|
||||||
new Chart(ctx, {
|
const chartDates = {{ dates| tojson | safe }}.map(dt => new Date(dt));
|
||||||
type: 'bar',
|
|
||||||
data: {
|
const data = {
|
||||||
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
|
labels: chartDates,
|
||||||
datasets: [{
|
datasets: [{
|
||||||
label: '# of Votes',
|
label: 'Example Data',
|
||||||
data: [12, 19, 3, 5, 2, 3],
|
data: {{ values }},
|
||||||
borderWidth: 1
|
|
||||||
}]
|
}]
|
||||||
},
|
};
|
||||||
|
|
||||||
|
const ctx = document.getElementById('myChart').getContext('2d');
|
||||||
|
new Chart(ctx, {
|
||||||
|
type: 'line',
|
||||||
|
data: data,
|
||||||
options: {
|
options: {
|
||||||
scales: {
|
scales: {
|
||||||
y: {
|
x: {
|
||||||
beginAtZero: true
|
type: 'time', // Important for datetime axis
|
||||||
|
time: {
|
||||||
|
unit: 'day'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
|
|
||||||
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
|
|
||||||
crossorigin="anonymous"></script>
|
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user