-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinteractive_batch_processor.py
More file actions
1479 lines (1219 loc) · 61.8 KB
/
interactive_batch_processor.py
File metadata and controls
1479 lines (1219 loc) · 61.8 KB
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Interactive Batch Processing with Content Preview
Part of AI File Organizer v3.1 - Adaptive Emergency Prevention System
Provides ADHD-friendly batch processing with:
- Content preview before organization decisions
- Human-in-the-loop batch processing
- Smart grouping and similarity detection
- Confidence-based automatic vs manual processing
- Preview-based decision making
Created by: RT Max / Claude Code
"""
import os
import sys
import time
from pathlib import Path
from datetime import datetime, timedelta
from typing import Dict, List, Set, Optional, Any, Tuple
import sqlite3
import json
import logging
import hashlib
from dataclasses import dataclass, asdict
from collections import defaultdict
import re
# Import existing system components
project_dir = Path(__file__).parent
sys.path.insert(0, str(project_dir))
from universal_adaptive_learning import UniversalAdaptiveLearning
from confidence_system import ADHDFriendlyConfidenceSystem, ConfidenceLevel
from content_extractor import ContentExtractor
from gdrive_integration import get_ai_organizer_root, get_metadata_root
from easy_rollback_system import EasyRollbackSystem
@dataclass
class FilePreview:
"""Preview information for a file"""
file_path: str
file_name: str
file_size_mb: float
file_type: str
creation_date: datetime
modification_date: datetime
content_preview: str
content_keywords: List[str]
content_summary: str
predicted_category: str
confidence_score: float
similar_files: List[str]
duplicate_indicator: bool
@dataclass
class BatchGroup:
"""A group of similar files for batch processing"""
group_id: str
group_type: str # "similar_content", "same_type", "same_source", "duplicates"
group_name: str
file_previews: List[FilePreview]
common_characteristics: Dict[str, Any]
suggested_action: str
confidence_level: ConfidenceLevel
batch_size: int
@dataclass
class BatchOperation:
"""A batch operation to be performed"""
operation_id: str
operation_type: str # "organize", "deduplicate", "archive", "delete"
target_location: str
affected_files: List[str]
confidence_score: float
user_approved: bool
execution_time: Optional[datetime] = None
success: Optional[bool] = None
class InteractiveBatchProcessor:
"""
Interactive batch processor with content preview capabilities
Designed for ADHD users to make informed decisions about file organization
through visual previews and intelligent grouping
"""
def __init__(self, base_dir: str = None):
self.base_dir = Path(base_dir) if base_dir else get_ai_organizer_root()
# Initialize components
self.learning_system = UniversalAdaptiveLearning(str(self.base_dir))
self.confidence_system = ADHDFriendlyConfidenceSystem(str(self.base_dir))
self.content_extractor = ContentExtractor(str(self.base_dir))
self.rollback_system = EasyRollbackSystem()
# Batch processing database
self.batch_db_path = get_metadata_root() / "batch_processing.db"
# Configuration
self.config = {
"max_batch_size": 50, # Maximum files per batch
"preview_length": 500, # Characters in content preview
"similarity_threshold": 0.7, # Similarity threshold for grouping
"auto_process_threshold": 0.85, # Confidence for automatic processing
"keyword_limit": 10, # Maximum keywords per file
"group_by_preferences": [
"duplicates",
"similar_content",
"same_type",
"same_source",
"same_date"
]
}
# Content preview cache
self.preview_cache = {}
# Active batch sessions
self.active_sessions = {}
# Statistics
self.stats = {
"batches_processed": 0,
"files_organized": 0,
"preview_cache_hits": 0,
"user_decisions": 0,
"automatic_decisions": 0,
"confidence_improvements": 0
}
# Initialize database
self._init_batch_database()
# Set up logging
self.logger = logging.getLogger(__name__)
self.logger.info("Interactive Batch Processor initialized")
def _init_batch_database(self):
"""Initialize batch processing database"""
self.batch_db_path.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(self.batch_db_path) as conn:
# File previews cache
conn.execute("""
CREATE TABLE IF NOT EXISTS file_previews (
file_path TEXT PRIMARY KEY,
file_hash TEXT,
preview_data TEXT,
generated_time TEXT,
last_accessed TEXT,
access_count INTEGER DEFAULT 1
)
""")
# Batch sessions
conn.execute("""
CREATE TABLE IF NOT EXISTS batch_sessions (
session_id TEXT PRIMARY KEY,
start_time TEXT,
end_time TEXT,
source_directory TEXT,
total_files INTEGER,
groups_created INTEGER,
files_processed INTEGER,
user_decisions INTEGER,
automatic_decisions INTEGER,
success_rate REAL
)
""")
# Batch operations
conn.execute("""
CREATE TABLE IF NOT EXISTS batch_operations (
operation_id TEXT PRIMARY KEY,
session_id TEXT,
operation_type TEXT,
target_location TEXT,
affected_files TEXT,
confidence_score REAL,
user_approved BOOLEAN,
execution_time TEXT,
success BOOLEAN,
error_message TEXT
)
""")
# User feedback
conn.execute("""
CREATE TABLE IF NOT EXISTS user_feedback (
feedback_id TEXT PRIMARY KEY,
session_id TEXT,
file_path TEXT,
predicted_action TEXT,
user_action TEXT,
satisfaction_score INTEGER,
feedback_time TEXT,
comments TEXT
)
""")
conn.commit()
def start_batch_session(self, source_directory: str, session_name: str = None) -> str:
"""
Start a new batch processing session
Args:
source_directory: Directory to process
session_name: Optional name for the session
Returns:
Session ID
"""
source_path = Path(source_directory)
if not source_path.exists():
raise ValueError(f"Source directory does not exist: {source_directory}")
# Generate session ID
session_id = hashlib.md5(f"{source_directory}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
# Scan directory for files
files_found = self._scan_directory_for_files(source_path)
if not files_found:
raise ValueError(f"No processable files found in {source_directory}")
self.logger.info(f"Starting batch session {session_id} with {len(files_found)} files")
# Create file previews
file_previews = []
# OPTIMIZATION: Reuse database connection for batch processing
# This prevents N+1 connection overhead (opens 1 connection instead of 2*N)
try:
with sqlite3.connect(self.batch_db_path) as conn:
# Also reuse content index connection
content_db_path = self.content_extractor.db_path
with sqlite3.connect(content_db_path) as content_conn:
for file_path in files_found:
preview = self._generate_file_preview(
file_path,
db_connection=conn,
content_db_connection=content_conn
)
if preview:
file_previews.append(preview)
# Group files intelligently (CPU bound, no DB)
batch_groups = self._create_intelligent_groups(file_previews)
# Create session
session_data = {
"session_id": session_id,
"session_name": session_name or f"Batch_{datetime.now().strftime('%Y%m%d_%H%M')}",
"start_time": datetime.now(),
"source_directory": str(source_path),
"total_files": len(files_found),
"file_previews": file_previews,
"batch_groups": batch_groups,
"current_group_index": 0,
"processed_groups": [],
"pending_operations": [],
"user_decisions": [],
"status": "active"
}
self.active_sessions[session_id] = session_data
# Record session in database (reusing connection)
self._record_batch_session(session_data, db_connection=conn)
except Exception as e:
self.logger.error(f"Error in batch session initialization: {e}")
raise
self.logger.info(f"Batch session {session_id} created with {len(batch_groups)} groups")
return session_id
def get_session_overview(self, session_id: str) -> Dict[str, Any]:
"""Get overview of batch session"""
if session_id not in self.active_sessions:
raise ValueError(f"Session {session_id} not found")
session = self.active_sessions[session_id]
return {
"session_id": session_id,
"session_name": session["session_name"],
"source_directory": session["source_directory"],
"total_files": session["total_files"],
"total_groups": len(session["batch_groups"]),
"current_group": session["current_group_index"],
"processed_groups": len(session["processed_groups"]),
"pending_operations": len(session["pending_operations"]),
"status": session["status"],
"progress_percent": (len(session["processed_groups"]) / len(session["batch_groups"]) * 100) if session["batch_groups"] else 0
}
def get_next_group_for_review(self, session_id: str) -> Optional[Dict[str, Any]]:
"""Get the next group for user review"""
if session_id not in self.active_sessions:
raise ValueError(f"Session {session_id} not found")
session = self.active_sessions[session_id]
if session["current_group_index"] >= len(session["batch_groups"]):
return None # No more groups
current_group = session["batch_groups"][session["current_group_index"]]
# Check if this group can be processed automatically
if (current_group.confidence_level in [ConfidenceLevel.ALWAYS] or
(current_group.confidence_level == ConfidenceLevel.SMART and
max(fp.confidence_score for fp in current_group.file_previews) >= self.config["auto_process_threshold"])):
# Process automatically
self._process_group_automatically(session_id, current_group)
session["current_group_index"] += 1
return self.get_next_group_for_review(session_id) # Get next group
# Prepare group for user review
group_data = self._prepare_group_for_review(current_group)
return group_data
def process_user_decision(self, session_id: str, group_id: str, user_decision: Dict[str, Any]) -> Dict[str, Any]:
"""
Process user decision for a group
Args:
session_id: Session ID
group_id: Group ID
user_decision: User's decision data
Returns:
Processing result
"""
if session_id not in self.active_sessions:
raise ValueError(f"Session {session_id} not found")
session = self.active_sessions[session_id]
current_group = session["batch_groups"][session["current_group_index"]]
if current_group.group_id != group_id:
raise ValueError(f"Group ID mismatch: expected {current_group.group_id}, got {group_id}")
# Process the decision
result = self._execute_user_decision(session_id, current_group, user_decision)
# Record user decision for learning
self._record_user_decision(session_id, current_group, user_decision, result)
# Update learning system
self._update_learning_from_decision(current_group, user_decision)
# Move to next group
session["processed_groups"].append(current_group.group_id)
session["current_group_index"] += 1
session["user_decisions"].append({
"group_id": group_id,
"decision": user_decision,
"timestamp": datetime.now().isoformat()
})
self.stats["user_decisions"] += 1
return result
def _scan_directory_for_files(self, directory: Path) -> List[Path]:
"""Scan directory for processable files"""
files = []
supported_extensions = {'.pdf', '.docx', '.doc', '.txt', '.md', '.pages', '.rtf',
'.jpg', '.png', '.gif', '.jpeg', '.mp4', '.mov', '.avi',
'.mp3', '.wav', '.m4a', '.ipynb', '.json', '.csv', '.xlsx'}
try:
for file_path in directory.rglob('*'):
if (file_path.is_file() and
file_path.suffix.lower() in supported_extensions and
not file_path.name.startswith('.')):
files.append(file_path)
except Exception as e:
self.logger.error(f"Error scanning directory {directory}: {e}")
return files
def _generate_file_preview(self, file_path: Path, db_connection: Optional[sqlite3.Connection] = None, content_db_connection: Optional[sqlite3.Connection] = None) -> Optional[FilePreview]:
"""Generate preview for a file"""
try:
# Check cache first
file_hash = self._calculate_file_hash(file_path)
cached_preview = self._get_cached_preview(str(file_path), file_hash, db_connection)
if cached_preview:
self.stats["preview_cache_hits"] += 1
return cached_preview
# Generate new preview
stat_info = file_path.stat()
# Extract content
content_preview = ""
content_keywords = []
content_summary = ""
try:
if file_path.suffix.lower() in ['.txt', '.md', '.py', '.js', '.css', '.html']:
# Read text files directly
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read(self.config["preview_length"] * 2)
content_preview = content[:self.config["preview_length"]]
content_keywords = self._extract_keywords(content)
content_summary = self._generate_content_summary(content)
elif file_path.suffix.lower() in ['.pdf', '.docx', '.doc', '.pages', '.rtf']:
# Use content extractor for documents
extraction_result = self.content_extractor.extract_content(file_path, db_connection=content_db_connection)
if extraction_result['success']:
content = extraction_result['text']
content_preview = content[:self.config["preview_length"]]
content_keywords = self._extract_keywords(content)
content_summary = self._generate_content_summary(content)
except Exception as e:
self.logger.warning(f"Could not extract content from {file_path}: {e}")
# Get prediction from learning system
prediction = self.learning_system.predict_user_action(
str(file_path),
{"content_keywords": content_keywords}
)
predicted_category = prediction.get("predicted_action", {}).get("target_category", "unknown")
confidence_score = prediction.get("confidence", 0.0)
# Check for similar files
similar_files = self._find_similar_files(file_path, content_keywords)
# Check for duplicates
duplicate_indicator = self._is_likely_duplicate(file_path)
preview = FilePreview(
file_path=str(file_path),
file_name=file_path.name,
file_size_mb=stat_info.st_size / (1024 * 1024),
file_type=file_path.suffix.lower(),
creation_date=datetime.fromtimestamp(stat_info.st_ctime),
modification_date=datetime.fromtimestamp(stat_info.st_mtime),
content_preview=content_preview,
content_keywords=content_keywords,
content_summary=content_summary,
predicted_category=predicted_category,
confidence_score=confidence_score,
similar_files=similar_files,
duplicate_indicator=duplicate_indicator
)
# Cache the preview
self._cache_preview(str(file_path), file_hash, preview, db_connection)
return preview
except Exception as e:
self.logger.error(f"Error generating preview for {file_path}: {e}")
return None
def _create_intelligent_groups(self, file_previews: List[FilePreview]) -> List[BatchGroup]:
"""Create intelligent groups of similar files"""
groups = []
remaining_files = file_previews.copy()
# Group by priority order
for group_type in self.config["group_by_preferences"]:
if group_type == "duplicates":
duplicate_groups = self._group_by_duplicates(remaining_files)
groups.extend(duplicate_groups)
# Remove grouped files
grouped_files = {fp.file_path for group in duplicate_groups for fp in group.file_previews}
remaining_files = [fp for fp in remaining_files if fp.file_path not in grouped_files]
elif group_type == "similar_content":
content_groups = self._group_by_similar_content(remaining_files)
groups.extend(content_groups)
# Remove grouped files
grouped_files = {fp.file_path for group in content_groups for fp in group.file_previews}
remaining_files = [fp for fp in remaining_files if fp.file_path not in grouped_files]
elif group_type == "same_type":
type_groups = self._group_by_file_type(remaining_files)
groups.extend(type_groups)
# Remove grouped files
grouped_files = {fp.file_path for group in type_groups for fp in group.file_previews}
remaining_files = [fp for fp in remaining_files if fp.file_path not in grouped_files]
elif group_type == "same_source":
source_groups = self._group_by_source_directory(remaining_files)
groups.extend(source_groups)
# Remove grouped files
grouped_files = {fp.file_path for group in source_groups for fp in group.file_previews}
remaining_files = [fp for fp in remaining_files if fp.file_path not in grouped_files]
elif group_type == "same_date":
date_groups = self._group_by_date(remaining_files)
groups.extend(date_groups)
# Remove grouped files
grouped_files = {fp.file_path for group in date_groups for fp in group.file_previews}
remaining_files = [fp for fp in remaining_files if fp.file_path not in grouped_files]
# Create individual groups for remaining files
for file_preview in remaining_files:
individual_group = self._create_individual_group(file_preview)
groups.append(individual_group)
# Ensure no group exceeds max batch size
final_groups = []
for group in groups:
if len(group.file_previews) <= self.config["max_batch_size"]:
final_groups.append(group)
else:
# Split large groups
split_groups = self._split_large_group(group)
final_groups.extend(split_groups)
return final_groups
def _group_by_duplicates(self, file_previews: List[FilePreview]) -> List[BatchGroup]:
"""Group files that appear to be duplicates"""
groups = []
duplicate_candidates = [fp for fp in file_previews if fp.duplicate_indicator]
if not duplicate_candidates:
return groups
# Group by similar file names and sizes
name_size_groups = defaultdict(list)
for fp in duplicate_candidates:
# Create a key based on base filename and size
base_name = re.sub(r'[\s\-_]*\([0-9]+\)|[\s\-_]*copy[\s\-_]*[0-9]*', '', fp.file_name.lower())
size_key = f"{fp.file_size_mb:.1f}mb"
key = f"{base_name}_{size_key}"
name_size_groups[key].append(fp)
# Create groups for sets with multiple files
for key, file_list in name_size_groups.items():
if len(file_list) > 1:
group_id = hashlib.md5(f"duplicates_{key}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
group = BatchGroup(
group_id=group_id,
group_type="duplicates",
group_name=f"Duplicates: {file_list[0].file_name}",
file_previews=file_list,
common_characteristics={
"base_filename": base_name,
"file_size": f"{file_list[0].file_size_mb:.1f} MB",
"file_type": file_list[0].file_type
},
suggested_action="remove_duplicates",
confidence_level=ConfidenceLevel.SMART,
batch_size=len(file_list)
)
groups.append(group)
return groups
def _group_by_similar_content(self, file_previews: List[FilePreview]) -> List[BatchGroup]:
"""Group files with similar content"""
groups = []
# Only group files with actual content
content_files = [fp for fp in file_previews if fp.content_keywords]
if len(content_files) < 2:
return groups
# Calculate content similarity matrix
similarity_groups = []
processed = set()
for i, fp1 in enumerate(content_files):
if fp1.file_path in processed:
continue
similar_files = [fp1]
processed.add(fp1.file_path)
for j, fp2 in enumerate(content_files[i+1:], i+1):
if fp2.file_path in processed:
continue
similarity = self._calculate_content_similarity(fp1, fp2)
if similarity >= self.config["similarity_threshold"]:
similar_files.append(fp2)
processed.add(fp2.file_path)
if len(similar_files) > 1:
similarity_groups.append(similar_files)
# Create batch groups
for i, file_list in enumerate(similarity_groups):
if len(file_list) > 1:
group_id = hashlib.md5(f"similar_content_{i}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
# Find common keywords
common_keywords = self._find_common_keywords([fp.content_keywords for fp in file_list])
group = BatchGroup(
group_id=group_id,
group_type="similar_content",
group_name=f"Similar: {', '.join(common_keywords[:3])}",
file_previews=file_list,
common_characteristics={
"common_keywords": common_keywords,
"content_theme": self._infer_content_theme(common_keywords),
"average_confidence": sum(fp.confidence_score for fp in file_list) / len(file_list)
},
suggested_action="organize_together",
confidence_level=ConfidenceLevel.SMART,
batch_size=len(file_list)
)
groups.append(group)
return groups
def _group_by_file_type(self, file_previews: List[FilePreview]) -> List[BatchGroup]:
"""Group files by file type"""
groups = []
type_groups = defaultdict(list)
for fp in file_previews:
type_groups[fp.file_type].append(fp)
for file_type, file_list in type_groups.items():
if len(file_list) > 1:
group_id = hashlib.md5(f"file_type_{file_type}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
group = BatchGroup(
group_id=group_id,
group_type="same_type",
group_name=f"{file_type.upper()} Files",
file_previews=file_list,
common_characteristics={
"file_type": file_type,
"total_size_mb": sum(fp.file_size_mb for fp in file_list),
"date_range": f"{min(fp.modification_date for fp in file_list).date()} to {max(fp.modification_date for fp in file_list).date()}"
},
suggested_action="organize_by_type",
confidence_level=ConfidenceLevel.MINIMAL,
batch_size=len(file_list)
)
groups.append(group)
return groups
def _group_by_source_directory(self, file_previews: List[FilePreview]) -> List[BatchGroup]:
"""Group files by source directory"""
groups = []
dir_groups = defaultdict(list)
for fp in file_previews:
source_dir = str(Path(fp.file_path).parent)
dir_groups[source_dir].append(fp)
for source_dir, file_list in dir_groups.items():
if len(file_list) > 1:
group_id = hashlib.md5(f"source_dir_{source_dir}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
group = BatchGroup(
group_id=group_id,
group_type="same_source",
group_name=f"From: {Path(source_dir).name}",
file_previews=file_list,
common_characteristics={
"source_directory": source_dir,
"total_files": len(file_list),
"mixed_types": len(set(fp.file_type for fp in file_list)) > 1
},
suggested_action="organize_from_source",
confidence_level=ConfidenceLevel.MINIMAL,
batch_size=len(file_list)
)
groups.append(group)
return groups
def _group_by_date(self, file_previews: List[FilePreview]) -> List[BatchGroup]:
"""Group files by modification date"""
groups = []
# Group by week
week_groups = defaultdict(list)
for fp in file_previews:
# Get the start of the week
week_start = fp.modification_date.date() - timedelta(days=fp.modification_date.weekday())
week_groups[week_start].append(fp)
for week_start, file_list in week_groups.items():
if len(file_list) > 2: # At least 3 files to make a group
group_id = hashlib.md5(f"date_week_{week_start}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
group = BatchGroup(
group_id=group_id,
group_type="same_date",
group_name=f"Week of {week_start}",
file_previews=file_list,
common_characteristics={
"week_start": str(week_start),
"date_range": f"{min(fp.modification_date for fp in file_list).date()} to {max(fp.modification_date for fp in file_list).date()}",
"total_files": len(file_list)
},
suggested_action="organize_by_date",
confidence_level=ConfidenceLevel.NEVER, # Always ask for date-based groups
batch_size=len(file_list)
)
groups.append(group)
return groups
def _create_individual_group(self, file_preview: FilePreview) -> BatchGroup:
"""Create a group for a single file"""
group_id = hashlib.md5(f"individual_{file_preview.file_path}_{datetime.now().isoformat()}".encode()).hexdigest()[:12]
# Determine confidence level based on prediction confidence
if file_preview.confidence_score >= 0.8:
confidence_level = ConfidenceLevel.SMART
elif file_preview.confidence_score >= 0.6:
confidence_level = ConfidenceLevel.MINIMAL
else:
confidence_level = ConfidenceLevel.NEVER
group = BatchGroup(
group_id=group_id,
group_type="individual",
group_name=file_preview.file_name,
file_previews=[file_preview],
common_characteristics={
"file_type": file_preview.file_type,
"predicted_category": file_preview.predicted_category,
"confidence": file_preview.confidence_score
},
suggested_action="organize_individual",
confidence_level=confidence_level,
batch_size=1
)
return group
def _split_large_group(self, group: BatchGroup) -> List[BatchGroup]:
"""Split a large group into smaller groups"""
split_groups = []
files = group.file_previews
for i in range(0, len(files), self.config["max_batch_size"]):
batch_files = files[i:i + self.config["max_batch_size"]]
split_group_id = f"{group.group_id}_split_{i // self.config['max_batch_size']}"
split_group = BatchGroup(
group_id=split_group_id,
group_type=group.group_type,
group_name=f"{group.group_name} (Part {i // self.config['max_batch_size'] + 1})",
file_previews=batch_files,
common_characteristics=group.common_characteristics,
suggested_action=group.suggested_action,
confidence_level=group.confidence_level,
batch_size=len(batch_files)
)
split_groups.append(split_group)
return split_groups
def _prepare_group_for_review(self, group: BatchGroup) -> Dict[str, Any]:
"""Prepare group data for user review"""
# Create summary information
group_summary = {
"group_id": group.group_id,
"group_type": group.group_type,
"group_name": group.group_name,
"batch_size": group.batch_size,
"suggested_action": group.suggested_action,
"confidence_level": group.confidence_level.name,
"common_characteristics": group.common_characteristics
}
# Prepare file previews for display
file_displays = []
for fp in group.file_previews:
file_display = {
"file_name": fp.file_name,
"file_path": fp.file_path,
"file_size_mb": round(fp.file_size_mb, 2),
"file_type": fp.file_type,
"modification_date": fp.modification_date.strftime("%Y-%m-%d %H:%M"),
"content_preview": fp.content_preview[:200] + "..." if len(fp.content_preview) > 200 else fp.content_preview,
"content_keywords": fp.content_keywords[:5], # Top 5 keywords
"content_summary": fp.content_summary,
"predicted_category": fp.predicted_category,
"confidence_score": round(fp.confidence_score, 2),
"duplicate_indicator": fp.duplicate_indicator
}
file_displays.append(file_display)
# Suggest possible actions
possible_actions = self._get_possible_actions(group)
return {
"group_summary": group_summary,
"file_previews": file_displays,
"possible_actions": possible_actions,
"recommendation": self._get_group_recommendation(group)
}
def _get_possible_actions(self, group: BatchGroup) -> List[Dict[str, Any]]:
"""Get possible actions for a group"""
actions = []
if group.group_type == "duplicates":
actions.extend([
{"action": "keep_newest", "label": "Keep newest, delete others", "confidence": 0.8},
{"action": "keep_largest", "label": "Keep largest, delete others", "confidence": 0.7},
{"action": "manual_select", "label": "Let me choose which to keep", "confidence": 1.0},
{"action": "skip", "label": "Skip this group", "confidence": 1.0}
])
elif group.group_type == "similar_content":
actions.extend([
{"action": "organize_together", "label": f"Organize to {group.file_previews[0].predicted_category}", "confidence": 0.8},
{"action": "organize_separately", "label": "Organize files separately", "confidence": 0.6},
{"action": "create_project_folder", "label": "Create new project folder", "confidence": 0.7},
{"action": "skip", "label": "Skip this group", "confidence": 1.0}
])
else:
# Generic actions
actions.extend([
{"action": "organize_predicted", "label": f"Organize to predicted categories", "confidence": 0.7},
{"action": "organize_together", "label": "Organize all together", "confidence": 0.6},
{"action": "organize_individually", "label": "Process each file separately", "confidence": 0.8},
{"action": "skip", "label": "Skip this group", "confidence": 1.0}
])
return actions
def _get_group_recommendation(self, group: BatchGroup) -> Dict[str, Any]:
"""Get recommendation for a group"""
avg_confidence = sum(fp.confidence_score for fp in group.file_previews) / len(group.file_previews)
if group.group_type == "duplicates":
recommendation = {
"action": "keep_newest",
"reasoning": "Keep the most recent version and remove duplicates to save space",
"confidence": 0.8
}
elif group.group_type == "similar_content" and avg_confidence > 0.7:
recommendation = {
"action": "organize_together",
"reasoning": f"Files appear related and can be organized to {group.file_previews[0].predicted_category}",
"confidence": avg_confidence
}
elif avg_confidence > 0.6:
recommendation = {
"action": "organize_predicted",
"reasoning": "System is confident about categorization predictions",
"confidence": avg_confidence
}
else:
recommendation = {
"action": "organize_individually",
"reasoning": "Files need individual review due to low confidence",
"confidence": avg_confidence
}
return recommendation
# Helper methods
def _resolve_category_path(self, category: str) -> Path:
"""Resolve category to a path relative to base_dir"""
# Map categories to Google Drive structure (simplified version of GDriveIntegration)
category_mapping = {
"creative": "01_ACTIVE_PROJECTS/Creative_Projects",
"entertainment": "01_ACTIVE_PROJECTS/Entertainment_Industry",
"business": "01_ACTIVE_PROJECTS/Business_Operations",
"visual": "02_MEDIA_ASSETS/Visual_Assets",
"audio": "02_MEDIA_ASSETS/Audio_Library",
"video": "02_MEDIA_ASSETS/Video_Content",
"research": "03_REFERENCE_LIBRARY/Research_Papers",
"templates": "03_REFERENCE_LIBRARY/Templates",
"unknown": "99_TEMP_PROCESSING/Manual_Review"
}
rel_path = category_mapping.get(category, "99_TEMP_PROCESSING/Manual_Review")
return self.base_dir / rel_path
def _move_file_safely(self, source_path: Path, target_dir: Path, session_id: str, action_type: str, confidence: float, category: str) -> Optional[Path]:
"""Move file safely with collision handling and rollback logging"""
try:
if not source_path.exists():
self.logger.warning(f"File not found for move: {source_path}")
return None
# Create target directory
target_dir.mkdir(parents=True, exist_ok=True)
target_path = target_dir / source_path.name
# Handle name conflicts
counter = 1
original_target = target_path
while target_path.exists():
stem = original_target.stem
suffix = original_target.suffix
target_path = target_dir / f"{stem}_{counter}{suffix}"
counter += 1
# Record operation before moving (in case it fails midway, though move is atomic-ish)
# Actually rollback system records what happened, so we log it.
# But EasyRollbackSystem needs the move to happen?
# Looking at EasyRollbackSystem, it just logs the operation.
import shutil
shutil.move(str(source_path), str(target_path))
# Log to rollback system
try:
self.rollback_system.record_operation(
operation_type=action_type,
original_path=str(source_path.parent),
original_filename=source_path.name,
new_filename=target_path.name,
new_location=str(target_dir),
category=category,
confidence=confidence,
notes=f"Batch session {session_id}"
)
except Exception as e:
self.logger.error(f"Failed to log rollback operation: {e}")
return target_path
except Exception as e:
self.logger.error(f"Error moving file {source_path}: {e}")
return None
def _calculate_file_hash(self, file_path: Path) -> str:
"""Calculate file hash for caching"""
try:
stat_info = file_path.stat()
content = f"{file_path.name}_{stat_info.st_size}_{stat_info.st_mtime}"
return hashlib.md5(content.encode()).hexdigest()
except:
return hashlib.md5(str(file_path).encode()).hexdigest()
def _get_cached_preview(self, file_path: str, file_hash: str, db_connection: Optional[sqlite3.Connection] = None) -> Optional[FilePreview]:
"""Get cached preview if available"""
try:
if db_connection:
# Use provided connection
cursor = db_connection.execute("""
SELECT preview_data FROM file_previews
WHERE file_path = ? AND file_hash = ?
""", (file_path, file_hash))
result = cursor.fetchone()
if result:
preview_data = json.loads(result[0])
# Update access count
db_connection.execute("""
UPDATE file_previews
SET last_accessed = ?, access_count = access_count + 1
WHERE file_path = ?
""", (datetime.now().isoformat(), file_path))
# Note: We do NOT commit here if using shared connection