Skip to main content

systemprompt_files/repository/file/
stats.rs

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