Skip to content

armgit / ente

Public Imported from GitHub
0

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

More conservatively wait on memory lane crops before showing it

laurenspriem88c312c19d2b
diff --git a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart
index b263295349..d4d1a61f74 100644
--- a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart
+++ b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart
@@ -49,6 +49,11 @@ class MemoryLaneService {
taskTimeout: const Duration(minutes: 5),
maxQueueSize: 1000,
);
+ final TaskQueue<String> _cropReadinessQueue = TaskQueue(
+ maxConcurrentTasks: 1,
+ taskTimeout: const Duration(minutes: 5),
+ maxQueueSize: 1000,
+ );
final ValueNotifier<Set<String>> readyPersonIds = ValueNotifier<Set<String>>(
{},
@@ -56,6 +61,7 @@ class MemoryLaneService {
final Map<String, int> _lastForcedComputeMicros = {};
final Map<String, _PendingRecomputeRequest> _pendingRequests = {};
+ final Set<String> _cropReadinessInFlight = {};
bool _initialized = false;
@@ -155,6 +161,36 @@ class MemoryLaneService {
});
}
+ Future<void> ensureTimelineReachability(
+ String personId, {
+ String trigger = "",
+ }) async {
+ if (!isFeatureEnabled) {
+ return;
+ }
+ if (personId.isEmpty) {
+ return;
+ }
+ final normalizedTrigger = trigger.isEmpty
+ ? "timeline_reachability"
+ : trigger.trim();
+ final timeline = await _cacheService.getTimeline(personId);
+ if (timeline == null || !timeline.isReady || timeline.entries.isEmpty) {
+ await _refreshReadyPersonIds();
+ schedulePersonRecompute(personId, trigger: normalizedTrigger);
+ return;
+ }
+ if (await _areTimelineFaceCropsCached(timeline)) {
+ await _refreshReadyPersonIds();
+ return;
+ }
+ await _refreshReadyPersonIds();
+ _queueTimelineCropReadiness(
+ personId,
+ trigger: normalizedTrigger,
+ );
+ }
+
Future<MemoryLanePersonTimeline?> getTimeline(String personId) async {
if (!isFeatureEnabled) return null;
final timeline = await _cacheService.getTimeline(personId);
@@ -169,6 +205,17 @@ class MemoryLaneService {
(entry) => hiddenFileIds.contains(entry.fileId),
);
if (!containsHiddenEntry) {
+ if (timeline.isReady && !await _areTimelineFaceCropsCached(timeline)) {
+ _logger.info(
+ "Memory Lane: cached timeline for $personId is missing face crops",
+ );
+ _queueTimelineCropReadiness(
+ personId,
+ trigger: "crop_cache_validation",
+ );
+ await _refreshReadyPersonIds();
+ return null;
+ }
return timeline;
}
@@ -195,7 +242,118 @@ class MemoryLaneService {
Future<bool> hasReadyTimeline(String personId) async {
if (!isFeatureEnabled) return false;
final timeline = await _cacheService.getTimeline(personId);
- return timeline?.isReady ?? false;
+ return timeline != null &&
+ timeline.isReady &&
+ await _areTimelineFaceCropsCached(timeline);
+ }
+
+ Future<bool> _areTimelineFaceCropsCached(
+ MemoryLanePersonTimeline timeline,
+ ) async {
+ if (!timeline.isReady || timeline.entries.isEmpty) {
+ return false;
+ }
+ return areFullFaceCropsCached(
+ timeline.entries.map((entry) => entry.faceId),
+ useTempCache: false,
+ );
+ }
+
+ void _queueTimelineCropReadiness(
+ String personId, {
+ required String trigger,
+ }) {
+ if (_cropReadinessInFlight.contains(personId)) {
+ return;
+ }
+ _cropReadinessInFlight.add(personId);
+ _cropReadinessQueue
+ .addTask(personId, () async {
+ try {
+ await _repairTimelineCropReadiness(personId, trigger: trigger);
+ } finally {
+ _cropReadinessInFlight.remove(personId);
+ }
+ })
+ .catchError((error, stackTrace) {
+ _cropReadinessInFlight.remove(personId);
+ _logger.warning(
+ "Memory Lane crop readiness task failed for $personId",
+ error,
+ stackTrace,
+ );
+ });
+ }
+
+ Future<void> _repairTimelineCropReadiness(
+ String personId, {
+ required String trigger,
+ }) async {
+ if (!isFeatureEnabled) {
+ return;
+ }
+ if (!PersonService.isInitialized) {
+ _logger.warning(
+ "Memory Lane crop readiness skipped for $personId: PersonService not initialized",
+ );
+ return;
+ }
+ final timeline = await _cacheService.getTimeline(personId);
+ if (timeline == null || !timeline.isReady || timeline.entries.isEmpty) {
+ await _refreshReadyPersonIds();
+ return;
+ }
+ if (await _areTimelineFaceCropsCached(timeline)) {
+ await _refreshReadyPersonIds();
+ Bus.instance.fire(
+ MemoryLaneChangedEvent(
+ personId: personId,
+ status: MemoryLaneStatus.ready,
+ ),
+ );
+ return;
+ }
+ final person = await PersonService.instance.getPerson(personId);
+ if (person == null) {
+ _logger.info("Memory Lane: person $personId missing, clearing cache");
+ await _cacheService.removeTimeline(personId);
+ await _refreshReadyPersonIds();
+ Bus.instance.fire(
+ MemoryLaneChangedEvent(
+ personId: personId,
+ status: MemoryLaneStatus.ineligible,
+ ),
+ );
+ return;
+ }
+ if (person.data.isIgnored) {
+ _logger.info("Memory Lane: person $personId ignored, clearing cache");
+ await _handleIgnoredPerson(personId);
+ return;
+ }
+
+ final fileIds = timeline.entries.map((entry) => entry.fileId).toSet();
+ final filesById = await _filesDB.getFileIDToFileFromIDs(
+ fileIds.toList(),
+ );
+ final cropsReady = await _ensureFaceCrops(
+ person,
+ timeline.entries,
+ filesById,
+ );
+ await _refreshReadyPersonIds();
+ if (cropsReady) {
+ Bus.instance.fire(
+ MemoryLaneChangedEvent(
+ personId: personId,
+ status: MemoryLaneStatus.ready,
+ ),
+ );
+ } else {
+ _logger.warning(
+ "Memory Lane crop readiness failed for $personId (trigger: $trigger)",
+ );
+ }
}
Future<void> _handleIgnoredPerson(String personId) async {
@@ -514,18 +672,36 @@ class MemoryLaneService {
logicVersion: _timelineLogicVersion,
),
);
- await _refreshReadyPersonIds();
- Bus.instance.fire(
- MemoryLaneChangedEvent(
- personId: person.remoteID,
- status: result.timeline.status,
- ),
+ if (!result.timeline.isReady) {
+ await _refreshReadyPersonIds();
+ Bus.instance.fire(
+ MemoryLaneChangedEvent(
+ personId: person.remoteID,
+ status: result.timeline.status,
+ ),
+ );
+ return;
+ }
+ final cropsReady = await _ensureFaceCrops(
+ person,
+ result.timeline.entries,
+ result.filesById,
);
- if (result.timeline.isReady) {
- await _ensureFaceCrops(
- person,
- result.timeline.entries,
- result.filesById,
+ await _refreshReadyPersonIds();
+ if (cropsReady) {
+ Bus.instance.fire(
+ MemoryLaneChangedEvent(
+ personId: person.remoteID,
+ status: result.timeline.status,
+ ),
+ );
+ } else {
+ _logger.warning(
+ "Memory Lane crop readiness failed for $personId (trigger: $trigger)",
+ );
+ _queueTimelineCropReadiness(
+ person.remoteID,
+ trigger: "recompute_crop_retry",
);
}
} catch (error, stackTrace) {
@@ -698,12 +874,16 @@ class MemoryLaneService {
return (timeline: timeline, filesById: fileMap, faceCount: totalFaceCount);
}
- Future<void> _ensureFaceCrops(
+ Future<bool> _ensureFaceCrops(
PersonEntity person,
List<MemoryLaneEntry> entries,
Map<int, EnteFile> fileMap,
) async {
final personId = person.remoteID;
+ if (entries.isEmpty) {
+ return false;
+ }
+ var allCropsReady = true;
final entriesByFile = <int, List<MemoryLaneEntry>>{};
for (final entry in entries) {
entriesByFile.putIfAbsent(entry.fileId, () => []).add(entry);
@@ -711,9 +891,21 @@ class MemoryLaneService {
for (final entry in entriesByFile.entries) {
final file = fileMap[entry.key];
- if (file == null) continue;
+ if (file == null) {
+ allCropsReady = false;
+ _logger.warning(
+ "Memory Lane: failed to cache crops for $personId, file ${entry.key} missing",
+ );
+ continue;
+ }
final faces = await _mlDataDB.getFacesForGivenFileID(entry.key);
- if (faces == null || faces.isEmpty) continue;
+ if (faces == null || faces.isEmpty) {
+ allCropsReady = false;
+ _logger.warning(
+ "Memory Lane: failed to cache crops for $personId, no faces for file ${entry.key}",
+ );
+ continue;
+ }
final selectedFaces = entry.value
.map(
(timelineEntry) => faces.firstWhereOrNull(
@@ -722,15 +914,37 @@ class MemoryLaneService {
)
.whereType<Face>()
.toList();
- if (selectedFaces.isEmpty) continue;
+ if (selectedFaces.length != entry.value.length) {
+ allCropsReady = false;
+ _logger.warning(
+ "Memory Lane: failed to cache all crops for $personId file ${entry.key}, some faces missing",
+ );
+ }
+ if (selectedFaces.isEmpty) {
+ continue;
+ }
try {
- await getCachedFaceCrops(
+ final cropMap = await getCachedFaceCrops(
file,
selectedFaces,
useFullFile: true,
useTempCache: false,
);
+ if (cropMap == null) {
+ allCropsReady = false;
+ continue;
+ }
+ for (final face in selectedFaces) {
+ final crop = cropMap[face.faceID];
+ if (crop == null || crop.isEmpty) {
+ allCropsReady = false;
+ _logger.warning(
+ "Memory Lane: missing generated crop for $personId file ${entry.key}",
+ );
+ }
+ }
} catch (error, stackTrace) {
+ allCropsReady = false;
_logger.warning(
"Memory Lane: failed to cache crops for $personId file ${entry.key}",
error,
@@ -738,6 +952,7 @@ class MemoryLaneService {
);
}
}
+ return allCropsReady;
}
Future<void> prewarmTimelineFrames(
@@ -807,10 +1022,15 @@ class MemoryLaneService {
Future<void> _refreshReadyPersonIds() async {
final cache = await _cacheService.getCache();
- final current = cache.allTimelines
- .where((timeline) => timeline.isReady)
- .map((timeline) => timeline.personId)
- .toSet();
+ final current = <String>{};
+ for (final timeline in cache.allTimelines) {
+ if (!timeline.isReady) {
+ continue;
+ }
+ if (await _areTimelineFaceCropsCached(timeline)) {
+ current.add(timeline.personId);
+ }
+ }
readyPersonIds.value = current;
}
diff --git a/mobile/apps/photos/lib/ui/viewer/people/people_page.dart b/mobile/apps/photos/lib/ui/viewer/people/people_page.dart
index d785fed5c5..b8c3477b76 100644
--- a/mobile/apps/photos/lib/ui/viewer/people/people_page.dart
+++ b/mobile/apps/photos/lib/ui/viewer/people/people_page.dart
@@ -125,16 +125,12 @@ class _PeoplePageState extends State<PeoplePage> {
_maybePrewarmMemoryLane();
};
_timelineNotifier!.addListener(_timelineListener!);
- if (!MemoryLaneService.instance.hasReadyTimelineSync(
- _person.remoteID,
- )) {
- MemoryLaneService.instance.schedulePersonRecompute(
+ unawaited(
+ MemoryLaneService.instance.ensureTimelineReachability(
_person.remoteID,
trigger: "people_page_visit",
- );
- } else {
- _maybePrewarmMemoryLane();
- }
+ ),
+ );
}
}
diff --git a/mobile/apps/photos/lib/utils/face/face_thumbnail_cache.dart b/mobile/apps/photos/lib/utils/face/face_thumbnail_cache.dart
index 7a3e499e23..39726583aa 100644
--- a/mobile/apps/photos/lib/utils/face/face_thumbnail_cache.dart
+++ b/mobile/apps/photos/lib/utils/face/face_thumbnail_cache.dart
@@ -53,6 +53,31 @@ Uint8List? _checkInMemoryCachedCropForFaceID(String faceID) {
return cachedCover;
}
+Future<bool> areFullFaceCropsCached(
+ Iterable<String> faceIDs, {
+ bool useTempCache = false,
+}) async {
+ final uniqueFaceIDs = faceIDs.toSet();
+ if (uniqueFaceIDs.isEmpty) {
+ return false;
+ }
+ for (final faceID in uniqueFaceIDs) {
+ final Uint8List? cachedFace = _checkInMemoryCachedCropForFaceID(faceID);
+ if (cachedFace != null && cachedFace.isNotEmpty) {
+ continue;
+ }
+ final faceCropCacheFile = cachedFaceCropPath(faceID, useTempCache);
+ if (!await faceCropCacheFile.exists()) {
+ return false;
+ }
+ final fileLength = await faceCropCacheFile.length();
+ if (fileLength <= 0) {
+ return false;
+ }
+ }
+ return true;
+}
+
Future<String?> checkUsedFaceIDForPersonOrClusterId(
String personOrClusterID,
) async {
@@ -197,8 +222,7 @@ Future<Map<String, Uint8List>?> getCachedFaceCrops(
);
final faceCropCacheFile = cachedFaceCropPath(entry.key, useTempCache);
try {
- // ignore: unawaited_futures
- faceCropCacheFile.writeAsBytes(computedCrop);
+ await faceCropCacheFile.writeAsBytes(computedCrop, flush: true);
} catch (e, s) {
_logger.severe(
"Error writing cached face crop for faceID ${entry.key} to file ${faceCropCacheFile.path}",