fix getData returning null

This commit is contained in:
Abhinav 2023-02-10 18:41:36 +05:30
parent 20cef72174
commit 74e8bb55c0
7 changed files with 36 additions and 27 deletions

View file

@ -1,7 +1,7 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Button, Spinner } from 'react-bootstrap'; import { Button, Spinner } from 'react-bootstrap';
import { EnteFile } from 'types/file'; import { EnteFile } from 'types/file';
import { getToken } from 'utils/common/key'; import { getToken, getUserID } from 'utils/common/key';
import mlService from '../../services/machineLearning/machineLearningService'; import mlService from '../../services/machineLearning/machineLearningService';
function MLServiceFileInfoButton({ function MLServiceFileInfoButton({
@ -18,9 +18,10 @@ function MLServiceFileInfoButton({
const runMLService = async () => { const runMLService = async () => {
setMlServiceRunning(true); setMlServiceRunning(true);
const token = getToken(); const token = getToken();
const userID = getUserID();
// index 4 is for timeout of 240 seconds // 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); setUpdateMLDataIndex(updateMLDataIndex + 1);
setMlServiceRunning(false); setMlServiceRunning(false);

View file

@ -122,15 +122,22 @@ export class MLFactory {
public static getMLSyncContext( public static getMLSyncContext(
token: string, token: string,
userID: number,
config: MLSyncConfig, config: MLSyncConfig,
shouldUpdateMLVersion: boolean = true shouldUpdateMLVersion: boolean = true
) { ) {
return new LocalMLSyncContext(token, config, shouldUpdateMLVersion); return new LocalMLSyncContext(
token,
userID,
config,
shouldUpdateMLVersion
);
} }
} }
export class LocalMLSyncContext implements MLSyncContext { export class LocalMLSyncContext implements MLSyncContext {
public token: string; public token: string;
public userID: number;
public config: MLSyncConfig; public config: MLSyncConfig;
public shouldUpdateMLVersion: boolean; public shouldUpdateMLVersion: boolean;
@ -166,11 +173,13 @@ export class LocalMLSyncContext implements MLSyncContext {
constructor( constructor(
token: string, token: string,
userID: number,
config: MLSyncConfig, config: MLSyncConfig,
shouldUpdateMLVersion: boolean = true, shouldUpdateMLVersion: boolean = true,
concurrency?: number concurrency?: number
) { ) {
this.token = token; this.token = token;
this.userID = userID;
this.config = config; this.config = config;
this.shouldUpdateMLVersion = shouldUpdateMLVersion; this.shouldUpdateMLVersion = shouldUpdateMLVersion;

View file

@ -33,7 +33,6 @@ import ObjectService from './objectService';
import ReaderService from './readerService'; import ReaderService from './readerService';
import { logError } from 'utils/sentry'; import { logError } from 'utils/sentry';
import { addLogLine } from 'utils/logging'; import { addLogLine } from 'utils/logging';
import { getData, LS_KEYS } from 'utils/storage/localStorage';
class MachineLearningService { class MachineLearningService {
private initialized = false; private initialized = false;
// private faceDetectionService: FaceDetectionService; // private faceDetectionService: FaceDetectionService;
@ -56,7 +55,7 @@ class MachineLearningService {
// this.clusteringService = new ClusteringService(); // this.clusteringService = new ClusteringService();
} }
public async sync(token: string): Promise<MLSyncResult> { public async sync(token: string, userID: number): Promise<MLSyncResult> {
if (!token) { if (!token) {
throw Error('Token needed by ml service to sync file'); 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 // needs to be cleaned using tf.dispose or tf.tidy
// tf.engine().startScope(); // tf.engine().startScope();
const syncContext = await this.getSyncContext(token); const syncContext = await this.getSyncContext(token, userID);
await this.syncLocalFiles(syncContext); await this.syncLocalFiles(syncContext);
@ -124,16 +123,9 @@ class MachineLearningService {
private async getLocalFilesMap(syncContext: MLSyncContext) { private async getLocalFilesMap(syncContext: MLSyncContext) {
if (!syncContext.localFilesMap) { if (!syncContext.localFilesMap) {
const localFiles = await getLocalFiles(); 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<number, EnteFile>();
}
const personalFiles = localFiles.filter( const personalFiles = localFiles.filter(
(f) => f.ownerID === user?.id (f) => f.ownerID === syncContext.userID
); );
syncContext.localFilesMap = new Map<number, EnteFile>(); syncContext.localFilesMap = new Map<number, EnteFile>();
personalFiles.forEach((f) => personalFiles.forEach((f) =>
@ -306,12 +298,12 @@ class MachineLearningService {
// await this.disposeMLModels(); // await this.disposeMLModels();
} }
private async getSyncContext(token: string) { private async getSyncContext(token: string, userID: number) {
if (!this.syncContext) { if (!this.syncContext) {
addLogLine('Creating syncContext'); addLogLine('Creating syncContext');
this.syncContext = getMLSyncConfig().then((mlSyncConfig) => this.syncContext = getMLSyncConfig().then((mlSyncConfig) =>
MLFactory.getMLSyncContext(token, mlSyncConfig, true) MLFactory.getMLSyncContext(token, userID, mlSyncConfig, true)
); );
} else { } else {
addLogLine('reusing existing syncContext'); addLogLine('reusing existing syncContext');
@ -319,11 +311,11 @@ class MachineLearningService {
return this.syncContext; return this.syncContext;
} }
private async getLocalSyncContext(token: string) { private async getLocalSyncContext(token: string, userID: number) {
if (!this.localSyncContext) { if (!this.localSyncContext) {
addLogLine('Creating localSyncContext'); addLogLine('Creating localSyncContext');
this.localSyncContext = getMLSyncConfig().then((mlSyncConfig) => this.localSyncContext = getMLSyncConfig().then((mlSyncConfig) =>
MLFactory.getMLSyncContext(token, mlSyncConfig, false) MLFactory.getMLSyncContext(token, userID, mlSyncConfig, false)
); );
} else { } else {
addLogLine('reusing existing localSyncContext'); addLogLine('reusing existing localSyncContext');
@ -342,11 +334,12 @@ class MachineLearningService {
public async syncLocalFile( public async syncLocalFile(
token: string, token: string,
userID: number,
enteFile: EnteFile, enteFile: EnteFile,
localFile?: globalThis.File, localFile?: globalThis.File,
textDetectionTimeoutIndex?: number textDetectionTimeoutIndex?: number
): Promise<MlFileData | Error> { ): Promise<MlFileData | Error> {
const syncContext = await this.getLocalSyncContext(token); const syncContext = await this.getLocalSyncContext(token, userID);
try { try {
const mlFileData = await this.syncFileWithErrorHandler( const mlFileData = await this.syncFileWithErrorHandler(

View file

@ -3,7 +3,7 @@ import PQueue from 'p-queue';
import { eventBus, Events } from 'services/events'; import { eventBus, Events } from 'services/events';
import { EnteFile } from 'types/file'; import { EnteFile } from 'types/file';
import { FILE_TYPE } from 'constants/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 { logQueueStats } from 'utils/machineLearning';
import { getMLSyncJobConfig } from 'utils/machineLearning/config'; import { getMLSyncJobConfig } from 'utils/machineLearning/config';
import { logError } from 'utils/sentry'; import { logError } from 'utils/sentry';
@ -174,8 +174,9 @@ class MLWorkManager {
const result = await this.liveSyncQueue.add(async () => { const result = await this.liveSyncQueue.add(async () => {
this.stopSyncJob(); this.stopSyncJob();
const token = getToken(); const token = getToken();
const userID = getUserID();
const mlWorker = await this.getLiveSyncWorker(); const mlWorker = await this.getLiveSyncWorker();
return mlWorker.syncLocalFile(token, enteFile, localFile); return mlWorker.syncLocalFile(token, userID, enteFile, localFile);
}); });
if ('message' in result) { if ('message' in result) {
@ -213,9 +214,10 @@ class MLWorkManager {
} }
const token = getToken(); const token = getToken();
const userID = getUserID();
const jobWorkerProxy = await this.getSyncJobWorker(); const jobWorkerProxy = await this.getSyncJobWorker();
const mlSyncResult = await jobWorkerProxy.sync(token); const mlSyncResult = await jobWorkerProxy.sync(token, userID);
// this.terminateSyncJobWorker(); // this.terminateSyncJobWorker();
const jobResult: MLSyncJobResult = { const jobResult: MLSyncJobResult = {

View file

@ -317,6 +317,7 @@ export interface MLSearchConfig extends Config {
export interface MLSyncContext { export interface MLSyncContext {
token: string; token: string;
userID: number;
config: MLSyncConfig; config: MLSyncConfig;
shouldUpdateMLVersion: boolean; shouldUpdateMLVersion: boolean;
@ -461,11 +462,12 @@ export interface MachineLearningWorker {
syncLocalFile( syncLocalFile(
token: string, token: string,
userID: number,
enteFile: EnteFile, enteFile: EnteFile,
localFile: globalThis.File localFile: globalThis.File
): Promise<MlFileData | Error>; ): Promise<MlFileData | Error>;
sync(token: string): Promise<MLSyncResult>; sync(token: string, userID: number): Promise<MLSyncResult>;
close(): void; close(): void;
} }

View file

@ -23,3 +23,4 @@ export const getActualKey = async () => {
}; };
export const getToken = () => getData(LS_KEYS.USER)?.token; export const getToken = () => getData(LS_KEYS.USER)?.token;
export const getUserID = () => getData(LS_KEYS.USER)?.id;

View file

@ -26,14 +26,15 @@ export class DedicatedMLWorker implements MachineLearningWorker {
public async syncLocalFile( public async syncLocalFile(
token: string, token: string,
userID: number,
enteFile: EnteFile, enteFile: EnteFile,
localFile: globalThis.File localFile: globalThis.File
) { ) {
return mlService.syncLocalFile(token, enteFile, localFile); return mlService.syncLocalFile(token, userID, enteFile, localFile);
} }
public async sync(token: string) { public async sync(token: string, userID: number) {
return mlService.sync(token); return mlService.sync(token, userID);
} }
public close() { public close() {