From 29d78dabddde177599283f96a5a8549055219e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Sat, 12 Jul 2025 16:06:16 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20dashboard=20?= =?UTF-8?q?=E7=9A=84=E6=9F=A5=E8=AF=A2=E6=80=A7=E8=83=BD=20(#393)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bili_sync/src/api/routes/dashboard/mod.rs | 42 +++++++------------ crates/bili_sync_migration/src/lib.rs | 2 + ...50712_080013_add_video_created_at_index.rs | 36 ++++++++++++++++ 3 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 crates/bili_sync_migration/src/m20250712_080013_add_video_created_at_index.rs diff --git a/crates/bili_sync/src/api/routes/dashboard/mod.rs b/crates/bili_sync/src/api/routes/dashboard/mod.rs index b405211..374fe64 100644 --- a/crates/bili_sync/src/api/routes/dashboard/mod.rs +++ b/crates/bili_sync/src/api/routes/dashboard/mod.rs @@ -34,35 +34,25 @@ async fn get_dashboard( // 用 SeaORM 太复杂了,直接写个裸 SQL " SELECT - dates.day AS day, - COUNT(video.id) AS cnt + dates.day AS day, + COUNT(video.id) AS cnt FROM - ( - SELECT - STRFTIME( - '%Y-%m-%d', - DATE('now', '-' || n || ' days', 'localtime')) AS day - FROM - ( - SELECT - 0 AS n UNION ALL - SELECT - 1 UNION ALL - SELECT - 2 UNION ALL - SELECT - 3 UNION ALL - SELECT - 4 UNION ALL - SELECT - 5 UNION ALL - SELECT - 6)) AS dates - LEFT JOIN video ON STRFTIME('%Y-%m-%d', video.created_at, 'localtime') = dates.day + ( + SELECT + STRFTIME('%Y-%m-%d', DATE('now', '-' || n || ' days', 'localtime')) AS day, + DATETIME(DATE('now', '-' || n || ' days', 'localtime'), 'utc') AS start_utc_datetime, + DATETIME(DATE('now', '-' || n || ' days', '+1 day', 'localtime'), 'utc') AS end_utc_datetime + FROM + ( + SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 + ) + ) AS dates +LEFT JOIN + video ON video.created_at >= dates.start_utc_datetime AND video.created_at < dates.end_utc_datetime GROUP BY - dates.day + dates.day ORDER BY - dates.day; + dates.day; " )) .all(db.as_ref()), diff --git a/crates/bili_sync_migration/src/lib.rs b/crates/bili_sync_migration/src/lib.rs index e29a77c..ff8ee24 100644 --- a/crates/bili_sync_migration/src/lib.rs +++ b/crates/bili_sync_migration/src/lib.rs @@ -7,6 +7,7 @@ mod m20240724_161008_submission; mod m20250122_062926_add_latest_row_at; mod m20250612_090826_add_enabled; mod m20250613_043257_add_config; +mod m20250712_080013_add_video_created_at_index; pub struct Migrator; @@ -21,6 +22,7 @@ impl MigratorTrait for Migrator { Box::new(m20250122_062926_add_latest_row_at::Migration), Box::new(m20250612_090826_add_enabled::Migration), Box::new(m20250613_043257_add_config::Migration), + Box::new(m20250712_080013_add_video_created_at_index::Migration), ] } } diff --git a/crates/bili_sync_migration/src/m20250712_080013_add_video_created_at_index.rs b/crates/bili_sync_migration/src/m20250712_080013_add_video_created_at_index.rs new file mode 100644 index 0000000..1e5659c --- /dev/null +++ b/crates/bili_sync_migration/src/m20250712_080013_add_video_created_at_index.rs @@ -0,0 +1,36 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_index( + Index::create() + .table(Video::Table) + .name("video_created_at_index") + .col(Video::CreatedAt) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_index( + Index::drop() + .table(Video::Table) + .name("video_created_at_index") + .to_owned(), + ) + .await + } +} + +#[derive(DeriveIden)] +enum Video { + Table, + CreatedAt, +}