smirrors 0.1.0

Automatic mirror list updater for Linux distributions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//! SQLite database operations for SMirrors
//!
//! This module provides persistent storage for mirror information, test results,
//! and update history using SQLite.

use crate::core::{Mirror, TestResult};
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use rusqlite::{params, Connection, OptionalExtension, Row};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tracing::{debug, info};
use url::Url;

/// Database connection pool wrapper
#[derive(Clone)]
pub struct Database {
    conn: Arc<Mutex<Connection>>,
    path: PathBuf,
}

/// Update history record
#[derive(Debug, Clone)]
pub struct UpdateRecord {
    pub id: i64,
    pub mirrors_changed: i64,
    pub success: bool,
    pub error: Option<String>,
    pub updated_at: DateTime<Utc>,
}

/// Filter options for querying test results
#[derive(Debug, Clone, Default)]
pub struct TestResultFilter {
    pub mirror_url: Option<String>,
    pub success_only: bool,
    pub since: Option<DateTime<Utc>>,
    pub until: Option<DateTime<Utc>>,
    pub limit: Option<usize>,
}

/// Filter options for querying mirrors
#[derive(Debug, Clone, Default)]
pub struct MirrorFilter {
    pub static_only: bool,
    pub tested_only: bool,
    pub country: Option<String>,
    pub min_score: Option<f64>,
}

impl Database {
    /// Create a new database connection
    ///
    /// # Arguments
    /// * `path` - Path to the SQLite database file
    ///
    /// # Returns
    /// A new Database instance with initialized schema
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref().to_path_buf();

