Разработаны модули извлечения и сохранения статистических данных, разработан набросок веб-приложения

This commit is contained in:
2024-07-26 02:45:25 +03:00
commit 9ba660f803
14 changed files with 977 additions and 0 deletions

1
web/__init__.py Normal file
View File

@ -0,0 +1 @@
from .main import app

96
web/main.py Normal file
View File

@ -0,0 +1,96 @@
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()