2023-12-14 17:55:40 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-03-20 21:20:38 +01:00
|
|
|
import { SystemConfigCore } from 'src/cores/system-config.core';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAssetRepository, WithoutProperty } from 'src/interfaces/asset.interface';
|
|
|
|
import { DatabaseLock, IDatabaseRepository } from 'src/interfaces/database.interface';
|
2024-03-21 04:15:09 +01:00
|
|
|
import {
|
|
|
|
IBaseJob,
|
|
|
|
IEntityJob,
|
|
|
|
IJobRepository,
|
|
|
|
JOBS_ASSET_PAGINATION_SIZE,
|
|
|
|
JobName,
|
|
|
|
JobStatus,
|
|
|
|
QueueName,
|
2024-03-21 12:59:49 +01:00
|
|
|
} from 'src/interfaces/job.interface';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IMachineLearningRepository } from 'src/interfaces/machine-learning.interface';
|
|
|
|
import { ISearchRepository } from 'src/interfaces/search.interface';
|
2024-05-16 00:58:23 +02:00
|
|
|
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
2024-05-14 21:31:36 +02:00
|
|
|
import { isSmartSearchEnabled } from 'src/utils/misc';
|
2024-03-21 04:15:09 +01:00
|
|
|
import { usePagination } from 'src/utils/pagination';
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SmartInfoService {
|
2023-08-25 06:15:03 +02:00
|
|
|
private configCore: SystemConfigCore;
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
constructor(
|
2023-03-20 16:55:28 +01:00
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
2023-12-28 00:36:51 +01:00
|
|
|
@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository,
|
2023-03-18 14:44:42 +01:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-02-25 15:12:03 +01:00
|
|
|
@Inject(IMachineLearningRepository) private machineLearning: IMachineLearningRepository,
|
2024-02-13 02:50:47 +01:00
|
|
|
@Inject(ISearchRepository) private repository: ISearchRepository,
|
2024-05-16 00:58:23 +02:00
|
|
|
@Inject(ISystemMetadataRepository) systemMetadataRepository: ISystemMetadataRepository,
|
2024-04-16 23:30:31 +02:00
|
|
|
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
2023-08-25 06:15:03 +02:00
|
|
|
) {
|
2024-04-16 23:30:31 +02:00
|
|
|
this.logger.setContext(SmartInfoService.name);
|
2024-05-16 00:58:23 +02:00
|
|
|
this.configCore = SystemConfigCore.create(systemMetadataRepository, this.logger);
|
2023-08-25 06:15:03 +02:00
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-12-08 17:15:46 +01:00
|
|
|
async init() {
|
2023-12-16 17:50:46 +01:00
|
|
|
await this.jobRepository.pause(QueueName.SMART_SEARCH);
|
2023-12-08 17:15:46 +01:00
|
|
|
|
2024-01-18 06:08:48 +01:00
|
|
|
await this.jobRepository.waitForQueueCompletion(QueueName.SMART_SEARCH);
|
2023-12-08 17:15:46 +01:00
|
|
|
|
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
|
|
|
|
2023-12-28 00:36:51 +01:00
|
|
|
await this.databaseRepository.withLock(DatabaseLock.CLIPDimSize, () =>
|
|
|
|
this.repository.init(machineLearning.clip.modelName),
|
|
|
|
);
|
2023-12-08 17:15:46 +01:00
|
|
|
|
2023-12-16 17:50:46 +01:00
|
|
|
await this.jobRepository.resume(QueueName.SMART_SEARCH);
|
2023-12-08 17:15:46 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleQueueEncodeClip({ force }: IBaseJob): Promise<JobStatus> {
|
2023-08-25 06:15:03 +02:00
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
2024-05-14 21:31:36 +02:00
|
|
|
if (!isSmartSearchEnabled(machineLearning)) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SKIPPED;
|
2023-08-25 06:15:03 +02:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:24:23 +01:00
|
|
|
if (force) {
|
|
|
|
await this.repository.deleteAllSearchEmbeddings();
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
return force
|
2024-04-19 03:37:55 +02:00
|
|
|
? this.assetRepository.getAll(pagination, { isVisible: true })
|
2024-01-29 15:51:22 +01:00
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.SMART_SEARCH);
|
2023-05-26 21:43:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
2024-01-29 15:51:22 +01:00
|
|
|
await this.jobRepository.queueAll(
|
|
|
|
assets.map((asset) => ({ name: JobName.SMART_SEARCH, data: { id: asset.id } })),
|
|
|
|
);
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
2023-05-26 21:43:24 +02:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
2023-03-18 14:44:42 +01:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleEncodeClip({ id }: IEntityJob): Promise<JobStatus> {
|
2023-08-25 06:15:03 +02:00
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
2024-05-14 21:31:36 +02:00
|
|
|
if (!isSmartSearchEnabled(machineLearning)) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SKIPPED;
|
2023-08-25 06:15:03 +02:00
|
|
|
}
|
2023-03-18 14:44:42 +01:00
|
|
|
|
2023-08-25 06:15:03 +02:00
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
2024-03-14 06:58:09 +01:00
|
|
|
if (!asset) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2024-03-14 06:58:09 +01:00
|
|
|
}
|
|
|
|
|
2024-04-19 03:37:55 +02:00
|
|
|
if (!asset.isVisible) {
|
|
|
|
return JobStatus.SKIPPED;
|
|
|
|
}
|
|
|
|
|
2024-04-02 06:56:56 +02:00
|
|
|
if (!asset.previewPath) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-03-18 14:44:42 +01:00
|
|
|
}
|
|
|
|
|
2023-08-29 15:58:00 +02:00
|
|
|
const clipEmbedding = await this.machineLearning.encodeImage(
|
|
|
|
machineLearning.url,
|
2024-04-02 06:56:56 +02:00
|
|
|
{ imagePath: asset.previewPath },
|
2023-08-29 15:58:00 +02:00
|
|
|
machineLearning.clip,
|
|
|
|
);
|
|
|
|
|
2023-12-28 00:36:51 +01:00
|
|
|
if (this.databaseRepository.isBusy(DatabaseLock.CLIPDimSize)) {
|
|
|
|
this.logger.verbose(`Waiting for CLIP dimension size to be updated`);
|
|
|
|
await this.databaseRepository.wait(DatabaseLock.CLIPDimSize);
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:37:06 +01:00
|
|
|
await this.repository.upsert(asset.id, clipEmbedding);
|
2023-05-26 21:43:24 +02:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-03-18 14:44:42 +01:00
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|