        // Create parent directory if it doesn't exist
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("Failed to create database directory: {:?}", parent))?;
        }

        let conn = Connection::open(&path)
            .with_context(|| format!("Failed to open database at {:?}", path))?;

        let db = Self {
            conn: Arc::new(Mutex::new(conn)),
            path,
        };

        db.init_schema()?;
        info!("Database initialized at {:?}", db.path);

        Ok(db)
    }

    /// Create an in-memory database (for testing)
    pub fn in_memory() -> Result<Self> {
        let conn = Connection::open_in_memory()
            .context("Failed to open in-memory database")?;

        let db = Self {
            conn: Arc::new(Mutex::new(conn)),
            path: PathBuf::from(":memory:"),
        };

        db.init_schema()?;
        debug!("In-memory database initialized");

        Ok(db)
    }

    /// Initialize database schema with tables and indexes
    fn init_schema(&self) -> Result<()> {
        let conn = self.conn.lock().unwrap();

        conn.execute_batch(
            r#"
            -- Mirrors table
            CREATE TABLE IF NOT EXISTS mirrors (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                url TEXT NOT NULL UNIQUE,
                country TEXT,
                is_static INTEGER NOT NULL DEFAULT 0,
                created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
            );

            -- Test results table
            CREATE TABLE IF NOT EXISTS test_results (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                mirror_id INTEGER NOT NULL,
                speed REAL,
                latency INTEGER,
                score REAL,
                success INTEGER NOT NULL,
                error TEXT,
                tested_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (mirror_id) REFERENCES mirrors (id) ON DELETE CASCADE
            );

            -- Update history table
            CREATE TABLE IF NOT EXISTS updates (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                mirrors_changed INTEGER NOT NULL,
                success INTEGER NOT NULL,
                error TEXT,
                updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
            );

            -- Indexes for performance
            CREATE INDEX IF NOT EXISTS idx_test_results_mirror_id
                ON test_results(mirror_id);
            CREATE INDEX IF NOT EXISTS idx_test_results_tested_at
                ON test_results(tested_at DESC);
            CREATE INDEX IF NOT EXISTS idx_mirrors_url
                ON mirrors(url);
            CREATE INDEX IF NOT EXISTS idx_mirrors_is_static
                ON mirrors(is_static);
            CREATE INDEX IF NOT EXISTS idx_updates_updated_at
                ON updates(updated_at DESC);
            "#
        )
        .context("Failed to initialize database schema")?;

        debug!("Database schema initialized successfully");
        Ok(())
    }

    /// Save or update a mirror in the database
    ///
    /// # Arguments
    /// * `mirror` - The mirror to save
    ///
    /// # Returns
    /// The database ID of the saved mirror
    pub fn save_mirror(&self, mirror: &Mirror) -> Result<i64> {
        let conn = self.conn.lock().unwrap();
        let url = mirror.url.to_string();

        conn.execute(
            r#"
            INSERT INTO mirrors (url, country, is_static)
            VALUES (?1, ?2, ?3)
            ON CONFLICT(url) DO UPDATE SET
                country = excluded.country,
                is_static = excluded.is_static
            "#,
            params![
                url,
                mirror.country.as_deref(),
                mirror.is_static as i32,
            ],
        )
        .with_context(|| format!("Failed to save mirror: {}", url))?;

        let id = conn.last_insert_rowid();
        debug!("Saved mirror {} with ID {}", url, id);

        Ok(id)
    }

    /// Get a mirror by URL
    ///
    /// # Arguments
    /// * `url` - The mirror URL to search for
    ///
    /// # Returns
    /// The mirror if found, None otherwise
    pub fn get_mirror(&self, url: &str) -> Result<Option<Mirror>> {
        let conn = self.conn.lock().unwrap();

        let result = conn
            .query_row(
                "SELECT url, country, is_static FROM mirrors WHERE url = ?1",
                params![url],
                |row| {
                    let url_str: String = row.get(0)?;
                    let country: Option<String> = row.get(1)?;
                    let is_static: i32 = row.get(2)?;

                    Ok((url_str, country, is_static))
                },
            )
            .optional()
            .context("Failed to query mirror")?;

        if let Some((url_str, country, is_static)) = result {
            let url = Url::parse(&url_str)
                .with_context(|| format!("Invalid URL in database: {}", url_str))?;

            let mut mirror = Mirror::new(url);
            mirror.country = country;
            mirror.is_static = is_static != 0;

            // Get latest test results for this mirror
            self.populate_mirror_stats(&conn, &mut mirror)?;

            Ok(Some(mirror))
        } else {
            Ok(None)
        }
    }

    /// Get a mirror's database ID by URL
    fn get_mirror_id(&self, conn: &Connection, url: &str) -> Result<Option<i64>> {
        conn.query_row(
            "SELECT id FROM mirrors WHERE url = ?1",
            params![url],
            |row| row.get(0),
        )
        .optional()
        .context("Failed to get mirror ID")
    }

    /// Populate mirror statistics from latest test result
    fn populate_mirror_stats(&self, conn: &Connection, mirror: &mut Mirror) -> Result<()> {
        let url = mirror.url.to_string();

        let result = conn
            .query_row(
                r#"
                SELECT speed, latency, score, tested_at
                FROM test_results
                WHERE mirror_id = (SELECT id FROM mirrors WHERE url = ?1)
                  AND success = 1
                ORDER BY tested_at DESC
                LIMIT 1
                "#,
                params![url],
                |row| {
                    let speed: Option<f64> = row.get(0)?;
                    let latency_ms: Option<i64> = row.get(1)?;
                    let score: Option<f64> = row.get(2)?;
                    let tested_at: String = row.get(3)?;

                    Ok((speed, latency_ms, score, tested_at))
                },
            )
            .optional()
            .context("Failed to query mirror stats")?;

        if let Some((speed, latency_ms, score, tested_at_str)) = result {
            mirror.speed = speed;
            mirror.latency = latency_ms.map(|ms| Duration::from_millis(ms as u64));
            mirror.score = score;

            if let Ok(tested_at) = DateTime::parse_from_rfc3339(&tested_at_str) {
                mirror.last_tested = Some(tested_at.with_timezone(&Utc));
            }
        }

        Ok(())
    }

    /// Save a test result to the database
    ///
    /// # Arguments
    /// * `result` - The test result to save
    pub fn save_test_result(&self, result: &TestResult) -> Result<()> {
        let conn = self.conn.lock().unwrap();
        let url = result.mirror.url.to_string();

        // Ensure mirror exists
        let mirror_id = match self.get_mirror_id(&conn, &url)? {
            Some(id) => id,
            None => {
                // Insert mirror first
                conn.execute(
                    "INSERT INTO mirrors (url, country, is_static) VALUES (?1, ?2, ?3)",
                    params![
                        url,
                        result.mirror.country.as_deref(),
                        result.mirror.is_static as i32,
                    ],
                )
                .context("Failed to insert mirror for test result")?;

                conn.last_insert_rowid()
            }
        };

        // Insert test result
        conn.execute(
            r#"
            INSERT INTO test_results
                (mirror_id, speed, latency, score, success, error, tested_at)
            VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
            "#,
            params![
                mirror_id,
                result.speed,
                result.latency.map(|d| d.as_millis() as i64),
                result.score,
                result.success as i32,
                result.error.as_deref(),
                result.tested_at.to_rfc3339(),
            ],
        )
        .with_context(|| format!("Failed to save test result for mirror: {}", url))?;

        debug!("Saved test result for mirror: {}", url);
        Ok(())
    }

    /// Get test results with optional filtering
    ///
    /// # Arguments
    /// * `filter` - Filter options for the query
    ///
    /// # Returns
    /// Vector of test results matching the filter
    pub fn get_test_results(&self, filter: &TestResultFilter) -> Result<Vec<TestResult>> {
        let conn = self.conn.lock().unwrap();

        let mut query = String::from(
            r#"
            SELECT
                m.url, m.country, m.is_static,
                tr.speed, tr.latency, tr.score,
                tr.success, tr.error, tr.tested_at
            FROM test_results tr
            JOIN mirrors m ON tr.mirror_id = m.id
            WHERE 1=1
            "#,
        );

        let mut conditions = Vec::new();

        if filter.mirror_url.is_some() {
            conditions.push("m.url = ?");
        }
        if filter.success_only {
            conditions.push("tr.success = 1");
        }
        if filter.since.is_some() {
            conditions.push("tr.tested_at >= ?");
        }
        if filter.until.is_some() {
            conditions.push("tr.tested_at <= ?");
        }

        for condition in conditions {
            query.push_str(" AND ");
            query.push_str(condition);
        }

        query.push_str(" ORDER BY tr.tested_at DESC");

        if let Some(limit) = filter.limit {
            query.push_str(&format!(" LIMIT {}", limit));
        }

        let mut stmt = conn.prepare(&query)
            .context("Failed to prepare test results query")?;

        let mut dynamic_params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();

        if let Some(ref url) = filter.mirror_url {
            dynamic_params.push(Box::new(url.clone()));
        }
        if let Some(since) = filter.since {
            dynamic_params.push(Box::new(since.to_rfc3339()));
        }
        if let Some(until) = filter.until {
            dynamic_params.push(Box::new(until.to_rfc3339()));
        }

        let params_refs: Vec<&dyn rusqlite::ToSql> = dynamic_params
            .iter()
            .map(|p| p.as_ref())
            .collect();

        let results = stmt
            .query_map(params_refs.as_slice(), Self::row_to_test_result)?
            .collect::<std::result::Result<Vec<_>, _>>()
            .context("Failed to map test results")?;

        debug!("Retrieved {} test results", results.len());
        Ok(results)
    }

    /// Convert a database row to a TestResult
    fn row_to_test_result(row: &Row) -> rusqlite::Result<TestResult> {
        let url_str: String = row.get(0)?;
        let country: Option<String> = row.get(1)?;
        let is_static: i32 = row.get(2)?;
        let speed: Option<f64> = row.get(3)?;
        let latency_ms: Option<i64> = row.get(4)?;
        let score: Option<f64> = row.get(5)?;
        let success: i32 = row.get(6)?;
        let error: Option<String> = row.get(7)?;
        let tested_at_str: String = row.get(8)?;

        let url = Url::parse(&url_str)
            .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;

        let mut mirror = Mirror::new(url);
        mirror.country = country;
        mirror.is_static = is_static != 0;
        mirror.speed = speed;
        mirror.latency = latency_ms.map(|ms| Duration::from_millis(ms as u64));
        mirror.score = score;

        let tested_at = DateTime::parse_from_rfc3339(&tested_at_str)
            .map(|dt| dt.with_timezone(&Utc))
            .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;

        Ok(TestResult {
            mirror,
            success: success != 0,
            speed,
            latency: latency_ms.map(|ms| Duration::from_millis(ms as u64)),
            score,
            error,
            tested_at,
        })
    }

    /// Save an update record to the database
    ///
    /// # Arguments
    /// * `mirrors_changed` - Number of mirrors that were changed
    /// * `success` - Whether the update was successful
    /// * `error` - Optional error message if update failed
    pub fn save_update_record(
        &self,
        mirrors_changed: i64,
        success: bool,
        error: Option<String>,
    ) -> Result<i64> {
        let conn = self.conn.lock().unwrap();

        conn.execute(
            r#"
            INSERT INTO updates (mirrors_changed, success, error, updated_at)
            VALUES (?1, ?2, ?3, ?4)
            "#,
            params![
                mirrors_changed,
                success as i32,
                error.as_deref(),
                Utc::now().to_rfc3339(),
            ],
        )
        .context("Failed to save update record")?;

        let id = conn.last_insert_rowid();
        info!(
            "Saved update record: {} mirrors changed, success: {}",
            mirrors_changed, success
        );

        Ok(id)
    }

    /// Get update history with optional limit
    ///
    /// # Arguments
    /// * `limit` - Maximum number of records to return
    ///
    /// # Returns
    /// Vector of update records ordered by date (newest first)
    pub fn get_history(&self, limit: usize) -> Result<Vec<UpdateRecord>> {
        let conn = self.conn.lock().unwrap();

        let mut stmt = conn.prepare(
            r#"
            SELECT id, mirrors_changed, success, error, updated_at
            FROM updates
            ORDER BY updated_at DESC
            LIMIT ?1
            "#,
        )
        .context("Failed to prepare history query")?;

        let records = stmt
            .query_map(params![limit], |row| {
                let id: i64 = row.get(0)?;
                let mirrors_changed: i64 = row.get(1)?;
                let success: i32 = row.get(2)?;
                let error: Option<String> = row.get(3)?;
                let updated_at_str: String = row.get(4)?;

                let updated_at = DateTime::parse_from_rfc3339(&updated_at_str)
                    .map(|dt| dt.with_timezone(&Utc))
                    .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;

                Ok(UpdateRecord {
                    id,
                    mirrors_changed,
                    success: success != 0,
                    error,
                    updated_at,
                })
            })?
            .collect::<std::result::Result<Vec<_>, _>>()
            .context("Failed to map update records")?;

        debug!("Retrieved {} update records", records.len());
        Ok(records)
    }

    /// Get all mirrors with optional filtering
    ///
    /// # Arguments
    /// * `filter` - Filter options for the query
    ///
    /// # Returns
    /// Vector of mirrors matching the filter
    pub fn get_mirrors(&self, filter: &MirrorFilter) -> Result<Vec<Mirror>> {
        let conn = self.conn.lock().unwrap();

        let mut query = String::from("SELECT DISTINCT m.url, m.country, m.is_static FROM mirrors m");
        let mut conditions = Vec::new();

        if filter.tested_only {
            query.push_str(" JOIN test_results tr ON m.id = tr.mirror_id");
            conditions.push("tr.success = 1");
        }

        if filter.static_only {
            conditions.push("m.is_static = 1");
        }

        if !conditions.is_empty() {
            query.push_str(" WHERE ");
            query.push_str(&conditions.join(" AND "));
        }

        query.push_str(" ORDER BY m.url");

        let mut stmt = conn.prepare(&query)
            .context("Failed to prepare mirrors query")?;

        let mirrors = stmt
            .query_map([], |row| {
                let url_str: String = row.get(0)?;
                let country: Option<String> = row.get(1)?;
                let is_static: i32 = row.get(2)?;

                Ok((url_str, country, is_static))
            })?
            .collect::<std::result::Result<Vec<_>, _>>()
            .context("Failed to map mirrors")?;

        let mut result = Vec::new();

        for (url_str, country, is_static) in mirrors {
            let url = Url::parse(&url_str)
                .with_context(|| format!("Invalid URL in database: {}", url_str))?;

            let mut mirror = Mirror::new(url);
            mirror.country = country;
            mirror.is_static = is_static != 0;

            // Populate stats
            self.populate_mirror_stats(&conn, &mut mirror)?;

            // Apply score filter if specified
            if let Some(min_score) = filter.min_score {
                if let Some(score) = mirror.score {
                    if score < min_score {
                        continue;
                    }
                } else {
                    continue;
                }
            }

            // Apply country filter if specified
            if let Some(ref filter_country) = filter.country {
                if mirror.country.as_ref() != Some(filter_country) {
                    continue;
                }
            }

            result.push(mirror);
        }

        debug!("Retrieved {} mirrors", result.len());
        Ok(result)
    }

    /// Delete old test results to keep database size manageable
    ///
    /// # Arguments
    /// * `older_than_days` - Delete results older than this many days
    ///
    /// # Returns
    /// Number of deleted records
    pub fn delete_old_results(&self, older_than_days: i64) -> Result<usize> {
        let conn = self.conn.lock().unwrap();

        let cutoff = Utc::now() - chrono::Duration::days(older_than_days);

        let deleted = conn.execute(
            "DELETE FROM test_results WHERE tested_at < ?1",
            params![cutoff.to_rfc3339()],
        )
        .context("Failed to delete old test results")?;

        if deleted > 0 {
            info!("Deleted {} old test results", deleted);
        }

        Ok(deleted)
    }

    /// Get the most recent update record
    ///
    /// # Returns
    /// The latest update record if any exists
    pub fn get_latest_update(&self) -> Result<Option<UpdateRecord>> {
        let conn = self.conn.lock().unwrap();

        let result = conn
            .query_row(
                r#"
                SELECT id, mirrors_changed, success, error, updated_at
                FROM updates
                ORDER BY updated_at DESC
                LIMIT 1
                "#,
                [],
                |row| {
                    let id: i64 = row.get(0)?;
                    let mirrors_changed: i64 = row.get(1)?;
                    let success: i32 = row.get(2)?;
                    let error: Option<String> = row.get(3)?;
                    let updated_at_str: String = row.get(4)?;

                    Ok((id, mirrors_changed, success, error, updated_at_str))
                },
            )
            .optional()
            .context("Failed to query latest update")?;

        if let Some((id, mirrors_changed, success, error, updated_at_str)) = result {
            let updated_at = DateTime::parse_from_rfc3339(&updated_at_str)
                .map(|dt| dt.with_timezone(&Utc))
                .context("Failed to parse update timestamp")?;

            Ok(Some(UpdateRecord {
                id,
                mirrors_changed,
                success: success != 0,
                error,
                updated_at,
            }))
        } else {
            Ok(None)
        }
    }

    /// Get database file path
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Vacuum the database to reclaim space
    pub fn vacuum(&self) -> Result<()> {
        let conn = self.conn.lock().unwrap();
        conn.execute_batch("VACUUM")
            .context("Failed to vacuum database")?;
        info!("Database vacuumed successfully");
        Ok(())
    }

    /// Get database statistics
    pub fn get_stats(&self) -> Result<DatabaseStats> {
        let conn = self.conn.lock().unwrap();

        let mirrors_count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM mirrors",
            [],
            |row| row.get(0),
        )?;

        let test_results_count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM test_results",
            [],
            |row| row.get(0),
        )?;

        let updates_count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM updates",
            [],
            |row| row.get(0),
        )?;

        let static_mirrors_count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM mirrors WHERE is_static = 1",
            [],
            |row| row.get(0),
        )?;

        Ok(DatabaseStats {
            mirrors_count: mirrors_count as usize,
            test_results_count: test_results_count as usize,
            updates_count: updates_count as usize,
            static_mirrors_count: static_mirrors_count as usize,
        })
    }
}

