From 74e8bb55c03fe0bcaf496317eb032f8efc8033c0 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 10 Feb 2023 18:41:36 +0530 Subject: [PATCH] fix getData returning null --- .../MLServiceFileInfoButton.tsx | 5 ++-- .../machineLearning/machineLearningFactory.ts | 11 +++++++- .../machineLearning/machineLearningService.ts | 27 +++++++------------ src/services/machineLearning/mlWorkManager.ts | 8 +++--- src/types/machineLearning/index.ts | 4 ++- src/utils/common/key.ts | 1 + src/worker/ml.worker.ts | 7 ++--- 7 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/components/MachineLearning/MLServiceFileInfoButton.tsx b/src/components/MachineLearning/MLServiceFileInfoButton.tsx index 98bf69df4..0def27ece 100644 --- a/src/components/MachineLearning/MLServiceFileInfoButton.tsx +++ b/src/components/MachineLearning/MLServiceFileInfoButton.tsx @@ -1,7 +1,7 @@ import React, { useState } from 'react'; import { Button, Spinner } from 'react-bootstrap'; import { EnteFile } from 'types/file'; -import { getToken } from 'utils/common/key'; +import { getToken, getUserID } from 'utils/common/key'; import mlService from '../../services/machineLearning/machineLearningService'; function MLServiceFileInfoButton({ @@ -18,9 +18,10 @@ function MLServiceFileInfoButton({ const runMLService = async () => { setMlServiceRunning(true); const token = getToken(); + const userID = getUserID(); // index 4 is for timeout of 240 seconds - await mlService.syncLocalFile(token, file as EnteFile, null, 4); + await mlService.syncLocalFile(token, userID, file as EnteFile, null, 4); setUpdateMLDataIndex(updateMLDataIndex + 1); setMlServiceRunning(false); diff --git a/src/services/machineLearning/machineLearningFactory.ts b/src/services/machineLearning/machineLearningFactory.ts index 43ce0ea7d..24d29d30b 100644 --- a/src/services/machineLearning/machineLearningFactory.ts +++ b/src/services/machineLearning/machineLearningFactory.ts @@ -122,15 +122,22 @@ export class MLFactory { public static getMLSyncContext( token: string, + userID: number, config: MLSyncConfig, shouldUpdateMLVersion: boolean = true ) { - return new LocalMLSyncContext(token, config, shouldUpdateMLVersion); + return new LocalMLSyncContext( + token, + userID, + config, + shouldUpdateMLVersion + ); } } export class LocalMLSyncContext implements MLSyncContext { public token: string; + public userID: number; public config: MLSyncConfig; public shouldUpdateMLVersion: boolean; @@ -166,11 +173,13 @@ export class LocalMLSyncContext implements MLSyncContext { constructor( token: string, + userID: number, config: MLSyncConfig, shouldUpdateMLVersion: boolean = true, concurrency?: number ) { this.token = token; + this.userID = userID; this.config = config; this.shouldUpdateMLVersion = shouldUpdateMLVersion; diff --git a/src/services/machineLearning/machineLearningService.ts b/src/services/machineLearning/machineLearningService.ts index 5e817df44..64a5340c9 100644 --- a/src/services/machineLearning/machineLearningService.ts +++ b/src/services/machineLearning/machineLearningService.ts @@ -33,7 +33,6 @@ import ObjectService from './objectService'; import ReaderService from './readerService'; import { logError } from 'utils/sentry'; import { addLogLine } from 'utils/logging'; -import { getData, LS_KEYS } from 'utils/storage/localStorage'; class MachineLearningService { private initialized = false; // private faceDetectionService: FaceDetectionService; @@ -56,7 +55,7 @@ class MachineLearningService { // this.clusteringService = new ClusteringService(); } - public async sync(token: string): Promise { + public async sync(token: string, userID: number): Promise { if (!token) { throw Error('Token needed by ml service to sync file'); } @@ -67,7 +66,7 @@ class MachineLearningService { // needs to be cleaned using tf.dispose or tf.tidy // tf.engine().startScope(); - const syncContext = await this.getSyncContext(token); + const syncContext = await this.getSyncContext(token, userID); await this.syncLocalFiles(syncContext); @@ -124,16 +123,9 @@ class MachineLearningService { private async getLocalFilesMap(syncContext: MLSyncContext) { if (!syncContext.localFilesMap) { const localFiles = await getLocalFiles(); - const user = getData(LS_KEYS.USER); - if (!user) { - logError( - Error('user not found'), - 'User not found in local storage' - ); - return new Map(); - } + const personalFiles = localFiles.filter( - (f) => f.ownerID === user?.id + (f) => f.ownerID === syncContext.userID ); syncContext.localFilesMap = new Map(); personalFiles.forEach((f) => @@ -306,12 +298,12 @@ class MachineLearningService { // await this.disposeMLModels(); } - private async getSyncContext(token: string) { + private async getSyncContext(token: string, userID: number) { if (!this.syncContext) { addLogLine('Creating syncContext'); this.syncContext = getMLSyncConfig().then((mlSyncConfig) => - MLFactory.getMLSyncContext(token, mlSyncConfig, true) + MLFactory.getMLSyncContext(token, userID, mlSyncConfig, true) ); } else { addLogLine('reusing existing syncContext'); @@ -319,11 +311,11 @@ class MachineLearningService { return this.syncContext; } - private async getLocalSyncContext(token: string) { + private async getLocalSyncContext(token: string, userID: number) { if (!this.localSyncContext) { addLogLine('Creating localSyncContext'); this.localSyncContext = getMLSyncConfig().then((mlSyncConfig) => - MLFactory.getMLSyncContext(token, mlSyncConfig, false) + MLFactory.getMLSyncContext(token, userID, mlSyncConfig, false) ); } else { addLogLine('reusing existing localSyncContext'); @@ -342,11 +334,12 @@ class MachineLearningService { public async syncLocalFile( token: string, + userID: number, enteFile: EnteFile, localFile?: globalThis.File, textDetectionTimeoutIndex?: number ): Promise { - const syncContext = await this.getLocalSyncContext(token); + const syncContext = await this.getLocalSyncContext(token, userID); try { const mlFileData = await this.syncFileWithErrorHandler( diff --git a/src/services/machineLearning/mlWorkManager.ts b/src/services/machineLearning/mlWorkManager.ts index 24ac11599..eef9757f4 100644 --- a/src/services/machineLearning/mlWorkManager.ts +++ b/src/services/machineLearning/mlWorkManager.ts @@ -3,7 +3,7 @@ import PQueue from 'p-queue'; import { eventBus, Events } from 'services/events'; import { EnteFile } from 'types/file'; import { FILE_TYPE } from 'constants/file'; -import { getToken } from 'utils/common/key'; +import { getToken, getUserID } from 'utils/common/key'; import { logQueueStats } from 'utils/machineLearning'; import { getMLSyncJobConfig } from 'utils/machineLearning/config'; import { logError } from 'utils/sentry'; @@ -174,8 +174,9 @@ class MLWorkManager { const result = await this.liveSyncQueue.add(async () => { this.stopSyncJob(); const token = getToken(); + const userID = getUserID(); const mlWorker = await this.getLiveSyncWorker(); - return mlWorker.syncLocalFile(token, enteFile, localFile); + return mlWorker.syncLocalFile(token, userID, enteFile, localFile); }); if ('message' in result) { @@ -213,9 +214,10 @@ class MLWorkManager { } const token = getToken(); + const userID = getUserID(); const jobWorkerProxy = await this.getSyncJobWorker(); - const mlSyncResult = await jobWorkerProxy.sync(token); + const mlSyncResult = await jobWorkerProxy.sync(token, userID); // this.terminateSyncJobWorker(); const jobResult: MLSyncJobResult = { diff --git a/src/types/machineLearning/index.ts b/src/types/machineLearning/index.ts index 26f19c5a3..90f86bb54 100644 --- a/src/types/machineLearning/index.ts +++ b/src/types/machineLearning/index.ts @@ -317,6 +317,7 @@ export interface MLSearchConfig extends Config { export interface MLSyncContext { token: string; + userID: number; config: MLSyncConfig; shouldUpdateMLVersion: boolean; @@ -461,11 +462,12 @@ export interface MachineLearningWorker { syncLocalFile( token: string, + userID: number, enteFile: EnteFile, localFile: globalThis.File ): Promise; - sync(token: string): Promise; + sync(token: string, userID: number): Promise; close(): void; } diff --git a/src/utils/common/key.ts b/src/utils/common/key.ts index 48df6a137..02aa22c9d 100644 --- a/src/utils/common/key.ts +++ b/src/utils/common/key.ts @@ -23,3 +23,4 @@ export const getActualKey = async () => { }; export const getToken = () => getData(LS_KEYS.USER)?.token; +export const getUserID = () => getData(LS_KEYS.USER)?.id; diff --git a/src/worker/ml.worker.ts b/src/worker/ml.worker.ts index a091bd7a7..dc94ed3b6 100644 --- a/src/worker/ml.worker.ts +++ b/src/worker/ml.worker.ts @@ -26,14 +26,15 @@ export class DedicatedMLWorker implements MachineLearningWorker { public async syncLocalFile( token: string, + userID: number, enteFile: EnteFile, localFile: globalThis.File ) { - return mlService.syncLocalFile(token, enteFile, localFile); + return mlService.syncLocalFile(token, userID, enteFile, localFile); } - public async sync(token: string) { - return mlService.sync(token); + public async sync(token: string, userID: number) { + return mlService.sync(token, userID); } public close() {