system-monitor/web/main.py

97 строки
2.5 KiB
Python
Исходник Обычный вид История

import asyncio
from fastapi import FastAPI, Request, Header, Body, Path
from fastapi.responses import Response, HTMLResponse, FileResponse, RedirectResponse, JSONResponse
from fastapi.exceptions import HTTPException
from fastapi.staticfiles import StaticFiles
from jinja2 import Environment, FileSystemLoader
import os
import database
import models
# from models import MailSizeData
app = FastAPI()
app.mount(
path='/static',
app=StaticFiles(
directory=os.path.join(
os.getcwd(),
'static',
),
),
)
env = Environment(
loader=FileSystemLoader('./templates'),
enable_async=True,
)
@app.get('/')
async def function():
return RedirectResponse(
url='/reports/mainboard',
status_code=307,
)
@app.get('/reports/mainboard')
async def function():
template = env.get_template('reports.jinja2')
return HTMLResponse(
content=await template.render_async(),
status_code=200,
)
@app.get('/settings')
async def function():
hardware_types = dict()
sensors = database.get_sensors()
# hardware_types = set(
# sensor.hardware.hardware_type
# for sensor in sensors
# )
for hardware_type in set(
sensor.hardware.hardware_type
for sensor in sensors
):
hardware_types[hardware_type] = dict()
for hardware in set(
sensor.hardware
for sensor in sensors
if sensor.hardware.hardware_type == hardware_type
):
hardware_types[hardware_type][hardware] = dict()
for sensor_type in set(
sensor.sensor_type
for sensor in sensors
if sensor.hardware == hardware
):
hardware_types[hardware_type][hardware][sensor_type] = list()
for sensor in sensors:
if sensor.hardware == hardware and sensor.sensor_type == sensor_type:
hardware_types[hardware_type][hardware][sensor_type].append(sensor)
template = env.get_template('settings.jinja2')
return HTMLResponse(
content=await template.render_async(
settings={
'hostname': 'localhost',
'port': 8000,
'hardware_types': hardware_types,
},
),
status_code=200,
)
@app.get('/api/sensors')
async def function():
return database.get_sensors()
@app.get('/api/sensor-values')
async def function():
return database.get_sensor_values()