You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.0 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. from fastapi import Depends, APIRouter, status, Query, Path, HTTPException
  2. from internal.models import *
  3. from internal.database import fetch_one, fetch_all, execute_query, response_success, raise_if_exists, raise_if_not_found
  4. from dependencies import get_current_active_user
  5. from collections import defaultdict
  6. router = APIRouter(
  7. prefix="/statistics",
  8. tags=['统计']
  9. )
  10. # 统计编辑量
  11. @router.get("/list")
  12. async def statistic_list():
  13. select_query = """SELECT DATE(create_at) AS date,COUNT(*) AS writCount FROM
  14. (SELECT create_at FROM diarys
  15. UNION ALL
  16. SELECT create_at FROM blogs) AS combined
  17. GROUP BY DATE
  18. ORDER BY DATE DESC;"""
  19. statistic_list = fetch_all(select_query)
  20. return response_success(statistic_list, "statistic get list success")
  21. # 记录所有blog和diary数据
  22. # 获取首页数据
  23. @router.get("/homepage")
  24. async def get_homepage_data():
  25. # 获取博客数据
  26. select_blog = """
  27. SELECT blogs.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink,
  28. blogs.wordcount, blogtypes.typename, JSON_ARRAYAGG(labels.labelname) AS labelnames
  29. FROM blogs
  30. LEFT JOIN `blogtypes` ON blogs.typeid = blogtypes.id
  31. LEFT JOIN blog_label ON blog_label.blogid = blogs.id
  32. LEFT JOIN labels ON blog_label.labelid = labels.id
  33. GROUP BY blogs.id, blogs.blogtitle, blogs.blogcontent, blogs.create_at, blogs.imglink,
  34. blogs.wordcount, blogtypes.typename
  35. ORDER BY create_at DESC
  36. """
  37. blog_list = fetch_all(select_blog)
  38. # 获取日记数据
  39. select_diary = """
  40. SELECT diarys.id, diarys.diarytitle, diarys.diarycontent, diarys.imglink,
  41. diarytypes.typename, diarys.create_at, diarys.update_at
  42. FROM diarys
  43. LEFT JOIN diarytypes ON diarys.typeid = diarytypes.id
  44. ORDER BY create_at DESC
  45. """
  46. diary_list = fetch_all(select_diary)
  47. # 合并博客和日记数据,按照创建日期降序排序
  48. combined_data = sorted(blog_list + diary_list, key=lambda x: x['create_at'], reverse=True)
  49. return {"data": combined_data}
  50. @router.get("/searchtitle")
  51. async def search_homepage_data(title: str = Query("", description="Title to search for")):
  52. # 查询博客数据
  53. select_blog = """
  54. SELECT
  55. blogs.id,
  56. blogs.blogtitle,
  57. blogs.blogcontent,
  58. blogs.create_at,
  59. blogs.imglink,
  60. blogs.wordcount,
  61. blogtypes.typename,
  62. JSON_ARRAYAGG(labels.labelname) AS labelnames
  63. FROM blogs
  64. LEFT JOIN blogtypes ON blogs.typeid = blogtypes.id
  65. LEFT JOIN blog_label ON blog_label.blogid = blogs.id
  66. LEFT JOIN labels ON blog_label.labelid = labels.id
  67. WHERE blogs.blogtitle LIKE %s
  68. GROUP BY
  69. blogs.id,
  70. blogs.blogtitle,
  71. blogs.blogcontent,
  72. blogs.create_at,
  73. blogs.imglink,
  74. blogs.wordcount,
  75. blogtypes.typename
  76. ORDER BY create_at DESC
  77. """
  78. blog_list =fetch_all(select_blog, ('%' + title + '%',))
  79. # 查询日记数据
  80. select_diary = """
  81. SELECT
  82. diarys.id,
  83. diarys.diarytitle,
  84. diarys.diarycontent,
  85. diarys.imglink,
  86. diarytypes.typename,
  87. diarys.create_at,
  88. diarys.update_at
  89. FROM diarys
  90. LEFT JOIN diarytypes ON diarys.typeid = diarytypes.id
  91. WHERE diarys.diarytitle LIKE %s
  92. ORDER BY create_at DESC
  93. """
  94. diary_list =fetch_all(select_diary, ('%' + title + '%',))
  95. # 合并博客和日记数据,按照创建日期降序排序
  96. filtered_blog_list = [item for item in blog_list if item.get('create_at')]
  97. filtered_diary_list = [item for item in diary_list if item.get('create_at')]
  98. combined_list = filtered_blog_list + filtered_diary_list
  99. combined_data = sorted(combined_list, key=lambda x: x['create_at'], reverse=True)
  100. return {"data": combined_data}