From 7fddc7a77ef646fb46085302cd13f14031aee705 Mon Sep 17 00:00:00 2001 From: maksim Date: Sat, 8 Jun 2024 01:22:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 4 +++- repository.py | 10 +++++++++- router_class.py | 3 +++ router_statistics.py | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 router_statistics.py diff --git a/main.py b/main.py index 3445756..f9bff10 100644 --- a/main.py +++ b/main.py @@ -2,11 +2,12 @@ from fastapi import FastAPI import logging from contextlib import asynccontextmanager -from database import create_tables, delete_tables + from router_questions import router as questions_router from router_class import router as class_router from router_flight import router as flight_router from router_reviews import router as reviews_router +from router_statistics import router as statistic_router # Настройка логирования logging.basicConfig(level=logging.INFO) @@ -23,4 +24,5 @@ app.include_router(questions_router) app.include_router(class_router) app.include_router(flight_router) app.include_router(reviews_router) +app.include_router(statistic_router) diff --git a/repository.py b/repository.py index 0d09d4b..4560f83 100644 --- a/repository.py +++ b/repository.py @@ -1,4 +1,4 @@ -from sqlalchemy import select, delete +from sqlalchemy import select, delete, func from sqlalchemy.orm import joinedload from datetime import datetime import urllib.parse @@ -68,3 +68,11 @@ class QuestionRepository: question_models = result.scalars().all() question_schemas = [SQuestion.model_validate(question_model) for question_model in question_models] 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 diff --git a/router_class.py b/router_class.py index 7a5b313..e882f04 100644 --- a/router_class.py +++ b/router_class.py @@ -1,6 +1,8 @@ from fastapi import APIRouter from typing import List +from repository import QuestionRepository + router = APIRouter( prefix="/class", tags=["Class"], @@ -21,3 +23,4 @@ async def get_class_names() -> List[str]: + diff --git a/router_statistics.py b/router_statistics.py new file mode 100644 index 0000000..2ba9673 --- /dev/null +++ b/router_statistics.py @@ -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 + + +