systemprompt_files/repository/file/
stats.rs1use super::FileRepository;
2use crate::error::FilesResult;
3
4#[derive(Debug, Clone, Copy)]
5pub struct FileStats {
6 pub total_files: i64,
7 pub total_size_bytes: i64,
8 pub ai_images_count: i64,
9 pub image_count: i64,
10 pub image_size_bytes: i64,
11 pub document_count: i64,
12 pub document_size_bytes: i64,
13 pub audio_count: i64,
14 pub audio_size_bytes: i64,
15 pub video_count: i64,
16 pub video_size_bytes: i64,
17 pub other_count: i64,
18 pub other_size_bytes: i64,
19}
20
21impl FileRepository {
22 pub async fn get_stats(&self) -> FilesResult<FileStats> {
23 let row = sqlx::query!(
24 r#"
25 SELECT
26 COUNT(*) as "total_files!",
27 COALESCE(SUM(size_bytes), 0)::bigint as "total_size_bytes!",
28 COUNT(*) FILTER (WHERE ai_content = true) as "ai_images_count!",
29 COUNT(*) FILTER (WHERE mime_type LIKE 'image/%') as "image_count!",
30 COALESCE(SUM(size_bytes) FILTER (WHERE mime_type LIKE 'image/%'), 0)::bigint as "image_size!",
31 COUNT(*) FILTER (WHERE mime_type LIKE 'application/pdf' OR mime_type LIKE 'application/msword%' OR mime_type LIKE 'application/vnd.openxmlformats%' OR mime_type LIKE 'text/%') as "document_count!",
32 COALESCE(SUM(size_bytes) FILTER (WHERE mime_type LIKE 'application/pdf' OR mime_type LIKE 'application/msword%' OR mime_type LIKE 'application/vnd.openxmlformats%' OR mime_type LIKE 'text/%'), 0)::bigint as "document_size!",
33 COUNT(*) FILTER (WHERE mime_type LIKE 'audio/%') as "audio_count!",
34 COALESCE(SUM(size_bytes) FILTER (WHERE mime_type LIKE 'audio/%'), 0)::bigint as "audio_size!",
35 COUNT(*) FILTER (WHERE mime_type LIKE 'video/%') as "video_count!",
36 COALESCE(SUM(size_bytes) FILTER (WHERE mime_type LIKE 'video/%'), 0)::bigint as "video_size!"
37 FROM files
38 WHERE deleted_at IS NULL
39 "#
40 )
41 .fetch_one(self.pool.as_ref())
42 .await?;
43
44 let image_count = row.image_count;
45 let document_count = row.document_count;
46 let audio_count = row.audio_count;
47 let video_count = row.video_count;
48 let other_count =
49 (row.total_files - image_count - document_count - audio_count - video_count).max(0);
50
51 let image_size = row.image_size;
52 let document_size = row.document_size;
53 let audio_size = row.audio_size;
54 let video_size = row.video_size;
55 let other_size =
56 (row.total_size_bytes - image_size - document_size - audio_size - video_size).max(0);
57
58 Ok(FileStats {
59 total_files: row.total_files,
60 total_size_bytes: row.total_size_bytes,
61 ai_images_count: row.ai_images_count,
62 image_count,
63 image_size_bytes: image_size,
64 document_count,
65 document_size_bytes: document_size,
66 audio_count,
67 audio_size_bytes: audio_size,
68 video_count,
69 video_size_bytes: video_size,
70 other_count,
71 other_size_bytes: other_size,
72 })
73 }
74}