Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][photos] Prune resolved ML error markers

laurenspriem543052956951
diff --git a/mobile/apps/photos/lib/db/ml/base.dart b/mobile/apps/photos/lib/db/ml/base.dart
index caa8ae3341..f19af5c699 100644
--- a/mobile/apps/photos/lib/db/ml/base.dart
+++ b/mobile/apps/photos/lib/db/ml/base.dart
@@ -70,6 +70,7 @@ abstract class IMLDataDB<T> {
Future<int> getTotalFaceCount();
Future<int> getErroredFaceCount();
Future<Set<T>> getErroredFileIDs();
+ Future<void> pruneResolvedFaceErrorResults(List<T> fileIDs);
Future<Set<T>> getFileIDsWithErrorResults(List<T> fileIDs);
Future<void> deleteFaceIndexForFiles(List<T> fileIDs);
Future<void> deleteUnclusteredFaceIndexForFiles(List<T> fileIDs);
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dart
index 1665e5f4e1..9e1ab6ffa6 100644
--- a/mobile/apps/photos/lib/db/ml/db.dart
+++ b/mobile/apps/photos/lib/db/ml/db.dart
@@ -1241,6 +1241,26 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
return maps.map((e) => e[fileIDColumn] as int).toSet();
}
+ @override
+ Future<void> pruneResolvedFaceErrorResults(List<int> fileIDs) async {
+ if (fileIDs.isEmpty) return;
+ final db = await asyncDB;
+ for (final chunk in fileIDs.chunks(_maxSqlBindParamsPerQuery)) {
+ final placeholders = List.filled(chunk.length, '?').join(', ');
+ await db.execute('''
+ DELETE FROM $facesTable
+ WHERE $fileIDColumn IN ($placeholders)
+ AND $faceScore < 0
+ AND EXISTS (
+ SELECT 1 FROM $facesTable AS successful
+ WHERE successful.$fileIDColumn = $facesTable.$fileIDColumn
+ AND successful.$faceScore >= 0
+ AND successful.$mlVersionColumn >= $facesTable.$mlVersionColumn
+ )
+ ''', chunk);
+ }
+ }
+
@override
Future<Set<int>> getFileIDsWithErrorResults(List<int> fileIDs) async {
if (fileIDs.isEmpty) return {};
@@ -1251,8 +1271,15 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {
final placeholders = List.filled(chunk.length, '?').join(', ');
final rows = await db.getAll(
'''
- SELECT $fileIDColumn FROM $facesTable
- WHERE $fileIDColumn IN ($placeholders) AND $faceScore < 0
+ SELECT failed.$fileIDColumn FROM $facesTable AS failed
+ WHERE failed.$fileIDColumn IN ($placeholders)
+ AND failed.$faceScore < 0
+ AND NOT EXISTS (
+ SELECT 1 FROM $facesTable AS successful
+ WHERE successful.$fileIDColumn = failed.$fileIDColumn
+ AND successful.$faceScore >= 0
+ AND successful.$mlVersionColumn >= failed.$mlVersionColumn
+ )
UNION
SELECT $fileIDColumn FROM $clipTable
WHERE $fileIDColumn IN ($placeholders) AND LENGTH($embeddingColumn) = 0
diff --git a/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart b/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
index 6b491af0a5..83f6b9c944 100644
--- a/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
+++ b/mobile/apps/photos/lib/ui/settings/ml/machine_learning_settings_page.dart
@@ -41,6 +41,7 @@ class _MachineLearningSettingsPageState
Timer? _advancedOptionsTimer;
bool _hasAcknowledgedMLConsent = false;
bool _hasHandledDisabledExit = false;
+ bool _mlDecryptionRecordsReady = false;
@override
void initState() {
@@ -67,26 +68,22 @@ class _MachineLearningSettingsPageState
Future<void> _pruneMlDecryptionRecords() async {
final recordedFileIDs = mlDecryptionRecordStore.fileIDs;
- if (recordedFileIDs.isEmpty) {
- mlDecryptionRecordStore.logFileIDs();
- return;
- }
try {
- final existingFiles = await FilesDB.instance.getFileIDToFileFromIDs(
- recordedFileIDs,
- );
- final errorResultFileIDs = await MLDataDB.instance
- .getFileIDsWithErrorResults(recordedFileIDs);
- final staleFileIDs = recordedFileIDs
- .where(
- (fileID) =>
- !existingFiles.containsKey(fileID) ||
- !errorResultFileIDs.contains(fileID),
- )
- .toSet();
- await mlDecryptionRecordStore.removeAll(staleFileIDs);
- if (staleFileIDs.isNotEmpty && mounted) {
- setState(() {});
+ if (recordedFileIDs.isNotEmpty) {
+ await MLDataDB.instance.pruneResolvedFaceErrorResults(recordedFileIDs);
+ final existingFiles = await FilesDB.instance.getFileIDToFileFromIDs(
+ recordedFileIDs,
+ );
+ final errorResultFileIDs = await MLDataDB.instance
+ .getFileIDsWithErrorResults(recordedFileIDs);
+ final staleFileIDs = recordedFileIDs
+ .where(
+ (fileID) =>
+ !existingFiles.containsKey(fileID) ||
+ !errorResultFileIDs.contains(fileID),
+ )
+ .toSet();
+ await mlDecryptionRecordStore.removeAll(staleFileIDs);
}
} catch (error, stackTrace) {
_logger.warning(
@@ -96,6 +93,9 @@ class _MachineLearningSettingsPageState
);
} finally {
mlDecryptionRecordStore.logFileIDs();
+ if (mounted) {
+ setState(() => _mlDecryptionRecordsReady = true);
+ }
}
}
@@ -374,7 +374,7 @@ class _MachineLearningSettingsPageState
onlyIndexingModels: true,
) ||
!localSettings.isMLLocalIndexingEnabled
- ? const MLStatusWidget()
+ ? MLStatusWidget(showDecryptionWarning: _mlDecryptionRecordsReady)
: const ModelLoadingState(),
],
);
@@ -490,7 +490,9 @@ class _ModelLoadingStateState extends State<ModelLoadingState> {
}
class MLStatusWidget extends StatefulWidget {
- const MLStatusWidget({super.key});
+ final bool showDecryptionWarning;
+
+ const MLStatusWidget({required this.showDecryptionWarning, super.key});
@override
State<MLStatusWidget> createState() => MLStatusWidgetState();
@@ -524,7 +526,9 @@ class MLStatusWidgetState extends State<MLStatusWidget> {
@override
Widget build(BuildContext context) {
final colors = context.componentColors;
- final decryptionIssueCount = mlDecryptionRecordStore.count;
+ final decryptionIssueCount = widget.showDecryptionWarning
+ ? mlDecryptionRecordStore.count
+ : 0;
return Column(
children: [
Padding(