|
5 | 5 | import os |
6 | 6 | import logging |
7 | 7 | from typing import Dict, List |
8 | | -from datetime import datetime, timedelta |
| 8 | +from datetime import datetime, timedelta, timezone |
9 | 9 | from sqlalchemy import text, func |
10 | 10 |
|
11 | 11 | from pixelprobe.models import db, ScanResult, ScanReport |
@@ -57,6 +57,49 @@ def get_file_statistics(self) -> Dict: |
57 | 57 | # Fallback to individual queries |
58 | 58 | return self._get_stats_fallback() |
59 | 59 |
|
| 60 | + def get_integrity_coverage(self) -> Dict: |
| 61 | + """Cumulative rolling-integrity coverage. |
| 62 | +
|
| 63 | + The integrity check sweeps the library stalest-first in budgeted |
| 64 | + slices, so no single run report answers "has every file been |
| 65 | + verified?". Coverage counts files by last_integrity_check_date: |
| 66 | + checked ever and within the last 30 days, plus the oldest check date |
| 67 | + (every checked file has been verified at least once since then) and |
| 68 | + how many files have never been checked. |
| 69 | + """ |
| 70 | + try: |
| 71 | + cutoff = datetime.now(timezone.utc) - timedelta(days=30) |
| 72 | + row = db.session.execute( |
| 73 | + text(""" |
| 74 | + SELECT |
| 75 | + COUNT(*) as total_files, |
| 76 | + SUM(CASE WHEN last_integrity_check_date IS NOT NULL THEN 1 ELSE 0 END) as checked_files, |
| 77 | + SUM(CASE WHEN last_integrity_check_date >= :cutoff THEN 1 ELSE 0 END) as checked_last_30_days, |
| 78 | + MIN(last_integrity_check_date) as oldest_check, |
| 79 | + SUM(CASE WHEN bitrot_suspected = TRUE THEN 1 ELSE 0 END) as bitrot_suspected |
| 80 | + FROM scan_results |
| 81 | + """), |
| 82 | + {'cutoff': cutoff} |
| 83 | + ).fetchone() |
| 84 | + |
| 85 | + total = row[0] or 0 |
| 86 | + checked = row[1] or 0 |
| 87 | + oldest = row[3] |
| 88 | + if oldest is not None and hasattr(oldest, 'isoformat'): |
| 89 | + oldest = oldest.isoformat() |
| 90 | + return { |
| 91 | + 'total_files': total, |
| 92 | + 'checked_files': checked, |
| 93 | + 'checked_percent': round(checked / total * 100, 1) if total else 0.0, |
| 94 | + 'checked_last_30_days': row[2] or 0, |
| 95 | + 'never_checked': total - checked, |
| 96 | + 'oldest_check_date': oldest, |
| 97 | + 'bitrot_suspected': row[4] or 0, |
| 98 | + } |
| 99 | + except Exception as e: |
| 100 | + logger.error(f"Error getting integrity coverage: {e}") |
| 101 | + return {} |
| 102 | + |
60 | 103 | def get_system_info(self) -> Dict: |
61 | 104 | """Get comprehensive system information""" |
62 | 105 | try: |
|
0 commit comments