Сделал статистику

This commit is contained in:
maksim 2024-06-08 01:22:35 +04:00
parent ba13e38690
commit 7fddc7a77e
4 changed files with 31 additions and 2 deletions

View File

@ -2,11 +2,12 @@ from fastapi import FastAPI
import logging import logging
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from database import create_tables, delete_tables
from router_questions import router as questions_router from router_questions import router as questions_router
from router_class import router as class_router from router_class import router as class_router
from router_flight import router as flight_router from router_flight import router as flight_router
from router_reviews import router as reviews_router from router_reviews import router as reviews_router
from router_statistics import router as statistic_router
# Настройка логирования # Настройка логирования
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -23,4 +24,5 @@ app.include_router(questions_router)
app.include_router(class_router) app.include_router(class_router)
app.include_router(flight_router) app.include_router(flight_router)
app.include_router(reviews_router) app.include_router(reviews_router)
app.include_router(statistic_router)

View File

@ -1,4 +1,4 @@
from sqlalchemy import select, delete from sqlalchemy import select, delete, func
from sqlalchemy.orm import joinedload from sqlalchemy.orm import joinedload
from datetime import datetime from datetime import datetime
import urllib.parse import urllib.parse
@ -68,3 +68,11 @@ class QuestionRepository:
question_models = result.scalars().all() question_models = result.scalars().all()
question_schemas = [SQuestion.model_validate(question_model) for question_model in question_models] question_schemas = [SQuestion.model_validate(question_model) for question_model in question_models]
return question_schemas return question_schemas
@staticmethod
async def get_class_statistics() -> dict:
async with new_session() as session:
query = select(QuestionOrm.answer, func.count(QuestionOrm.id)).group_by(QuestionOrm.answer)
result = await session.execute(query)
statistics = {answer: count for answer, count in result.fetchall()}
return statistics

View File

@ -1,6 +1,8 @@
from fastapi import APIRouter from fastapi import APIRouter
from typing import List from typing import List
from repository import QuestionRepository
router = APIRouter( router = APIRouter(
prefix="/class", prefix="/class",
tags=["Class"], tags=["Class"],
@ -21,3 +23,4 @@ async def get_class_names() -> List[str]:

16
router_statistics.py Normal file
View File

@ -0,0 +1,16 @@
from fastapi import APIRouter
from repository import QuestionRepository
router = APIRouter(
prefix="/statistics",
tags=["Statistics"],
)
@router.get("/")
async def get_class_statistics():
statistics = await QuestionRepository.get_class_statistics()
return statistics