From 6ab13710773d03f8e4f1c91b19f273adec7feaea Mon Sep 17 00:00:00 2001 From: laurenspriem Date: Wed, 15 May 2024 11:33:35 +0530 Subject: [PATCH] [mob][photos] Internally keep track of MLController status --- .../face_ml/face_ml_service.dart | 16 ++++++++++------ .../machine_learning_controller.dart | 11 +++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart b/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart index d4c1a891f..0508505ef 100644 --- a/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart +++ b/mobile/lib/services/machine_learning/face_ml/face_ml_service.dart @@ -41,7 +41,6 @@ import 'package:photos/services/machine_learning/face_ml/face_ml_exceptions.dart import 'package:photos/services/machine_learning/face_ml/face_ml_result.dart'; import 'package:photos/services/machine_learning/file_ml/file_ml.dart'; import 'package:photos/services/machine_learning/file_ml/remote_fileml_service.dart'; -import "package:photos/services/machine_learning/machine_learning_controller.dart"; import "package:photos/services/search_service.dart"; import "package:photos/utils/file_util.dart"; import 'package:photos/utils/image_ml_isolate.dart'; @@ -86,7 +85,9 @@ class FaceMlService { final _computer = Computer.shared(); bool isInitialized = false; + bool canRunMLController = false; bool isImageIndexRunning = false; + bool isClusteringRunning = false; final int _parallelism = 10; final int _remoteFetchLimit = 100; @@ -123,7 +124,8 @@ class FaceMlService { /// hooking FaceML into [MachineLearningController] if (Platform.isAndroid) { Bus.instance.on().listen((event) { - if (event.shouldRun) { + canRunMLController = event.shouldRun; + if (canRunMLController) { unawaited(indexAllImages()); } else { pauseIndexing(); @@ -257,7 +259,8 @@ class FaceMlService { return _functionLock.synchronized(() async { _resetInactivityTimer(); - if (isImageIndexRunning == false || MachineLearningController.instance.canRunML == false) { + if (isImageIndexRunning == false || + canRunMLController == false) { return null; } @@ -649,7 +652,7 @@ class FaceMlService { stopwatch.stop(); _logger.info( - "`indexAllImages()` finished. Analyzed $fileAnalyzedCount images, in ${stopwatch.elapsed.inSeconds} seconds (avg of ${stopwatch.elapsed.inSeconds / fileAnalyzedCount} seconds per image, skipped $fileSkippedCount images. MLController status: ${MachineLearningController.instance.canRunML})", + "`indexAllImages()` finished. Analyzed $fileAnalyzedCount images, in ${stopwatch.elapsed.inSeconds} seconds (avg of ${stopwatch.elapsed.inSeconds / fileAnalyzedCount} seconds per image, skipped $fileSkippedCount images. MLController status: $canRunMLController)", ); // Dispose of all the isolates @@ -1343,7 +1346,7 @@ class FaceMlService { '''Skipped analysis of image with enteFile, it might be the wrong format or has no uploadedFileID, or MLController doesn't allow it to run. enteFile: ${enteFile.toString()} isImageIndexRunning: $isImageIndexRunning - canRunML: ${MachineLearningController.instance.canRunML} + canRunML: $canRunMLController ''', ); throw CouldNotRetrieveAnyFileData(); @@ -1351,7 +1354,8 @@ class FaceMlService { } bool _skipAnalysisEnteFile(EnteFile enteFile, Map indexedFileIds) { - if (isImageIndexRunning == false || MachineLearningController.instance.canRunML == false) { + if (isImageIndexRunning == false || + canRunMLController == false) { return true; } // Skip if the file is not uploaded or not owned by the user diff --git a/mobile/lib/services/machine_learning/machine_learning_controller.dart b/mobile/lib/services/machine_learning/machine_learning_controller.dart index 81c1dc8bc..8f30dc9ff 100644 --- a/mobile/lib/services/machine_learning/machine_learning_controller.dart +++ b/mobile/lib/services/machine_learning/machine_learning_controller.dart @@ -22,11 +22,9 @@ class MachineLearningController { bool _isDeviceHealthy = true; bool _isUserInteracting = true; - bool _isRunningML = false; + bool _canRunML = false; late Timer _userInteractionTimer; - get canRunML => _isDeviceHealthy && !_isUserInteracting; - void init() { if (Platform.isAndroid) { _startInteractionTimer(); @@ -37,6 +35,7 @@ class MachineLearningController { }); } else { // Always run Machine Learning on iOS + _canRunML = true; Bus.instance.fire(MachineLearningControlEvent(true)); } } @@ -54,9 +53,9 @@ class MachineLearningController { } void _fireControlEvent() { - final shouldRunML = canRunML; - if (shouldRunML != _isRunningML) { - _isRunningML = shouldRunML; + final shouldRunML = _isDeviceHealthy && !_isUserInteracting; + if (shouldRunML != _canRunML) { + _canRunML = shouldRunML; _logger.info( "Firing event with device health: $_isDeviceHealthy and user interaction: $_isUserInteracting", );