statistics.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. from collections import defaultdict
  5. from fastapi import Depends
  6. from sqlalchemy import select, func, desc
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from crud.marktask import crud_task
  9. from crud.resource import crud_work_resource, crud_exam_resource
  10. from crud.school import crud_school, crud_class
  11. from crud.sysdata.region import crud_region
  12. from crud.user import crud_teacher, crud_student
  13. from models.marktask import MarkTask
  14. from models.resource import WorkResource, ExamResource
  15. from models.sysdata.region import Region
  16. from models.user import Admin
  17. from utils.depends import get_async_db, get_current_user
  18. async def statistic_resource(db: AsyncSession = Depends(get_async_db),
  19. current_user: Admin = Depends(get_current_user)):
  20. # 作业资源统计
  21. stmt = select(WorkResource.subject, func.count()).select_from(WorkResource)\
  22. .group_by(WorkResource.subject)
  23. total_work = 0
  24. work_resource_dist = []
  25. for x in await crud_work_resource.execute_v2(db, stmt):
  26. total_work += x[1]
  27. temp = {"sub": x[0], "count": x[1]}
  28. work_resource_dist.append(temp)
  29. # 考试资源统计
  30. stmt = select(ExamResource.subject, func.count()).select_from(ExamResource)\
  31. .group_by(ExamResource.subject)
  32. total_exam = 0
  33. exam_resource_dist = []
  34. for x in await crud_work_resource.execute_v2(db, stmt):
  35. total_exam += x[1]
  36. temp = {"sub": x[0], "count": x[1]}
  37. exam_resource_dist.append(temp)
  38. # 最终结果
  39. data = {
  40. "all": {
  41. "total": total_work + total_exam,
  42. "work": total_work,
  43. "exam": total_exam
  44. },
  45. "work_dist": work_resource_dist,
  46. "exam_dist": exam_resource_dist
  47. }
  48. return {"data": data}
  49. async def resource_date_dist(db: AsyncSession = Depends(get_async_db),
  50. current_user: Admin = Depends(get_current_user)):
  51. today = datetime.date.today()
  52. start_date = datetime.datetime(today.year, 1, 1, 0, 0, 0)
  53. temp = defaultdict(int)
  54. for x in [(WorkResource, crud_work_resource), (ExamResource, crud_exam_resource)]:
  55. stmt = select(
  56. func.concat(func.year(x[0].created_at), ".",
  57. func.month(x[0].created_at)).label("month"),
  58. func.count(),
  59. ).select_from(x[0]).where(x[0].created_at >= start_date).group_by("month")
  60. for y in await x[1].execute_v2(db, stmt):
  61. temp[y[0]] += y[1]
  62. data = [{"date": k, "count": v} for k, v in temp.items()]
  63. data.sort(key=lambda z: z["date"])
  64. return {"data": data}
  65. async def statistic_marktask(db: AsyncSession = Depends(get_async_db),
  66. current_user: Admin = Depends(get_current_user)):
  67. data = {"work": 0, "exam": 0, "completed": 0, "ongoing": 0, "total": 0}
  68. # 按任务类型统计
  69. stmt = select(MarkTask.mtype, func.count()).select_from(MarkTask).group_by(MarkTask.mtype)
  70. for x in await crud_task.execute_v2(db, stmt):
  71. data[x[0]] += x[1]
  72. data["total"] += x[1]
  73. # 按批阅进度统计
  74. stmt = select(MarkTask.status, func.count()).select_from(MarkTask).where(MarkTask.status > 0)\
  75. .group_by(MarkTask.status)
  76. for item in await crud_task.execute_v2(db, stmt):
  77. if item[0] == 2:
  78. data["completed"] += item[1]
  79. else:
  80. data["ongoing"] += item[1]
  81. return {"data": data}
  82. async def get_top10_school_by_task(db: AsyncSession = Depends(get_async_db),
  83. current_user: Admin = Depends(get_current_user)):
  84. stmt = select(MarkTask.school_name, func.count().label("school_count"))\
  85. .select_from(MarkTask).group_by(MarkTask.school_name)\
  86. .order_by(desc("school_count")).limit(10)
  87. data = [{"key": x[0], "val": x[1]} for x in await crud_task.execute_v2(db, stmt)]
  88. return {"data": data}
  89. async def get_top10_subject_by_task(db: AsyncSession = Depends(get_async_db),
  90. current_user: Admin = Depends(get_current_user)):
  91. stmt = select(MarkTask.subject, func.count().label("subject_count")).select_from(MarkTask)\
  92. .group_by(MarkTask.subject).order_by(desc("subject_count")).limit(10)
  93. data = [{"key": x[0], "val": x[1]} for x in await crud_task.execute_v2(db, stmt)]
  94. return {"data": data}
  95. async def statistic_school(db: AsyncSession = Depends(get_async_db),
  96. current_user: Admin = Depends(get_current_user)):
  97. school_count = await crud_school.count(db)
  98. class_count = await crud_class.count(db)
  99. teacher_count = await crud_teacher.count(db)
  100. student_count = await crud_student.count(db)
  101. data = {
  102. "school": school_count,
  103. "class": class_count,
  104. "teacher": teacher_count,
  105. "student": student_count
  106. }
  107. return {"data": data}
  108. async def school_distribute(db: AsyncSession = Depends(get_async_db),
  109. current_user: Admin = Depends(get_current_user)):
  110. # 查询学校
  111. _, db_schools = await crud_school.find_all(db)
  112. temp = defaultdict(list)
  113. area_codes = []
  114. for item in db_schools:
  115. area = item.area_code[14:]
  116. temp[area].append({"id": item.id, "name": item.name, "area": item.area_name})
  117. area_codes.append(area)
  118. # 根据地区查询经纬度
  119. if area_codes:
  120. _, db_regions = await crud_region.find_all(db, filters=[Region.code.in_(area_codes)])
  121. for region in db_regions:
  122. for school in temp[region.code]:
  123. school["position"] = (region.lng, region.lat)
  124. data = []
  125. for item in temp.values():
  126. data.extend(item)
  127. return {"data": data}