添加了一些每日统计的辅助函数

This commit is contained in:
:NoBody: committed 2026-04-08 22:51:34 +08:00
1 parent 47c3e908ec
commit 7096384161
6 files changed
+87 -11

No files matched your search

+48 -9
View File
@@ -1,28 +1,67 @@
use std::ops::Add;
use anyhow::{Ok, Result};
use chrono::NaiveDate;
use log::info;
use crate::{config::Config, db};
pub struct ScheduleHandler {
sche: tokio_cron_scheduler::JobScheduler, //这里可以添加一些定时任务相关的字段,比如定时器,任务列表等
}
impl ScheduleHandler {
//new新实例后会自动启动定时任务,所以不需要单独调用start方法
pub async fn new() -> Self {
Self {
let _self = Self {
sche: tokio_cron_scheduler::JobScheduler::new().await.unwrap(),
//初始化字段
}
};
_self.start().await.unwrap();
_self
}
pub async fn start(&self) -> Result<()> {
async fn start(&self) -> Result<()> {
//这里可以启动定时任务,比如使用tokio的定时器,或者其他的定时任务库
info!("定时任务已启动");
self.sche.start().await?;
Ok(())
}
pub async fn add_daily_statistics_and_report_schedule(
&self,
bot: impl crate::bot::define::Bot + Send + Sync + 'static,
cron: impl AsRef<str>,
) -> Result<()> {
let cron = cron.as_ref();
//获取和转换日期
let date = chrono::Local::now()
.naive_local()
.date()
.add(chrono::Duration::hours(8)); //调整时区为北京时间
let date = date.format("%Y年%-m月%-d日").to_string();
//获取签到人数
let db_path = std::path::Path::new("daily_state.sled.db");
let db = db::crud_handler::CrudHandler::new(
db::sled_db::SledDB::new(db_path).expect("无法获取数据库实例"),
);
let states = db.get_all_user_state().await?;
let checked_nums = states.len();
//TODO:完成此函数,重点是获取全部人数,最好低耦合
let md_reply = r#"
# {date}签到统计报告
- 今日签到人数:{checked_nums}
"#; //每日统计报告
Ok(())
}
pub fn daily_statistics_and_report(&self) -> Result<()> {
//这里可以实现每天统计和报告的逻辑,比如统计每天的消息数量,活跃用户等,并生成报告发送到指定的频道
Ok(())
}
pub fn month_statistics_and_report(&self) -> Result<()> {
pub fn add_month_statistics_and_report_schedule(&self, cron: impl AsRef<str>) -> Result<()> {
let cron = cron.as_ref();
//这里可以实现每月统计和报告的逻辑,比如统计每月的消息数量,活跃用户等,并生成报告发送到指定的频道
Ok(())
}