mirror of
				https://github.com/StefBuwalda/dashboard_test.git
				synced 2025-10-31 11:49:58 +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 models import service, log | ||||
| from typing import Any, Optional, cast | ||||
| import json | ||||
|  | ||||
| # Init and upgrade | ||||
| with app.app_context(): | ||||
| @@ -34,12 +35,21 @@ with app.app_context(): | ||||
|  | ||||
| @app.route("/") | ||||
| def homepage(): | ||||
|     return render_template("home.html", services=services) | ||||
|     return render_template("home.html") | ||||
|  | ||||
|  | ||||
| @app.route("/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") | ||||
|   | ||||
| @@ -1,45 +1,48 @@ | ||||
| <!DOCTYPE html> | ||||
| <html lang="en" data-bs-theme="dark"> | ||||
| <html lang="en"> | ||||
|  | ||||
| <head> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1" charset=" UTF-8"> | ||||
|     <title>Chart</title> | ||||
|     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" | ||||
|         integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous"> | ||||
|     <link rel="icon" type="image/svg+xml" href="/static/favicon.svg"> | ||||
|     <meta charset="UTF-8"> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||||
|     <title>Simple Line Chart</title> | ||||
|     <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||||
|     <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> | ||||
|  | ||||
| <div class="container-fluid" style="width:900px;"> | ||||
|     <canvas id="myChart"></canvas> | ||||
| </div> | ||||
| <body> | ||||
|  | ||||
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||||
|     <canvas id="myChart"></canvas> | ||||
|  | ||||
| </body> | ||||
|  | ||||
| <script> | ||||
|     const ctx = document.getElementById('myChart'); | ||||
|  | ||||
|     const chartDates = {{ dates| tojson | safe }}.map(dt => new Date(dt)); | ||||
|  | ||||
|     const data = { | ||||
|         labels: chartDates, | ||||
|         datasets: [{ | ||||
|             label: 'Example Data', | ||||
|             data: {{ values }}, | ||||
|         }] | ||||
|     }; | ||||
|  | ||||
|     const ctx = document.getElementById('myChart').getContext('2d'); | ||||
|     new Chart(ctx, { | ||||
|         type: 'bar', | ||||
|         data: { | ||||
|             labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], | ||||
|             datasets: [{ | ||||
|                 label: '# of Votes', | ||||
|                 data: [12, 19, 3, 5, 2, 3], | ||||
|                 borderWidth: 1 | ||||
|             }] | ||||
|         }, | ||||
|         type: 'line', | ||||
|         data: data, | ||||
|         options: { | ||||
|             scales: { | ||||
|                 y: { | ||||
|                     beginAtZero: true | ||||
|                 x: { | ||||
|                     type: 'time', // Important for datetime axis | ||||
|                     time: { | ||||
|                         unit: 'day' | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     }); | ||||
| </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> | ||||
		Reference in New Issue
	
	Block a user