from apiflask import APIBlueprint from flask import abort from flask_sqlalchemy import get_debug_queries from ...dependencies import db from ...examination_schedule.models import ExaminationSchedule, TermOfDefence from ...project_supervisor.models import ProjectSupervisor from ..schemas import WorkloadSchema bp = APIBlueprint("workloads", __name__, url_prefix="/") @bp.get('/examination_schedule//workloads/') @bp.output(WorkloadSchema) def workloads_statistics(examination_schedule_id: int) -> dict: es = ExaminationSchedule.query.filter_by(id=examination_schedule_id).first() if es is None: abort(404, "Not found examination schedule!") statistics = db.session.query( ProjectSupervisor.first_name + " " + ProjectSupervisor.last_name, db.func.count(TermOfDefence.group_id), db.func.count(TermOfDefence.id), ).join(TermOfDefence.members_of_committee). \ group_by(ProjectSupervisor.id).all() # print(statistics) # print(len(statistics)) # print(get_debug_queries()) workloads = ({"full_name": s[0], "groups_assigned_to_his_committee": s[1], "assigned_to_committee": s[2]} for s in statistics) return {'workloads': workloads}