[mob][photos] Internally keep track of MLController status

This commit is contained in:
laurenspriem 2024-05-15 11:33:35 +05:30
parent b2c274e73b
commit 6ab1371077
2 changed files with 15 additions and 12 deletions

View file

@ -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/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/file_ml.dart';
import 'package:photos/services/machine_learning/file_ml/remote_fileml_service.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/services/search_service.dart";
import "package:photos/utils/file_util.dart"; import "package:photos/utils/file_util.dart";
import 'package:photos/utils/image_ml_isolate.dart'; import 'package:photos/utils/image_ml_isolate.dart';
@ -86,7 +85,9 @@ class FaceMlService {
final _computer = Computer.shared(); final _computer = Computer.shared();
bool isInitialized = false; bool isInitialized = false;
bool canRunMLController = false;
bool isImageIndexRunning = false; bool isImageIndexRunning = false;
bool isClusteringRunning = false;
final int _parallelism = 10; final int _parallelism = 10;
final int _remoteFetchLimit = 100; final int _remoteFetchLimit = 100;
@ -123,7 +124,8 @@ class FaceMlService {
/// hooking FaceML into [MachineLearningController] /// hooking FaceML into [MachineLearningController]
if (Platform.isAndroid) { if (Platform.isAndroid) {
Bus.instance.on<MachineLearningControlEvent>().listen((event) { Bus.instance.on<MachineLearningControlEvent>().listen((event) {
if (event.shouldRun) { canRunMLController = event.shouldRun;
if (canRunMLController) {
unawaited(indexAllImages()); unawaited(indexAllImages());
} else { } else {
pauseIndexing(); pauseIndexing();
@ -257,7 +259,8 @@ class FaceMlService {
return _functionLock.synchronized(() async { return _functionLock.synchronized(() async {
_resetInactivityTimer(); _resetInactivityTimer();
if (isImageIndexRunning == false || MachineLearningController.instance.canRunML == false) { if (isImageIndexRunning == false ||
canRunMLController == false) {
return null; return null;
} }
@ -649,7 +652,7 @@ class FaceMlService {
stopwatch.stop(); stopwatch.stop();
_logger.info( _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 // 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. '''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()} enteFile: ${enteFile.toString()}
isImageIndexRunning: $isImageIndexRunning isImageIndexRunning: $isImageIndexRunning
canRunML: ${MachineLearningController.instance.canRunML} canRunML: $canRunMLController
''', ''',
); );
throw CouldNotRetrieveAnyFileData(); throw CouldNotRetrieveAnyFileData();
@ -1351,7 +1354,8 @@ class FaceMlService {
} }
bool _skipAnalysisEnteFile(EnteFile enteFile, Map<int, int> indexedFileIds) { bool _skipAnalysisEnteFile(EnteFile enteFile, Map<int, int> indexedFileIds) {
if (isImageIndexRunning == false || MachineLearningController.instance.canRunML == false) { if (isImageIndexRunning == false ||
canRunMLController == false) {
return true; return true;
} }
// Skip if the file is not uploaded or not owned by the user // Skip if the file is not uploaded or not owned by the user

View file

@ -22,11 +22,9 @@ class MachineLearningController {
bool _isDeviceHealthy = true; bool _isDeviceHealthy = true;
bool _isUserInteracting = true; bool _isUserInteracting = true;
bool _isRunningML = false; bool _canRunML = false;
late Timer _userInteractionTimer; late Timer _userInteractionTimer;
get canRunML => _isDeviceHealthy && !_isUserInteracting;
void init() { void init() {
if (Platform.isAndroid) { if (Platform.isAndroid) {
_startInteractionTimer(); _startInteractionTimer();
@ -37,6 +35,7 @@ class MachineLearningController {
}); });
} else { } else {
// Always run Machine Learning on iOS // Always run Machine Learning on iOS
_canRunML = true;
Bus.instance.fire(MachineLearningControlEvent(true)); Bus.instance.fire(MachineLearningControlEvent(true));
} }
} }
@ -54,9 +53,9 @@ class MachineLearningController {
} }
void _fireControlEvent() { void _fireControlEvent() {
final shouldRunML = canRunML; final shouldRunML = _isDeviceHealthy && !_isUserInteracting;
if (shouldRunML != _isRunningML) { if (shouldRunML != _canRunML) {
_isRunningML = shouldRunML; _canRunML = shouldRunML;
_logger.info( _logger.info(
"Firing event with device health: $_isDeviceHealthy and user interaction: $_isUserInteracting", "Firing event with device health: $_isDeviceHealthy and user interaction: $_isUserInteracting",
); );