Skip to content

armgit / ente

Public Imported from GitHub
0

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

Daily recompute

laurenspriem475be7defa62
diff --git a/mobile/apps/photos/lib/services/wrapped/models.dart b/mobile/apps/photos/lib/services/wrapped/models.dart
index 92178f4339..2743d62a14 100644
--- a/mobile/apps/photos/lib/services/wrapped/models.dart
+++ b/mobile/apps/photos/lib/services/wrapped/models.dart
@@ -344,23 +344,28 @@ class WrappedResult {
WrappedResult({
required List<WrappedCard> cards,
required this.year,
+ required DateTime generatedAt,
this.badgeKey,
- }) : cards = List<WrappedCard>.unmodifiable(cards);
+ }) : cards = List<WrappedCard>.unmodifiable(cards),
+ generatedAt = generatedAt.toUtc();
final List<WrappedCard> cards;
final int year;
+ final DateTime generatedAt;
final String? badgeKey;
Map<String, Object?> toJson() {
return <String, Object?>{
"year": year,
"badgeKey": badgeKey,
+ "generatedAt": generatedAt.toIso8601String(),
"cards": cards.map((WrappedCard card) => card.toJson()).toList(),
};
}
static WrappedResult fromJson(Map<String, Object?> json) {
final List<dynamic> rawCards = json["cards"] as List<dynamic>? ?? const [];
+ final DateTime generatedAt = _parseGeneratedAt(json);
return WrappedResult(
cards: rawCards
.map(
@@ -369,6 +374,7 @@ class WrappedResult {
)
.toList(growable: false),
year: json["year"] as int? ?? DateTime.now().year,
+ generatedAt: generatedAt,
badgeKey: json["badgeKey"] as String?,
);
}
@@ -388,6 +394,22 @@ class WrappedResult {
String encode() {
return jsonEncode(toJson());
}
+
+ static DateTime _parseGeneratedAt(Map<String, Object?> json) {
+ final String? rawIso = json["generatedAt"] as String?;
+ if (rawIso != null && rawIso.isNotEmpty) {
+ final DateTime? parsed = DateTime.tryParse(rawIso);
+ if (parsed != null) {
+ return parsed.toUtc();
+ }
+ }
+ final int micros =
+ (json["generatedAtMicros"] as num?)?.toInt() ?? 0; // legacy fallback
+ if (micros > 0) {
+ return DateTime.fromMicrosecondsSinceEpoch(micros, isUtc: true);
+ }
+ return DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
+ }
}
@immutable
diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart
index f537a52856..c4a8e20dac 100644
--- a/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart
+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_engine.dart
@@ -462,6 +462,7 @@ Future<WrappedResult> _wrappedComputeIsolate(
return WrappedResult(
cards: finalCards,
year: year,
+ generatedAt: now,
badgeKey: badgeSelection.badgeKey,
);
}
diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart
index 165a8baafb..441eb727ba 100644
--- a/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart
+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart
@@ -56,6 +56,8 @@ class WrappedService {
static final DateTime _kAvailabilityStart = DateTime(_kWrappedYear, 12, 6);
static final DateTime _kAvailabilityEndExclusive =
DateTime(_kWrappedYear + 1, 1, 7);
+ static final DateTime _kFinalComputeCutoff =
+ DateTime(_kWrappedYear + 1, 1, 1);
final Logger _logger;
final WrappedCacheService _cacheService;
@@ -112,6 +114,13 @@ class WrappedService {
"${cached.cards.length} cards",
);
updateResult(cached);
+ if (_shouldScheduleDailyRecompute(cached)) {
+ _logger
+ .info("Scheduling daily Wrapped recompute for $_kWrappedYear");
+ unawaited(
+ _runCompute(reason: "daily", bypassFlag: false),
+ );
+ }
return;
}
} catch (error, stackTrace) {
@@ -250,4 +259,27 @@ class WrappedService {
return !now.isBefore(_kAvailabilityStart) &&
now.isBefore(_kAvailabilityEndExclusive);
}
+
+ bool _shouldScheduleDailyRecompute(WrappedResult result) {
+ if (!isEnabled) {
+ return false;
+ }
+ final DateTime now = DateTime.now();
+ if (_dateOnly(now).isBefore(_dateOnly(_kAvailabilityStart))) {
+ return false;
+ }
+ final DateTime generatedDate = _dateOnly(result.generatedAt.toLocal());
+ final DateTime today = _dateOnly(now);
+ if (!generatedDate.isBefore(today)) {
+ return false;
+ }
+ if (!generatedDate.isBefore(_kFinalComputeCutoff)) {
+ return false;
+ }
+ return true;
+ }
+
+ static DateTime _dateOnly(DateTime source) {
+ return DateTime(source.year, source.month, source.day);
+ }
}