File size: 5,510 Bytes
c6abe34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import os
import sys
import shutil

# Add the parent directory to sys.path to allow importing app
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app.services.supabase_client import get_supabase_service
from app.config import get_settings

async def clear_all_video_data():
    """
    Clears all video files and associated database records for team and personal videos.
    """
    supabase = get_supabase_service()
    settings = get_settings()
    
    print("πŸš€ Starting comprehensive video data cleanup...")
    
    # 1. Fetch all videos to know what files to delete
    print("--- πŸ“Ί Fetching all video records ---")
    videos = await supabase.select("videos")
    print(f"Found {len(videos)} video records.")
    
    # 2. Delete files for each video
    print("--- πŸ“‚ Deleting local and cloud files ---")
    for video in videos:
        video_id = str(video.get("id"))
        storage_path = video.get("storage_path")
        
        # Original video
        if storage_path and os.path.exists(storage_path):
            try:
                os.remove(storage_path)
                print(f"Deleted original: {storage_path}")
            except Exception as e:
                print(f"Error deleting original for {video_id}: {e}")
        
        # Annotated video
        annotated_path = os.path.join("output_videos", "annotated", f"{video_id}.mp4")
        if os.path.exists(annotated_path):
            try:
                os.remove(annotated_path)
                print(f"Deleted annotated: {annotated_path}")
            except Exception as e:
                print(f"Error deleting annotated for {video_id}: {e}")
                
        # Personal analysis outputs
        personal_output_dir = os.path.join("uploads", "personal_output")
        for suffix in ["_output.mp4", "_output.avi", "_report.txt", "_input.mp4", "_input.avi"]:
            personal_path = os.path.join(personal_output_dir, f"{video_id}{suffix}")
            if os.path.exists(personal_path):
                try:
                    os.remove(personal_path)
                    print(f"Deleted personal output: {personal_path}")
                except Exception:
                    pass
        
        # Reports
        reports_dir = "reports"
        report_path = os.path.join(reports_dir, f"{video_id}_report.txt")
        if os.path.exists(report_path):
            try:
                os.remove(report_path)
                print(f"Deleted report: {report_path}")
            except Exception:
                pass

        # Supabase Storage (if connected)
        if supabase.is_connected:
            try:
                # Video bucket
                uploader_id = video.get("uploader_id")
                filename = os.path.basename(storage_path) if storage_path else f"{video_id}.mp4"
                cloud_path = f"{uploader_id}/{filename}"
                await supabase.delete_file("videos", cloud_path)
                
                # Personal analysis bucket
                personal_cloud_path = f"{uploader_id}/{video_id}_output.mp4"
                await supabase.delete_file("personal-analysis-videos", personal_cloud_path)
            except Exception:
                pass

    # 3. Purge entire directories to catch orphaned files
    print("--- 🧹 Purging directories ---")
    dirs_to_purge = [
        os.path.join("uploads", "personal_output"),
        "output_videos/annotated",
        "stubs",
        "reports",
    ]
    
    for d in dirs_to_purge:
        if os.path.exists(d):
            try:
                for f in os.listdir(d):
                    fpath = os.path.join(d, f)
                    if os.path.isfile(fpath):
                        os.remove(fpath)
                    elif os.path.isdir(fpath):
                        shutil.rmtree(fpath)
                print(f"Purged directory: {d}")
            except Exception as e:
                print(f"Error purging {d}: {e}")

    # 4. Clear database tables
    print("--- πŸ—„οΈ  Clearing database tables ---")
    # Tables to clear that are directly video-related
    # Order: Children first if NO CASCADE (but schema has cascade for detections and analysis_results)
    
    # Tables that might NOT have cascade or need manual clearing of video-linked rows
    video_related_tables = [
        "analytics",
        "personal_analyses",
        "activities" # Some activities might be linked to video processing
    ]
    
    for table in video_related_tables:
        try:
            print(f"Clearing video-related records in {table}...")
            # Use neq id 0 as a broad filter to delete everything in these tables
            await supabase._run_sync(lambda: supabase.client.table(table).delete().neq("id", "00000000-0000-0000-0000-000000000000").execute())
            print(f"Successfully cleared {table}")
        except Exception as e:
            print(f"Note: Error clearing {table}: {e}")

    # Finally, clear the main videos table (should cascade to detections and analysis_results)
    try:
        print("Clearing videos table (cascading)...")
        await supabase._run_sync(lambda: supabase.client.table("videos").delete().neq("id", "00000000-0000-0000-0000-000000000000").execute())
        print("Successfully cleared videos table")
    except Exception as e:
        print(f"Error clearing videos table: {e}")

    print("βœ… Cleanup complete.")

if __name__ == "__main__":
    asyncio.run(clear_all_video_data())