update code level
This commit is contained in:
@@ -219,18 +219,6 @@ impl Database {
|
||||
}).collect()
|
||||
}
|
||||
|
||||
// ── 统计 ──
|
||||
|
||||
pub async fn get_metadata_stats(&self) -> MetadataStats {
|
||||
MetadataStats {
|
||||
total_persons: sqlx::query_scalar("SELECT COUNT(*) FROM persons").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_aliases: sqlx::query_scalar("SELECT COUNT(*) FROM person_aliases").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_albums_with_person: sqlx::query_scalar("SELECT COUNT(*) FROM albums WHERE person_name_original IS NOT NULL AND person_name_original != ''").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_albums_without_person: sqlx::query_scalar("SELECT COUNT(*) FROM albums WHERE person_name_original IS NULL OR person_name_original = ''").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
total_tags: sqlx::query_scalar("SELECT COUNT(*) FROM album_tags").fetch_one(&self.pool).await.unwrap_or(0),
|
||||
}
|
||||
}
|
||||
|
||||
// ── 写入 ──
|
||||
|
||||
pub async fn insert_person(&self, name: &str, display_name: Option<&str>) -> i64 {
|
||||
|
||||
@@ -20,8 +20,8 @@ pub struct TagListResponse {
|
||||
pub struct MetadataStats {
|
||||
pub total_persons: i64,
|
||||
pub total_aliases: i64,
|
||||
pub total_albums_with_person: i64,
|
||||
pub total_albums_without_person: i64,
|
||||
pub total_albums: i64,
|
||||
pub total_images: i64,
|
||||
pub total_tags: i64,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::cache::TagCache;
|
||||
use crate::db::Database;
|
||||
use crate::search_db::SearchDb;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
/// GET /api/metadata/stats — 仍查 DB(需要实时统计数据)
|
||||
pub async fn get_metadata_stats(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
HttpResponse::Ok().json(db.get_metadata_stats().await)
|
||||
/// GET /api/metadata/stats — 从 search.db 获取统计数据
|
||||
pub async fn get_metadata_stats(search_db: web::Data<Arc<SearchDb>>) -> impl Responder {
|
||||
HttpResponse::Ok().json(search_db.get_metadata_stats().await)
|
||||
}
|
||||
|
||||
/// GET /api/tags/search?tag=xxx — 从缓存按标签搜索相册
|
||||
|
||||
@@ -339,4 +339,30 @@ impl SearchDb {
|
||||
results.sort_by(|a, b| b.total_albums.cmp(&a.total_albums));
|
||||
results
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════
|
||||
// 元数据统计
|
||||
// ═══════════════════════════════════════
|
||||
|
||||
/// 获取全局统计信息
|
||||
pub async fn get_metadata_stats(&self) -> MetadataStats {
|
||||
let total_persons: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM persons")
|
||||
.fetch_one(&self.pool).await.unwrap_or(0);
|
||||
let total_aliases: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM person_aliases")
|
||||
.fetch_one(&self.pool).await.unwrap_or(0);
|
||||
let total_albums: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM albums")
|
||||
.fetch_one(&self.pool).await.unwrap_or(0);
|
||||
let total_images: i64 = sqlx::query_scalar("SELECT COALESCE(SUM(image_count), 0) FROM albums")
|
||||
.fetch_one(&self.pool).await.unwrap_or(0);
|
||||
let total_tags: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM album_tags")
|
||||
.fetch_one(&self.pool).await.unwrap_or(0);
|
||||
|
||||
MetadataStats {
|
||||
total_persons,
|
||||
total_aliases,
|
||||
total_albums,
|
||||
total_images,
|
||||
total_tags,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user