mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-18 18:50:05 +08:00
🐛 修复功能调用统计空白 (#1694)
This commit is contained in:
@@ -2,18 +2,17 @@ from datetime import datetime, timedelta
|
||||
|
||||
from tortoise.functions import Count
|
||||
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.models.group_member_info import GroupInfoUser
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.models.statistics import Statistics
|
||||
from zhenxun.utils.image_utils import BuildImage
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.utils.echart_utils import ChartUtils
|
||||
from zhenxun.utils.echart_utils.models import Barh
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.image_utils import BuildImage, BuildMat, MatType
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.models.group_member_info import GroupInfoUser
|
||||
|
||||
|
||||
class StatisticsManage:
|
||||
|
||||
@classmethod
|
||||
async def get_statistics(
|
||||
cls,
|
||||
@@ -28,12 +27,12 @@ class StatisticsManage:
|
||||
if search_type == "day":
|
||||
day = 1
|
||||
day_type = "日"
|
||||
if search_type == "week":
|
||||
day = 7
|
||||
day_type = "周"
|
||||
if search_type == "month":
|
||||
elif search_type == "month":
|
||||
day = 30
|
||||
day_type = "月"
|
||||
elif search_type == "week":
|
||||
day = 7
|
||||
day_type = "周"
|
||||
if day_type:
|
||||
day_type += f"({day}天)"
|
||||
title = ""
|
||||
@@ -53,7 +52,7 @@ class StatisticsManage:
|
||||
else:
|
||||
title = "功能调用统计"
|
||||
if is_global and not user_id:
|
||||
title = "全局 " + title
|
||||
title = f"全局 {title}"
|
||||
return await cls.get_global_statistics(plugin_name, day, title)
|
||||
if user_id:
|
||||
return await cls.get_my_statistics(user_id, group_id, day, title)
|
||||
@@ -76,9 +75,11 @@ class StatisticsManage:
|
||||
.group_by("plugin_name")
|
||||
.values_list("plugin_name", "count")
|
||||
)
|
||||
if not data_list:
|
||||
return "统计数据为空..."
|
||||
return await cls.__build_image(data_list, title)
|
||||
return (
|
||||
await cls.__build_image(data_list, title)
|
||||
if data_list
|
||||
else "统计数据为空..."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_my_statistics(
|
||||
@@ -95,9 +96,11 @@ class StatisticsManage:
|
||||
.group_by("plugin_name")
|
||||
.values_list("plugin_name", "count")
|
||||
)
|
||||
if not data_list:
|
||||
return "统计数据为空..."
|
||||
return await cls.__build_image(data_list, title)
|
||||
return (
|
||||
await cls.__build_image(data_list, title)
|
||||
if data_list
|
||||
else "统计数据为空..."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def get_group_statistics(cls, group_id: str, day: int | None, title: str):
|
||||
@@ -110,9 +113,11 @@ class StatisticsManage:
|
||||
.group_by("plugin_name")
|
||||
.values_list("plugin_name", "count")
|
||||
)
|
||||
if not data_list:
|
||||
return "统计数据为空..."
|
||||
return await cls.__build_image(data_list, title)
|
||||
return (
|
||||
await cls.__build_image(data_list, title)
|
||||
if data_list
|
||||
else "统计数据为空..."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def __build_image(cls, data_list: list[tuple[str, int]], title: str):
|
||||
@@ -126,5 +131,5 @@ class StatisticsManage:
|
||||
for plugin in plugin_info:
|
||||
x_index.append(plugin.name)
|
||||
data.append(module2count.get(plugin.module, 0))
|
||||
barh = Barh(data=data, category_data=x_index)
|
||||
barh = Barh(data=data, category_data=x_index, title=title)
|
||||
return await ChartUtils.barh(barh)
|
||||
|
||||
@@ -1,22 +1,30 @@
|
||||
import os
|
||||
import random
|
||||
|
||||
from nonebot_plugin_htmlrender import template_to_pic
|
||||
|
||||
from zhenxun.configs.path_config import TEMPLATE_PATH
|
||||
from zhenxun.utils._build_image import BuildImage
|
||||
from zhenxun.configs.path_config import TEMPLATE_PATH
|
||||
|
||||
from .models import Barh
|
||||
|
||||
BACKGROUND_PATH = TEMPLATE_PATH / "bar_chart" / "background"
|
||||
|
||||
|
||||
class ChartUtils:
|
||||
|
||||
@classmethod
|
||||
async def barh(cls, data: Barh) -> BuildImage:
|
||||
"""横向统计图"""
|
||||
to_json = data.dict()
|
||||
to_json["background_image"] = (
|
||||
f"./background/{random.choice(os.listdir(BACKGROUND_PATH))}"
|
||||
)
|
||||
pic = await template_to_pic(
|
||||
template_path=str((TEMPLATE_PATH / "bar_chart").absolute()),
|
||||
template_name="main.html",
|
||||
templates={"data": data},
|
||||
templates={"data": to_json},
|
||||
pages={
|
||||
"viewport": {"width": 1000, "height": 500},
|
||||
"viewport": {"width": 1000, "height": 1000},
|
||||
"base_url": f"file://{TEMPLATE_PATH}",
|
||||
},
|
||||
wait=2,
|
||||
|
||||
@@ -2,8 +2,9 @@ from pydantic import BaseModel
|
||||
|
||||
|
||||
class Barh(BaseModel):
|
||||
|
||||
category_data: list[str]
|
||||
"""坐标轴数据"""
|
||||
data: list[int | float]
|
||||
"""实际数据"""
|
||||
title: str
|
||||
"""标题"""
|
||||
|
||||
Reference in New Issue
Block a user