mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-13 16:10:05 +08:00
update 0.0.6.2
This commit is contained in:
Regular → Executable
+128
-128
@@ -1,128 +1,128 @@
|
||||
from pathlib import Path
|
||||
from configs.path_config import DATA_PATH
|
||||
import nonebot
|
||||
import os
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
nonebot.load_plugins("plugins/statistics")
|
||||
|
||||
old_file1 = Path(DATA_PATH) / "_prefix_count.json"
|
||||
old_file2 = Path(DATA_PATH) / "_prefix_user_count.json"
|
||||
new_path = Path(DATA_PATH) / "statistics"
|
||||
new_path.mkdir(parents=True, exist_ok=True)
|
||||
if old_file1.exists():
|
||||
os.rename(old_file1, new_path / "_prefix_count.json")
|
||||
if old_file2.exists():
|
||||
os.rename(old_file2, new_path / "_prefix_user_count.json")
|
||||
|
||||
|
||||
# 修改旧数据
|
||||
|
||||
statistics_group_file = Path(DATA_PATH) / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = Path(DATA_PATH) / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
for file in [statistics_group_file, statistics_user_file]:
|
||||
if file.exists():
|
||||
with open(file, "r", encoding="utf8") as f:
|
||||
data = json.load(f)
|
||||
if not (statistics_group_file.parent / f"{file}.bak").exists():
|
||||
with open(f"{file}.bak", "w", encoding="utf8") as wf:
|
||||
json.dump(data, wf, ensure_ascii=False, indent=4)
|
||||
for x in ["total_statistics", "day_statistics"]:
|
||||
for key in data[x].keys():
|
||||
num = 0
|
||||
if data[x][key].get("ai") is not None:
|
||||
if data[x][key].get("Ai") is not None:
|
||||
data[x][key]["Ai"] += data[x][key]["ai"]
|
||||
else:
|
||||
data[x][key]["Ai"] = data[x][key]["ai"]
|
||||
del data[x][key]["ai"]
|
||||
if data[x][key].get("抽卡") is not None:
|
||||
if data[x][key].get("游戏抽卡") is not None:
|
||||
data[x][key]["游戏抽卡"] += data[x][key]["抽卡"]
|
||||
else:
|
||||
data[x][key]["游戏抽卡"] = data[x][key]["抽卡"]
|
||||
del data[x][key]["抽卡"]
|
||||
if data[x][key].get("我的道具") is not None:
|
||||
num += data[x][key]["我的道具"]
|
||||
del data[x][key]["我的道具"]
|
||||
if data[x][key].get("使用道具") is not None:
|
||||
num += data[x][key]["使用道具"]
|
||||
del data[x][key]["使用道具"]
|
||||
if data[x][key].get("我的金币") is not None:
|
||||
num += data[x][key]["我的金币"]
|
||||
del data[x][key]["我的金币"]
|
||||
if data[x][key].get("购买") is not None:
|
||||
num += data[x][key]["购买"]
|
||||
del data[x][key]["购买"]
|
||||
if data[x][key].get("商店") is not None:
|
||||
data[x][key]["商店"] += num
|
||||
else:
|
||||
data[x][key]["商店"] = num
|
||||
for x in ["week_statistics", "month_statistics"]:
|
||||
for key in data[x].keys():
|
||||
if key == "total":
|
||||
if data[x][key].get("ai") is not None:
|
||||
if data[x][key].get("Ai") is not None:
|
||||
data[x][key]["Ai"] += data[x][key]["ai"]
|
||||
else:
|
||||
data[x][key]["Ai"] = data[x][key]["ai"]
|
||||
del data[x][key]["ai"]
|
||||
if data[x][key].get("抽卡") is not None:
|
||||
if data[x][key].get("游戏抽卡") is not None:
|
||||
data[x][key]["游戏抽卡"] += data[x][key]["抽卡"]
|
||||
else:
|
||||
data[x][key]["游戏抽卡"] = data[x][key]["抽卡"]
|
||||
del data[x][key]["抽卡"]
|
||||
if data[x][key].get("我的道具") is not None:
|
||||
num += data[x][key]["我的道具"]
|
||||
del data[x][key]["我的道具"]
|
||||
if data[x][key].get("使用道具") is not None:
|
||||
num += data[x][key]["使用道具"]
|
||||
del data[x][key]["使用道具"]
|
||||
if data[x][key].get("我的金币") is not None:
|
||||
num += data[x][key]["我的金币"]
|
||||
del data[x][key]["我的金币"]
|
||||
if data[x][key].get("购买") is not None:
|
||||
num += data[x][key]["购买"]
|
||||
del data[x][key]["购买"]
|
||||
if data[x][key].get("商店") is not None:
|
||||
data[x][key]["商店"] += num
|
||||
else:
|
||||
data[x][key]["商店"] = num
|
||||
else:
|
||||
for day in data[x][key].keys():
|
||||
num = 0
|
||||
if data[x][key][day].get("ai") is not None:
|
||||
if data[x][key][day].get("Ai") is not None:
|
||||
data[x][key][day]["Ai"] += data[x][key][day]["ai"]
|
||||
else:
|
||||
data[x][key][day]["Ai"] = data[x][key][day]["ai"]
|
||||
del data[x][key][day]["ai"]
|
||||
if data[x][key][day].get("抽卡") is not None:
|
||||
if data[x][key][day].get("游戏抽卡") is not None:
|
||||
data[x][key][day]["游戏抽卡"] += data[x][key][day]["抽卡"]
|
||||
else:
|
||||
data[x][key][day]["游戏抽卡"] = data[x][key][day]["抽卡"]
|
||||
del data[x][key][day]["抽卡"]
|
||||
if data[x][key][day].get("我的道具") is not None:
|
||||
num += data[x][key][day]["我的道具"]
|
||||
del data[x][key][day]["我的道具"]
|
||||
if data[x][key][day].get("使用道具") is not None:
|
||||
num += data[x][key][day]["使用道具"]
|
||||
del data[x][key][day]["使用道具"]
|
||||
if data[x][key][day].get("我的金币") is not None:
|
||||
num += data[x][key][day]["我的金币"]
|
||||
del data[x][key][day]["我的金币"]
|
||||
if data[x][key][day].get("购买") is not None:
|
||||
num += data[x][key][day]["购买"]
|
||||
del data[x][key][day]["购买"]
|
||||
if data[x][key][day].get("商店") is not None:
|
||||
data[x][key][day]["商店"] += num
|
||||
else:
|
||||
data[x][key][day]["商店"] = num
|
||||
with open(file, "w", encoding="utf8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
from pathlib import Path
|
||||
from configs.path_config import DATA_PATH
|
||||
import nonebot
|
||||
import os
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
nonebot.load_plugins("plugins/statistics")
|
||||
|
||||
old_file1 = Path(DATA_PATH) / "_prefix_count.json"
|
||||
old_file2 = Path(DATA_PATH) / "_prefix_user_count.json"
|
||||
new_path = Path(DATA_PATH) / "statistics"
|
||||
new_path.mkdir(parents=True, exist_ok=True)
|
||||
if old_file1.exists():
|
||||
os.rename(old_file1, new_path / "_prefix_count.json")
|
||||
if old_file2.exists():
|
||||
os.rename(old_file2, new_path / "_prefix_user_count.json")
|
||||
|
||||
|
||||
# 修改旧数据
|
||||
|
||||
statistics_group_file = Path(DATA_PATH) / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = Path(DATA_PATH) / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
for file in [statistics_group_file, statistics_user_file]:
|
||||
if file.exists():
|
||||
with open(file, "r", encoding="utf8") as f:
|
||||
data = json.load(f)
|
||||
if not (statistics_group_file.parent / f"{file}.bak").exists():
|
||||
with open(f"{file}.bak", "w", encoding="utf8") as wf:
|
||||
json.dump(data, wf, ensure_ascii=False, indent=4)
|
||||
for x in ["total_statistics", "day_statistics"]:
|
||||
for key in data[x].keys():
|
||||
num = 0
|
||||
if data[x][key].get("ai") is not None:
|
||||
if data[x][key].get("Ai") is not None:
|
||||
data[x][key]["Ai"] += data[x][key]["ai"]
|
||||
else:
|
||||
data[x][key]["Ai"] = data[x][key]["ai"]
|
||||
del data[x][key]["ai"]
|
||||
if data[x][key].get("抽卡") is not None:
|
||||
if data[x][key].get("游戏抽卡") is not None:
|
||||
data[x][key]["游戏抽卡"] += data[x][key]["抽卡"]
|
||||
else:
|
||||
data[x][key]["游戏抽卡"] = data[x][key]["抽卡"]
|
||||
del data[x][key]["抽卡"]
|
||||
if data[x][key].get("我的道具") is not None:
|
||||
num += data[x][key]["我的道具"]
|
||||
del data[x][key]["我的道具"]
|
||||
if data[x][key].get("使用道具") is not None:
|
||||
num += data[x][key]["使用道具"]
|
||||
del data[x][key]["使用道具"]
|
||||
if data[x][key].get("我的金币") is not None:
|
||||
num += data[x][key]["我的金币"]
|
||||
del data[x][key]["我的金币"]
|
||||
if data[x][key].get("购买") is not None:
|
||||
num += data[x][key]["购买"]
|
||||
del data[x][key]["购买"]
|
||||
if data[x][key].get("商店") is not None:
|
||||
data[x][key]["商店"] += num
|
||||
else:
|
||||
data[x][key]["商店"] = num
|
||||
for x in ["week_statistics", "month_statistics"]:
|
||||
for key in data[x].keys():
|
||||
if key == "total":
|
||||
if data[x][key].get("ai") is not None:
|
||||
if data[x][key].get("Ai") is not None:
|
||||
data[x][key]["Ai"] += data[x][key]["ai"]
|
||||
else:
|
||||
data[x][key]["Ai"] = data[x][key]["ai"]
|
||||
del data[x][key]["ai"]
|
||||
if data[x][key].get("抽卡") is not None:
|
||||
if data[x][key].get("游戏抽卡") is not None:
|
||||
data[x][key]["游戏抽卡"] += data[x][key]["抽卡"]
|
||||
else:
|
||||
data[x][key]["游戏抽卡"] = data[x][key]["抽卡"]
|
||||
del data[x][key]["抽卡"]
|
||||
if data[x][key].get("我的道具") is not None:
|
||||
num += data[x][key]["我的道具"]
|
||||
del data[x][key]["我的道具"]
|
||||
if data[x][key].get("使用道具") is not None:
|
||||
num += data[x][key]["使用道具"]
|
||||
del data[x][key]["使用道具"]
|
||||
if data[x][key].get("我的金币") is not None:
|
||||
num += data[x][key]["我的金币"]
|
||||
del data[x][key]["我的金币"]
|
||||
if data[x][key].get("购买") is not None:
|
||||
num += data[x][key]["购买"]
|
||||
del data[x][key]["购买"]
|
||||
if data[x][key].get("商店") is not None:
|
||||
data[x][key]["商店"] += num
|
||||
else:
|
||||
data[x][key]["商店"] = num
|
||||
else:
|
||||
for day in data[x][key].keys():
|
||||
num = 0
|
||||
if data[x][key][day].get("ai") is not None:
|
||||
if data[x][key][day].get("Ai") is not None:
|
||||
data[x][key][day]["Ai"] += data[x][key][day]["ai"]
|
||||
else:
|
||||
data[x][key][day]["Ai"] = data[x][key][day]["ai"]
|
||||
del data[x][key][day]["ai"]
|
||||
if data[x][key][day].get("抽卡") is not None:
|
||||
if data[x][key][day].get("游戏抽卡") is not None:
|
||||
data[x][key][day]["游戏抽卡"] += data[x][key][day]["抽卡"]
|
||||
else:
|
||||
data[x][key][day]["游戏抽卡"] = data[x][key][day]["抽卡"]
|
||||
del data[x][key][day]["抽卡"]
|
||||
if data[x][key][day].get("我的道具") is not None:
|
||||
num += data[x][key][day]["我的道具"]
|
||||
del data[x][key][day]["我的道具"]
|
||||
if data[x][key][day].get("使用道具") is not None:
|
||||
num += data[x][key][day]["使用道具"]
|
||||
del data[x][key][day]["使用道具"]
|
||||
if data[x][key][day].get("我的金币") is not None:
|
||||
num += data[x][key][day]["我的金币"]
|
||||
del data[x][key][day]["我的金币"]
|
||||
if data[x][key][day].get("购买") is not None:
|
||||
num += data[x][key][day]["购买"]
|
||||
del data[x][key][day]["购买"]
|
||||
if data[x][key][day].get("商店") is not None:
|
||||
data[x][key][day]["商店"] += num
|
||||
else:
|
||||
data[x][key][day]["商店"] = num
|
||||
with open(file, "w", encoding="utf8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
Regular → Executable
+280
-280
@@ -1,280 +1,280 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent
|
||||
from models.group_info import GroupInfo
|
||||
from nonebot.typing import T_State
|
||||
from pathlib import Path
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH
|
||||
from utils.utils import get_message_text
|
||||
from utils.image_utils import CreateMat
|
||||
from utils.message_builder import image
|
||||
from utils.manager import plugins2settings_manager
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
|
||||
__zx_plugin_name__ = "功能调用统计可视化"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
功能调用统计可视化
|
||||
指令:
|
||||
功能调用统计
|
||||
日功能调用统计
|
||||
周功能调用统计 ?[功能]
|
||||
月功能调用统计 ?[功能]
|
||||
我的功能调用统计
|
||||
我的日功能调用统计 ?[功能]
|
||||
我的周功能调用统计 ?[功能]
|
||||
我的月功能调用统计 ?[功能]
|
||||
""".strip()
|
||||
__plugin_superuser_usage__ = """
|
||||
usage:
|
||||
功能调用统计可视化
|
||||
指令:
|
||||
全局功能调用统计
|
||||
全局日功能调用统计
|
||||
全局周功能调用统计 ?[功能]
|
||||
全局月功能调用统计 ?[功能]
|
||||
""".strip()
|
||||
__plugin_des__ = "功能调用统计可视化"
|
||||
__plugin_cmd__ = [
|
||||
"功能调用统计",
|
||||
"全局功能调用统计 [_superuser]",
|
||||
"全局日功能调用统计 [_superuser]",
|
||||
"全局周功能调用统计 ?[功能] [_superuser]",
|
||||
"全局月功能调用统计 ?[功能] [_superuser]",
|
||||
"周功能调用统计 ?[功能]",
|
||||
"月功能调用统计 ?[功能]",
|
||||
"我的功能调用统计",
|
||||
"我的日功能调用统计 ?[功能]",
|
||||
"我的周功能调用统计 ?[功能]",
|
||||
"我的月功能调用统计 ?[功能]",
|
||||
]
|
||||
__plugin_type__ = ("功能调用统计可视化", 1)
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
"level": 5,
|
||||
"default_status": True,
|
||||
"limit_superuser": False,
|
||||
"cmd": ["功能调用统计"],
|
||||
}
|
||||
|
||||
|
||||
statistics = on_command(
|
||||
"功能调用统计",
|
||||
aliases={
|
||||
"全局功能调用统计",
|
||||
"全局日功能调用统计",
|
||||
"全局周功能调用统计",
|
||||
"全局月功能调用统计",
|
||||
"日功能调用统计",
|
||||
"周功能调用统计",
|
||||
"月功能调用统计",
|
||||
"我的功能调用统计",
|
||||
"我的日功能调用统计",
|
||||
"我的周功能调用统计",
|
||||
"我的月功能调用统计",
|
||||
},
|
||||
priority=5,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
statistics_group_file = Path(DATA_PATH) / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = Path(DATA_PATH) / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
|
||||
@statistics.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
msg = get_message_text(event.json())
|
||||
if state["_prefix"]["raw_command"][:2] == "全局":
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
data: dict = json.load(open(statistics_group_file, "r", encoding="utf8"))
|
||||
if state["_prefix"]["raw_command"][2] == '日':
|
||||
itype = 'day_statistics'
|
||||
elif state["_prefix"]["raw_command"][2] == '周':
|
||||
itype = 'week_statistics'
|
||||
elif state["_prefix"]["raw_command"][2] == '月':
|
||||
itype = 'month_statistics'
|
||||
else:
|
||||
itype = 'total_statistics'
|
||||
tmp_dict = {}
|
||||
data = data[itype]
|
||||
if itype in ["day_statistics", "total_statistics"]:
|
||||
for key in data['total']:
|
||||
tmp_dict[key] = data['total'][key]
|
||||
else:
|
||||
for group in data.keys():
|
||||
if group != 'total':
|
||||
for day in data[group].keys():
|
||||
for plugin_name in data[group][day].keys():
|
||||
if data[group][day][plugin_name] is not None:
|
||||
if tmp_dict.get(plugin_name) is None:
|
||||
tmp_dict[plugin_name] = 1
|
||||
else:
|
||||
tmp_dict[plugin_name] += data[group][day][plugin_name]
|
||||
bar_graph = await init_bar_graph(tmp_dict, state["_prefix"]["raw_command"])
|
||||
await asyncio.get_event_loop().run_in_executor(None, bar_graph.gen_graph)
|
||||
await statistics.finish(image(b64=bar_graph.pic2bs4()))
|
||||
return
|
||||
if state["_prefix"]["raw_command"][:2] == "我的":
|
||||
itype = "user"
|
||||
key = str(event.user_id)
|
||||
state["_prefix"]["raw_command"] = state["_prefix"]["raw_command"][2:]
|
||||
if not statistics_user_file.exists():
|
||||
await statistics.finish("统计文件不存在...", at_sender=True)
|
||||
else:
|
||||
if not isinstance(event, GroupMessageEvent):
|
||||
await statistics.finish("请在群内调用此功能...")
|
||||
itype = "group"
|
||||
key = str(event.group_id)
|
||||
if not statistics_group_file.exists():
|
||||
await statistics.finish("统计文件不存在...", at_sender=True)
|
||||
plugin = ""
|
||||
if state["_prefix"]["raw_command"][0] == "日":
|
||||
arg = "day_statistics"
|
||||
elif state["_prefix"]["raw_command"][0] == "周":
|
||||
arg = "week_statistics"
|
||||
elif state["_prefix"]["raw_command"][0] == "月":
|
||||
arg = "month_statistics"
|
||||
else:
|
||||
arg = "total_statistics"
|
||||
if msg:
|
||||
plugin = plugins2settings_manager.get_plugin_module(msg)
|
||||
if not plugin:
|
||||
if arg not in ["day_statistics", "total_statistics"]:
|
||||
await statistics.finish("未找到此功能的调用...", at_sender=True)
|
||||
if itype == "group":
|
||||
data: dict = json.load(open(statistics_group_file, "r", encoding="utf8"))
|
||||
if not data[arg].get(str(event.group_id)):
|
||||
await statistics.finish("该群统计数据不存在...", at_sender=True)
|
||||
else:
|
||||
data: dict = json.load(open(statistics_user_file, "r", encoding="utf8"))
|
||||
if not data[arg].get(str(event.user_id)):
|
||||
await statistics.finish("该用户统计数据不存在...", at_sender=True)
|
||||
day_index = data["day_index"]
|
||||
data = data[arg][key]
|
||||
if itype == "group":
|
||||
name = await GroupInfo.get_group_info(event.group_id)
|
||||
name = name.group_name if name else str(event.group_id)
|
||||
else:
|
||||
name = event.sender.card if event.sender.card else event.sender.nickname
|
||||
img = await generate_statistics_img(data, arg, name, plugin, day_index)
|
||||
await statistics.send(image(b64=img))
|
||||
|
||||
|
||||
async def generate_statistics_img(
|
||||
data: dict, arg: str, name: str, plugin: str, day_index: int
|
||||
):
|
||||
try:
|
||||
plugin = plugins2settings_manager.get_plugin_data(plugin)['cmd'][0]
|
||||
except (KeyError, IndexError):
|
||||
pass
|
||||
bar_graph = None
|
||||
if arg == "day_statistics":
|
||||
bar_graph = await init_bar_graph(data, f"{name} 日功能调用统计")
|
||||
elif arg == "week_statistics":
|
||||
if plugin:
|
||||
current_week = day_index % 7
|
||||
week_lst = []
|
||||
if current_week == 0:
|
||||
week_lst = [1, 2, 3, 4, 5, 6, 7]
|
||||
else:
|
||||
for i in range(current_week + 1, 7):
|
||||
week_lst.append(str(i))
|
||||
for i in range(current_week + 1):
|
||||
week_lst.append(str(i))
|
||||
count = []
|
||||
for i in range(7):
|
||||
if int(week_lst[i]) == 7:
|
||||
try:
|
||||
count.append(data[str(0)][plugin])
|
||||
except KeyError:
|
||||
count.append(0)
|
||||
else:
|
||||
try:
|
||||
count.append(data[str(week_lst[i])][plugin])
|
||||
except KeyError:
|
||||
count.append(0)
|
||||
week_lst = ["7" if i == "0" else i for i in week_lst]
|
||||
bar_graph = CreateMat(
|
||||
y=count,
|
||||
mat_type="line",
|
||||
title=f"{name} 周 {plugin} 功能调用统计【为7天统计】",
|
||||
x_index=week_lst,
|
||||
display_num=True,
|
||||
background=[
|
||||
f"{IMAGE_PATH}/background/create_mat/{x}"
|
||||
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
|
||||
],
|
||||
bar_color=["*"],
|
||||
)
|
||||
else:
|
||||
bar_graph = await init_bar_graph(update_data(data), f"{name} 周功能调用统计【为7天统计】")
|
||||
elif arg == "month_statistics":
|
||||
if plugin:
|
||||
day_index = day_index % 30
|
||||
day_lst = []
|
||||
for i in range(day_index + 1, 30):
|
||||
day_lst.append(i)
|
||||
for i in range(day_index + 1):
|
||||
day_lst.append(i)
|
||||
count = [data[str(day_lst[i])][plugin] for i in range(30)]
|
||||
day_lst = [str(x + 1) for x in day_lst]
|
||||
bar_graph = CreateMat(
|
||||
y=count,
|
||||
mat_type="line",
|
||||
title=f"{name} 月 {plugin} 功能调用统计【为30天统计】",
|
||||
x_index=day_lst,
|
||||
display_num=True,
|
||||
background=[
|
||||
f"{IMAGE_PATH}/background/create_mat/{x}"
|
||||
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
|
||||
],
|
||||
bar_color=["*"],
|
||||
)
|
||||
else:
|
||||
bar_graph = await init_bar_graph(update_data(data), f"{name} 月功能调用统计【为30天统计】")
|
||||
elif arg == "total_statistics":
|
||||
bar_graph = await init_bar_graph(data, f"{name} 功能调用统计")
|
||||
await asyncio.get_event_loop().run_in_executor(None, bar_graph.gen_graph)
|
||||
return bar_graph.pic2bs4()
|
||||
|
||||
|
||||
async def init_bar_graph(data: dict, title: str) -> CreateMat:
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _init_bar_graph, data, title)
|
||||
|
||||
|
||||
def _init_bar_graph(data: dict, title: str) -> CreateMat:
|
||||
bar_graph = CreateMat(
|
||||
y=[data[x] for x in data.keys() if data[x] != 0],
|
||||
mat_type="barh",
|
||||
title=title,
|
||||
x_index=[x for x in data.keys() if data[x] != 0],
|
||||
display_num=True,
|
||||
background=[
|
||||
f"{IMAGE_PATH}/background/create_mat/{x}"
|
||||
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
|
||||
],
|
||||
bar_color=["*"],
|
||||
)
|
||||
return bar_graph
|
||||
|
||||
|
||||
def update_data(data: dict):
|
||||
tmp_dict = {}
|
||||
for day in data.keys():
|
||||
for plugin_name in data[day].keys():
|
||||
# print(f'{day}:{plugin_name} = {data[day][plugin_name]}')
|
||||
if data[day][plugin_name] is not None:
|
||||
if tmp_dict.get(plugin_name) is None:
|
||||
tmp_dict[plugin_name] = 1
|
||||
else:
|
||||
tmp_dict[plugin_name] += data[day][plugin_name]
|
||||
return tmp_dict
|
||||
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent
|
||||
from models.group_info import GroupInfo
|
||||
from nonebot.typing import T_State
|
||||
from pathlib import Path
|
||||
from configs.path_config import DATA_PATH, IMAGE_PATH
|
||||
from utils.utils import get_message_text
|
||||
from utils.image_utils import CreateMat
|
||||
from utils.message_builder import image
|
||||
from utils.manager import plugins2settings_manager
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
|
||||
__zx_plugin_name__ = "功能调用统计可视化"
|
||||
__plugin_usage__ = """
|
||||
usage:
|
||||
功能调用统计可视化
|
||||
指令:
|
||||
功能调用统计
|
||||
日功能调用统计
|
||||
周功能调用统计 ?[功能]
|
||||
月功能调用统计 ?[功能]
|
||||
我的功能调用统计
|
||||
我的日功能调用统计 ?[功能]
|
||||
我的周功能调用统计 ?[功能]
|
||||
我的月功能调用统计 ?[功能]
|
||||
""".strip()
|
||||
__plugin_superuser_usage__ = """
|
||||
usage:
|
||||
功能调用统计可视化
|
||||
指令:
|
||||
全局功能调用统计
|
||||
全局日功能调用统计
|
||||
全局周功能调用统计 ?[功能]
|
||||
全局月功能调用统计 ?[功能]
|
||||
""".strip()
|
||||
__plugin_des__ = "功能调用统计可视化"
|
||||
__plugin_cmd__ = [
|
||||
"功能调用统计",
|
||||
"全局功能调用统计 [_superuser]",
|
||||
"全局日功能调用统计 [_superuser]",
|
||||
"全局周功能调用统计 ?[功能] [_superuser]",
|
||||
"全局月功能调用统计 ?[功能] [_superuser]",
|
||||
"周功能调用统计 ?[功能]",
|
||||
"月功能调用统计 ?[功能]",
|
||||
"我的功能调用统计",
|
||||
"我的日功能调用统计 ?[功能]",
|
||||
"我的周功能调用统计 ?[功能]",
|
||||
"我的月功能调用统计 ?[功能]",
|
||||
]
|
||||
__plugin_type__ = ("功能调用统计可视化", 1)
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
__plugin_settings__ = {
|
||||
"level": 5,
|
||||
"default_status": True,
|
||||
"limit_superuser": False,
|
||||
"cmd": ["功能调用统计"],
|
||||
}
|
||||
|
||||
|
||||
statistics = on_command(
|
||||
"功能调用统计",
|
||||
aliases={
|
||||
"全局功能调用统计",
|
||||
"全局日功能调用统计",
|
||||
"全局周功能调用统计",
|
||||
"全局月功能调用统计",
|
||||
"日功能调用统计",
|
||||
"周功能调用统计",
|
||||
"月功能调用统计",
|
||||
"我的功能调用统计",
|
||||
"我的日功能调用统计",
|
||||
"我的周功能调用统计",
|
||||
"我的月功能调用统计",
|
||||
},
|
||||
priority=5,
|
||||
block=True,
|
||||
)
|
||||
|
||||
|
||||
statistics_group_file = Path(DATA_PATH) / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = Path(DATA_PATH) / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
|
||||
@statistics.handle()
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
msg = get_message_text(event.json())
|
||||
if state["_prefix"]["raw_command"][:2] == "全局":
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
data: dict = json.load(open(statistics_group_file, "r", encoding="utf8"))
|
||||
if state["_prefix"]["raw_command"][2] == '日':
|
||||
itype = 'day_statistics'
|
||||
elif state["_prefix"]["raw_command"][2] == '周':
|
||||
itype = 'week_statistics'
|
||||
elif state["_prefix"]["raw_command"][2] == '月':
|
||||
itype = 'month_statistics'
|
||||
else:
|
||||
itype = 'total_statistics'
|
||||
tmp_dict = {}
|
||||
data = data[itype]
|
||||
if itype in ["day_statistics", "total_statistics"]:
|
||||
for key in data['total']:
|
||||
tmp_dict[key] = data['total'][key]
|
||||
else:
|
||||
for group in data.keys():
|
||||
if group != 'total':
|
||||
for day in data[group].keys():
|
||||
for plugin_name in data[group][day].keys():
|
||||
if data[group][day][plugin_name] is not None:
|
||||
if tmp_dict.get(plugin_name) is None:
|
||||
tmp_dict[plugin_name] = 1
|
||||
else:
|
||||
tmp_dict[plugin_name] += data[group][day][plugin_name]
|
||||
bar_graph = await init_bar_graph(tmp_dict, state["_prefix"]["raw_command"])
|
||||
await asyncio.get_event_loop().run_in_executor(None, bar_graph.gen_graph)
|
||||
await statistics.finish(image(b64=bar_graph.pic2bs4()))
|
||||
return
|
||||
if state["_prefix"]["raw_command"][:2] == "我的":
|
||||
itype = "user"
|
||||
key = str(event.user_id)
|
||||
state["_prefix"]["raw_command"] = state["_prefix"]["raw_command"][2:]
|
||||
if not statistics_user_file.exists():
|
||||
await statistics.finish("统计文件不存在...", at_sender=True)
|
||||
else:
|
||||
if not isinstance(event, GroupMessageEvent):
|
||||
await statistics.finish("请在群内调用此功能...")
|
||||
itype = "group"
|
||||
key = str(event.group_id)
|
||||
if not statistics_group_file.exists():
|
||||
await statistics.finish("统计文件不存在...", at_sender=True)
|
||||
plugin = ""
|
||||
if state["_prefix"]["raw_command"][0] == "日":
|
||||
arg = "day_statistics"
|
||||
elif state["_prefix"]["raw_command"][0] == "周":
|
||||
arg = "week_statistics"
|
||||
elif state["_prefix"]["raw_command"][0] == "月":
|
||||
arg = "month_statistics"
|
||||
else:
|
||||
arg = "total_statistics"
|
||||
if msg:
|
||||
plugin = plugins2settings_manager.get_plugin_module(msg)
|
||||
if not plugin:
|
||||
if arg not in ["day_statistics", "total_statistics"]:
|
||||
await statistics.finish("未找到此功能的调用...", at_sender=True)
|
||||
if itype == "group":
|
||||
data: dict = json.load(open(statistics_group_file, "r", encoding="utf8"))
|
||||
if not data[arg].get(str(event.group_id)):
|
||||
await statistics.finish("该群统计数据不存在...", at_sender=True)
|
||||
else:
|
||||
data: dict = json.load(open(statistics_user_file, "r", encoding="utf8"))
|
||||
if not data[arg].get(str(event.user_id)):
|
||||
await statistics.finish("该用户统计数据不存在...", at_sender=True)
|
||||
day_index = data["day_index"]
|
||||
data = data[arg][key]
|
||||
if itype == "group":
|
||||
name = await GroupInfo.get_group_info(event.group_id)
|
||||
name = name.group_name if name else str(event.group_id)
|
||||
else:
|
||||
name = event.sender.card if event.sender.card else event.sender.nickname
|
||||
img = await generate_statistics_img(data, arg, name, plugin, day_index)
|
||||
await statistics.send(image(b64=img))
|
||||
|
||||
|
||||
async def generate_statistics_img(
|
||||
data: dict, arg: str, name: str, plugin: str, day_index: int
|
||||
):
|
||||
try:
|
||||
plugin = plugins2settings_manager.get_plugin_data(plugin)['cmd'][0]
|
||||
except (KeyError, IndexError):
|
||||
pass
|
||||
bar_graph = None
|
||||
if arg == "day_statistics":
|
||||
bar_graph = await init_bar_graph(data, f"{name} 日功能调用统计")
|
||||
elif arg == "week_statistics":
|
||||
if plugin:
|
||||
current_week = day_index % 7
|
||||
week_lst = []
|
||||
if current_week == 0:
|
||||
week_lst = [1, 2, 3, 4, 5, 6, 7]
|
||||
else:
|
||||
for i in range(current_week + 1, 7):
|
||||
week_lst.append(str(i))
|
||||
for i in range(current_week + 1):
|
||||
week_lst.append(str(i))
|
||||
count = []
|
||||
for i in range(7):
|
||||
if int(week_lst[i]) == 7:
|
||||
try:
|
||||
count.append(data[str(0)][plugin])
|
||||
except KeyError:
|
||||
count.append(0)
|
||||
else:
|
||||
try:
|
||||
count.append(data[str(week_lst[i])][plugin])
|
||||
except KeyError:
|
||||
count.append(0)
|
||||
week_lst = ["7" if i == "0" else i for i in week_lst]
|
||||
bar_graph = CreateMat(
|
||||
y=count,
|
||||
mat_type="line",
|
||||
title=f"{name} 周 {plugin} 功能调用统计【为7天统计】",
|
||||
x_index=week_lst,
|
||||
display_num=True,
|
||||
background=[
|
||||
f"{IMAGE_PATH}/background/create_mat/{x}"
|
||||
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
|
||||
],
|
||||
bar_color=["*"],
|
||||
)
|
||||
else:
|
||||
bar_graph = await init_bar_graph(update_data(data), f"{name} 周功能调用统计【为7天统计】")
|
||||
elif arg == "month_statistics":
|
||||
if plugin:
|
||||
day_index = day_index % 30
|
||||
day_lst = []
|
||||
for i in range(day_index + 1, 30):
|
||||
day_lst.append(i)
|
||||
for i in range(day_index + 1):
|
||||
day_lst.append(i)
|
||||
count = [data[str(day_lst[i])][plugin] for i in range(30)]
|
||||
day_lst = [str(x + 1) for x in day_lst]
|
||||
bar_graph = CreateMat(
|
||||
y=count,
|
||||
mat_type="line",
|
||||
title=f"{name} 月 {plugin} 功能调用统计【为30天统计】",
|
||||
x_index=day_lst,
|
||||
display_num=True,
|
||||
background=[
|
||||
f"{IMAGE_PATH}/background/create_mat/{x}"
|
||||
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
|
||||
],
|
||||
bar_color=["*"],
|
||||
)
|
||||
else:
|
||||
bar_graph = await init_bar_graph(update_data(data), f"{name} 月功能调用统计【为30天统计】")
|
||||
elif arg == "total_statistics":
|
||||
bar_graph = await init_bar_graph(data, f"{name} 功能调用统计")
|
||||
await asyncio.get_event_loop().run_in_executor(None, bar_graph.gen_graph)
|
||||
return bar_graph.pic2bs4()
|
||||
|
||||
|
||||
async def init_bar_graph(data: dict, title: str) -> CreateMat:
|
||||
return await asyncio.get_event_loop().run_in_executor(None, _init_bar_graph, data, title)
|
||||
|
||||
|
||||
def _init_bar_graph(data: dict, title: str) -> CreateMat:
|
||||
bar_graph = CreateMat(
|
||||
y=[data[x] for x in data.keys() if data[x] != 0],
|
||||
mat_type="barh",
|
||||
title=title,
|
||||
x_index=[x for x in data.keys() if data[x] != 0],
|
||||
display_num=True,
|
||||
background=[
|
||||
f"{IMAGE_PATH}/background/create_mat/{x}"
|
||||
for x in os.listdir(f"{IMAGE_PATH}/background/create_mat")
|
||||
],
|
||||
bar_color=["*"],
|
||||
)
|
||||
return bar_graph
|
||||
|
||||
|
||||
def update_data(data: dict):
|
||||
tmp_dict = {}
|
||||
for day in data.keys():
|
||||
for plugin_name in data[day].keys():
|
||||
# print(f'{day}:{plugin_name} = {data[day][plugin_name]}')
|
||||
if data[day][plugin_name] is not None:
|
||||
if tmp_dict.get(plugin_name) is None:
|
||||
tmp_dict[plugin_name] = 1
|
||||
else:
|
||||
tmp_dict[plugin_name] += data[day][plugin_name]
|
||||
return tmp_dict
|
||||
|
||||
|
||||
Regular → Executable
+217
-217
@@ -1,217 +1,217 @@
|
||||
from configs.path_config import DATA_PATH
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_postprocessor
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
|
||||
from datetime import datetime
|
||||
from utils.manager import plugins2settings_manager
|
||||
from utils.utils import scheduler
|
||||
from nonebot.typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
|
||||
__zx_plugin_name__ = "功能调用统计 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
statistics_group_file = Path(DATA_PATH) / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = Path(DATA_PATH) / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
try:
|
||||
with open(statistics_group_file, "r", encoding="utf8") as f:
|
||||
_prefix_count_dict = json.load(f)
|
||||
except (FileNotFoundError, ValueError):
|
||||
_prefix_count_dict = {
|
||||
"total_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": str(datetime.now().date()),
|
||||
"day_index": 0,
|
||||
}
|
||||
|
||||
try:
|
||||
with open(statistics_user_file, "r", encoding="utf8") as f:
|
||||
_prefix_user_count_dict = json.load(f)
|
||||
except (FileNotFoundError, ValueError):
|
||||
_prefix_user_count_dict = {
|
||||
"total_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": str(datetime.now().date()),
|
||||
"day_index": 0,
|
||||
}
|
||||
|
||||
|
||||
# 以前版本转换
|
||||
if _prefix_count_dict.get("day_index") is None:
|
||||
tmp = _prefix_count_dict.copy()
|
||||
_prefix_count_dict = {
|
||||
"total_statistics": tmp["total_statistics"],
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": tmp["start_time"],
|
||||
"day_index": 0,
|
||||
}
|
||||
|
||||
|
||||
# 添加命令次数
|
||||
@run_postprocessor
|
||||
async def _(
|
||||
matcher: Matcher,
|
||||
exception: Optional[Exception],
|
||||
bot: Bot,
|
||||
event: GroupMessageEvent,
|
||||
state: T_State,
|
||||
):
|
||||
global _prefix_count_dict
|
||||
if (
|
||||
matcher.type == "message"
|
||||
and matcher.priority not in [1, 9]
|
||||
and matcher.module not in ["update_info", "statistics_handle"]
|
||||
):
|
||||
module = matcher.module
|
||||
day_index = _prefix_count_dict["day_index"]
|
||||
try:
|
||||
group_id = str(event.group_id)
|
||||
except AttributeError:
|
||||
group_id = "total"
|
||||
user_id = str(event.user_id)
|
||||
plugin_name = plugins2settings_manager.get_plugin_data(module)
|
||||
if plugin_name and plugin_name.get('cmd'):
|
||||
plugin_name = plugin_name.get('cmd')[0]
|
||||
check_exists_key(group_id, user_id, plugin_name)
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
data["total_statistics"]["total"][plugin_name] += 1
|
||||
data["day_statistics"]["total"][plugin_name] += 1
|
||||
data["week_statistics"]["total"][plugin_name] += 1
|
||||
data["month_statistics"]["total"][plugin_name] += 1
|
||||
# print(_prefix_count_dict)
|
||||
if group_id != "total":
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
if data == _prefix_count_dict:
|
||||
key = group_id
|
||||
else:
|
||||
key = user_id
|
||||
data["total_statistics"][key][plugin_name] += 1
|
||||
data["day_statistics"][key][plugin_name] += 1
|
||||
data["week_statistics"][key][str(day_index % 7)][
|
||||
plugin_name
|
||||
] += 1
|
||||
data["month_statistics"][key][str(day_index % 30)][
|
||||
plugin_name
|
||||
] += 1
|
||||
with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
with open(
|
||||
statistics_user_file, "w", encoding="utf8"
|
||||
) as f:
|
||||
json.dump(_prefix_user_count_dict, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def check_exists_key(group_id: str, user_id: str, plugin_name: str):
|
||||
global _prefix_count_dict, _prefix_user_count_dict
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
if data == _prefix_count_dict:
|
||||
key = group_id
|
||||
else:
|
||||
key = user_id
|
||||
if not data["total_statistics"]["total"].get(plugin_name):
|
||||
data["total_statistics"]["total"][plugin_name] = 0
|
||||
if not data["day_statistics"]["total"].get(plugin_name):
|
||||
data["day_statistics"]["total"][plugin_name] = 0
|
||||
if not data["week_statistics"]["total"].get(plugin_name):
|
||||
data["week_statistics"]["total"][plugin_name] = 0
|
||||
if not data["month_statistics"]["total"].get(plugin_name):
|
||||
data["month_statistics"]["total"][plugin_name] = 0
|
||||
|
||||
if not data["total_statistics"].get(key):
|
||||
data["total_statistics"][key] = {}
|
||||
if not data["total_statistics"][key].get(plugin_name):
|
||||
data["total_statistics"][key][plugin_name] = 0
|
||||
if not data["day_statistics"].get(key):
|
||||
data["day_statistics"][key] = {}
|
||||
if not data["day_statistics"][key].get(plugin_name):
|
||||
data["day_statistics"][key][plugin_name] = 0
|
||||
|
||||
if key != 'total':
|
||||
if not data["week_statistics"].get(key):
|
||||
data["week_statistics"][key] = {}
|
||||
if data["week_statistics"][key].get("0") is None:
|
||||
for i in range(7):
|
||||
data["week_statistics"][key][str(i)] = {}
|
||||
if data["week_statistics"][key]["0"].get(plugin_name) is None:
|
||||
for i in range(7):
|
||||
data["week_statistics"][key][str(i)][plugin_name] = 0
|
||||
|
||||
if not data["month_statistics"].get(key):
|
||||
data["month_statistics"][key] = {}
|
||||
if data["month_statistics"][key].get("0") is None:
|
||||
for i in range(30):
|
||||
data["month_statistics"][key][str(i)] = {}
|
||||
if data["month_statistics"][key]["0"].get(plugin_name) is None:
|
||||
for i in range(30):
|
||||
data["month_statistics"][key][str(i)][plugin_name] = 0
|
||||
|
||||
|
||||
# 天
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=0,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
data["day_index"] += 1
|
||||
for x in data["day_statistics"].keys():
|
||||
for key in data["day_statistics"][x].keys():
|
||||
try:
|
||||
data["day_statistics"][x][key] = 0
|
||||
except KeyError:
|
||||
pass
|
||||
for type_ in ["week_statistics", "month_statistics"]:
|
||||
index = str(
|
||||
data["day_index"] % 7
|
||||
if type_ == "week_statistics"
|
||||
else data["day_index"] % 30
|
||||
)
|
||||
for x in data[type_].keys():
|
||||
try:
|
||||
for key in data[type_][x][index].keys():
|
||||
data[type_][x][index][key] = 0
|
||||
except KeyError:
|
||||
pass
|
||||
with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
with open(statistics_user_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_user_count_dict, f, indent=4, ensure_ascii=False)
|
||||
|
||||
from configs.path_config import DATA_PATH
|
||||
from nonebot.matcher import Matcher
|
||||
from nonebot.message import run_postprocessor
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
|
||||
from datetime import datetime
|
||||
from utils.manager import plugins2settings_manager
|
||||
from utils.utils import scheduler
|
||||
from nonebot.typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
|
||||
__zx_plugin_name__ = "功能调用统计 [Hidden]"
|
||||
__plugin_version__ = 0.1
|
||||
__plugin_author__ = "HibiKier"
|
||||
|
||||
statistics_group_file = Path(DATA_PATH) / "statistics" / "_prefix_count.json"
|
||||
statistics_user_file = Path(DATA_PATH) / "statistics" / "_prefix_user_count.json"
|
||||
|
||||
try:
|
||||
with open(statistics_group_file, "r", encoding="utf8") as f:
|
||||
_prefix_count_dict = json.load(f)
|
||||
except (FileNotFoundError, ValueError):
|
||||
_prefix_count_dict = {
|
||||
"total_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": str(datetime.now().date()),
|
||||
"day_index": 0,
|
||||
}
|
||||
|
||||
try:
|
||||
with open(statistics_user_file, "r", encoding="utf8") as f:
|
||||
_prefix_user_count_dict = json.load(f)
|
||||
except (FileNotFoundError, ValueError):
|
||||
_prefix_user_count_dict = {
|
||||
"total_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": str(datetime.now().date()),
|
||||
"day_index": 0,
|
||||
}
|
||||
|
||||
|
||||
# 以前版本转换
|
||||
if _prefix_count_dict.get("day_index") is None:
|
||||
tmp = _prefix_count_dict.copy()
|
||||
_prefix_count_dict = {
|
||||
"total_statistics": tmp["total_statistics"],
|
||||
"day_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"week_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"month_statistics": {
|
||||
"total": {},
|
||||
},
|
||||
"start_time": tmp["start_time"],
|
||||
"day_index": 0,
|
||||
}
|
||||
|
||||
|
||||
# 添加命令次数
|
||||
@run_postprocessor
|
||||
async def _(
|
||||
matcher: Matcher,
|
||||
exception: Optional[Exception],
|
||||
bot: Bot,
|
||||
event: GroupMessageEvent,
|
||||
state: T_State,
|
||||
):
|
||||
global _prefix_count_dict
|
||||
if (
|
||||
matcher.type == "message"
|
||||
and matcher.priority not in [1, 9]
|
||||
and matcher.module not in ["update_info", "statistics_handle"]
|
||||
):
|
||||
module = matcher.module
|
||||
day_index = _prefix_count_dict["day_index"]
|
||||
try:
|
||||
group_id = str(event.group_id)
|
||||
except AttributeError:
|
||||
group_id = "total"
|
||||
user_id = str(event.user_id)
|
||||
plugin_name = plugins2settings_manager.get_plugin_data(module)
|
||||
if plugin_name and plugin_name.get('cmd'):
|
||||
plugin_name = plugin_name.get('cmd')[0]
|
||||
check_exists_key(group_id, user_id, plugin_name)
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
data["total_statistics"]["total"][plugin_name] += 1
|
||||
data["day_statistics"]["total"][plugin_name] += 1
|
||||
data["week_statistics"]["total"][plugin_name] += 1
|
||||
data["month_statistics"]["total"][plugin_name] += 1
|
||||
# print(_prefix_count_dict)
|
||||
if group_id != "total":
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
if data == _prefix_count_dict:
|
||||
key = group_id
|
||||
else:
|
||||
key = user_id
|
||||
data["total_statistics"][key][plugin_name] += 1
|
||||
data["day_statistics"][key][plugin_name] += 1
|
||||
data["week_statistics"][key][str(day_index % 7)][
|
||||
plugin_name
|
||||
] += 1
|
||||
data["month_statistics"][key][str(day_index % 30)][
|
||||
plugin_name
|
||||
] += 1
|
||||
with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
with open(
|
||||
statistics_user_file, "w", encoding="utf8"
|
||||
) as f:
|
||||
json.dump(_prefix_user_count_dict, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def check_exists_key(group_id: str, user_id: str, plugin_name: str):
|
||||
global _prefix_count_dict, _prefix_user_count_dict
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
if data == _prefix_count_dict:
|
||||
key = group_id
|
||||
else:
|
||||
key = user_id
|
||||
if not data["total_statistics"]["total"].get(plugin_name):
|
||||
data["total_statistics"]["total"][plugin_name] = 0
|
||||
if not data["day_statistics"]["total"].get(plugin_name):
|
||||
data["day_statistics"]["total"][plugin_name] = 0
|
||||
if not data["week_statistics"]["total"].get(plugin_name):
|
||||
data["week_statistics"]["total"][plugin_name] = 0
|
||||
if not data["month_statistics"]["total"].get(plugin_name):
|
||||
data["month_statistics"]["total"][plugin_name] = 0
|
||||
|
||||
if not data["total_statistics"].get(key):
|
||||
data["total_statistics"][key] = {}
|
||||
if not data["total_statistics"][key].get(plugin_name):
|
||||
data["total_statistics"][key][plugin_name] = 0
|
||||
if not data["day_statistics"].get(key):
|
||||
data["day_statistics"][key] = {}
|
||||
if not data["day_statistics"][key].get(plugin_name):
|
||||
data["day_statistics"][key][plugin_name] = 0
|
||||
|
||||
if key != 'total':
|
||||
if not data["week_statistics"].get(key):
|
||||
data["week_statistics"][key] = {}
|
||||
if data["week_statistics"][key].get("0") is None:
|
||||
for i in range(7):
|
||||
data["week_statistics"][key][str(i)] = {}
|
||||
if data["week_statistics"][key]["0"].get(plugin_name) is None:
|
||||
for i in range(7):
|
||||
data["week_statistics"][key][str(i)][plugin_name] = 0
|
||||
|
||||
if not data["month_statistics"].get(key):
|
||||
data["month_statistics"][key] = {}
|
||||
if data["month_statistics"][key].get("0") is None:
|
||||
for i in range(30):
|
||||
data["month_statistics"][key][str(i)] = {}
|
||||
if data["month_statistics"][key]["0"].get(plugin_name) is None:
|
||||
for i in range(30):
|
||||
data["month_statistics"][key][str(i)][plugin_name] = 0
|
||||
|
||||
|
||||
# 天
|
||||
@scheduler.scheduled_job(
|
||||
"cron",
|
||||
hour=0,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
for data in [_prefix_count_dict, _prefix_user_count_dict]:
|
||||
data["day_index"] += 1
|
||||
for x in data["day_statistics"].keys():
|
||||
for key in data["day_statistics"][x].keys():
|
||||
try:
|
||||
data["day_statistics"][x][key] = 0
|
||||
except KeyError:
|
||||
pass
|
||||
for type_ in ["week_statistics", "month_statistics"]:
|
||||
index = str(
|
||||
data["day_index"] % 7
|
||||
if type_ == "week_statistics"
|
||||
else data["day_index"] % 30
|
||||
)
|
||||
for x in data[type_].keys():
|
||||
try:
|
||||
for key in data[type_][x][index].keys():
|
||||
data[type_][x][index][key] = 0
|
||||
except KeyError:
|
||||
pass
|
||||
with open(statistics_group_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_count_dict, f, indent=4, ensure_ascii=False)
|
||||
with open(statistics_user_file, "w", encoding="utf8") as f:
|
||||
json.dump(_prefix_user_count_dict, f, indent=4, ensure_ascii=False)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user