[mob] Minor refactor

This commit is contained in:
Neeraj Gupta
2024-10-19 15:08:47 +05:30
committed by Neeraj Gupta
parent e95aae589c
commit bba975e6b6
2 changed files with 16 additions and 7 deletions

View File

@@ -100,15 +100,17 @@ class EntityService {
}
}
Future<void> syncEntity(EntityType type) async {
Future<int> syncEntity(EntityType type) async {
try {
await _remoteToLocalSync(type);
return _remoteToLocalSync(type);
} catch (e) {
_logger.severe("Failed to sync entities", e);
return -1;
}
}
Future<void> _remoteToLocalSync(EntityType type) async {
Future<int> _remoteToLocalSync(EntityType type,
{int prevFetchCount = 0}) async {
final int lastSyncTime =
_prefs.getInt(_getEntityLastSyncTimePrefix(type)) ?? 0;
final List<EntityData> result = await _gateway.getDiff(
@@ -117,7 +119,7 @@ class EntityService {
limit: fetchLimit,
);
if (result.isEmpty) {
return;
return prevFetchCount;
}
final bool hasMoreItems = result.length == fetchLimit;
_logger.info("${result.length} entries of type $type fetched");
@@ -161,6 +163,7 @@ class EntityService {
);
} catch (e, s) {
_logger.severe("Failed to decrypted data for key $type", e, s);
rethrow;
}
}
if (entities.isNotEmpty) {
@@ -170,8 +173,10 @@ class EntityService {
await _prefs.setInt(_getEntityLastSyncTimePrefix(type), maxSyncTime);
if (hasMoreItems) {
_logger.info("Diff limit reached, pulling again");
await _remoteToLocalSync(type);
await _remoteToLocalSync(type,
prevFetchCount: prevFetchCount + result.length);
}
return prevFetchCount + result.length;
}
Future<Uint8List> getOrCreateEntityKey(EntityType type) async {

View File

@@ -252,8 +252,13 @@ class PersonService {
}
Future<void> fetchRemoteClusterFeedback() async {
await entityService.syncEntities();
await entityService.syncEntity(EntityType.cgroup);
final entities = await entityService.getEntities(EntityType.cgroup);
// todo: (neerajg) perf change, this can be expensive to do on every sync
// especially when we have a lot of people. We should only do this when the
// last sync time is updated for cgroup entity type. To avoid partial update,
// we need to maintain a lastSyncTime value whenever this data is processed
// and stored in the db.
entities.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
final Map<String, String> faceIdToClusterID = {};
final Map<String, String> clusterToPersonID = {};
@@ -286,7 +291,6 @@ class PersonService {
);
}
}
logger.info("Storing feedback for ${faceIdToClusterID.length} faces");
await faceMLDataDB.updateFaceIdToClusterId(faceIdToClusterID);
await faceMLDataDB.bulkAssignClusterToPersonID(clusterToPersonID);