/// Database statistics
#[derive(Debug, Clone)]
pub struct DatabaseStats {
    pub mirrors_count: usize,
    pub test_results_count: usize,
    pub updates_count: usize,
    pub static_mirrors_count: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_mirror(url: &str) -> Mirror {
        let url = Url::parse(url).unwrap();
        Mirror::new(url)
    }

    #[test]
    fn test_database_creation() {
        let db = Database::in_memory().unwrap();
        assert_eq!(db.path(), Path::new(":memory:"));
    }

    #[test]
    fn test_save_and_get_mirror() {
        let db = Database::in_memory().unwrap();
        let mirror = create_test_mirror("https://mirror.example.com/debian");

        db.save_mirror(&mirror).unwrap();

        let retrieved = db.get_mirror("https://mirror.example.com/debian").unwrap();
        assert!(retrieved.is_some());

        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.url, mirror.url);
    }

    #[test]
    fn test_save_static_mirror() {
        let db = Database::in_memory().unwrap();
        let url = Url::parse("https://static.example.com/repo").unwrap();
        let mirror = Mirror::new_static(url);

        db.save_mirror(&mirror).unwrap();

        let retrieved = db.get_mirror("https://static.example.com/repo").unwrap().unwrap();
        assert!(retrieved.is_static);
    }

    #[test]
    fn test_save_test_result() {
        let db = Database::in_memory().unwrap();
        let mirror = create_test_mirror("https://mirror.example.com/debian");

        let result = TestResult::success(
            mirror,
            50.0,
            Duration::from_millis(100),
            0.75,
        );

        db.save_test_result(&result).unwrap();

        let filter = TestResultFilter {
            mirror_url: Some("https://mirror.example.com/debian".to_string()),
            ..Default::default()
        };

        let results = db.get_test_results(&filter).unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert_eq!(results[0].speed, Some(50.0));
    }

    #[test]
    fn test_get_test_results_with_filter() {
        let db = Database::in_memory().unwrap();

        let mirror1 = create_test_mirror("https://mirror1.example.com/repo");
        let mirror2 = create_test_mirror("https://mirror2.example.com/repo");

        db.save_test_result(&TestResult::success(mirror1, 50.0, Duration::from_millis(100), 0.75)).unwrap();
        db.save_test_result(&TestResult::failure(mirror2, "Test error".to_string())).unwrap();

        let filter = TestResultFilter {
            success_only: true,
            ..Default::default()
        };

        let results = db.get_test_results(&filter).unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].success);
    }

    #[test]
    fn test_save_update_record() {
        let db = Database::in_memory().unwrap();

        db.save_update_record(5, true, None).unwrap();

        let history = db.get_history(10).unwrap();
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].mirrors_changed, 5);
        assert!(history[0].success);
    }

    #[test]
    fn test_get_latest_update() {
        let db = Database::in_memory().unwrap();

        db.save_update_record(3, true, None).unwrap();
        std::thread::sleep(Duration::from_millis(10));
        db.save_update_record(5, true, None).unwrap();

        let latest = db.get_latest_update().unwrap();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().mirrors_changed, 5);
    }

    #[test]
    fn test_get_mirrors_with_filter() {
        let db = Database::in_memory().unwrap();

        let mirror1 = create_test_mirror("https://mirror1.example.com/repo");
        let url2 = Url::parse("https://mirror2.example.com/repo").unwrap();
        let mirror2 = Mirror::new_static(url2);

        db.save_mirror(&mirror1).unwrap();
        db.save_mirror(&mirror2).unwrap();

        let filter = MirrorFilter {
            static_only: true,
            ..Default::default()
        };

        let mirrors = db.get_mirrors(&filter).unwrap();
        assert_eq!(mirrors.len(), 1);
        assert!(mirrors[0].is_static);
    }

    #[test]
    fn test_delete_old_results() {
        let db = Database::in_memory().unwrap();
        let mirror = create_test_mirror("https://mirror.example.com/repo");

        db.save_test_result(&TestResult::success(mirror, 50.0, Duration::from_millis(100), 0.75)).unwrap();

        // Delete results older than 0 days (should delete all)
        let deleted = db.delete_old_results(0).unwrap();
        assert!(deleted > 0);

        let results = db.get_test_results(&TestResultFilter::default()).unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_database_stats() {
        let db = Database::in_memory().unwrap();

        let mirror1 = create_test_mirror("https://mirror1.example.com/repo");
        let mirror2 = create_test_mirror("https://mirror2.example.com/repo");

        db.save_mirror(&mirror1).unwrap();
        db.save_mirror(&mirror2).unwrap();
        db.save_test_result(&TestResult::success(mirror1, 50.0, Duration::from_millis(100), 0.75)).unwrap();
        db.save_update_record(2, true, None).unwrap();

        let stats = db.get_stats().unwrap();
        assert_eq!(stats.mirrors_count, 2);
        assert_eq!(stats.test_results_count, 1);
        assert_eq!(stats.updates_count, 1);
    }

    #[test]
    fn test_populate_mirror_stats() {
        let db = Database::in_memory().unwrap();
        let mirror = create_test_mirror("https://mirror.example.com/repo");

        db.save_test_result(&TestResult::success(
            mirror.clone(),
            75.5,
            Duration::from_millis(50),
            0.85,
        )).unwrap();

        let retrieved = db.get_mirror("https://mirror.example.com/repo").unwrap().unwrap();

        assert_eq!(retrieved.speed, Some(75.5));
        assert_eq!(retrieved.latency, Some(Duration::from_millis(50)));
        assert_eq!(retrieved.score, Some(0.85));
        assert!(retrieved.last_tested.is_some());
    }
}