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