fix getData returning null
This commit is contained in:
parent
20cef72174
commit
74e8bb55c0
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<MLSyncResult> {
|
||||
public async sync(token: string, userID: number): Promise<MLSyncResult> {
|
||||
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<number, EnteFile>();
|
||||
}
|
||||
|
||||
const personalFiles = localFiles.filter(
|
||||
(f) => f.ownerID === user?.id
|
||||
(f) => f.ownerID === syncContext.userID
|
||||
);
|
||||
syncContext.localFilesMap = new Map<number, EnteFile>();
|
||||
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<MlFileData | Error> {
|
||||
const syncContext = await this.getLocalSyncContext(token);
|
||||
const syncContext = await this.getLocalSyncContext(token, userID);
|
||||
|
||||
try {
|
||||
const mlFileData = await this.syncFileWithErrorHandler(
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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<MlFileData | Error>;
|
||||
|
||||
sync(token: string): Promise<MLSyncResult>;
|
||||
sync(token: string, userID: number): Promise<MLSyncResult>;
|
||||
|
||||
close(): void;
|
||||
}
|
||||
|
|
|
@ -23,3 +23,4 @@ export const getActualKey = async () => {
|
|||
};
|
||||
|
||||
export const getToken = () => getData(LS_KEYS.USER)?.token;
|
||||
export const getUserID = () => getData(LS_KEYS.USER)?.id;
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue