2020-12-30 22:13:19 +02:00
|
|
|
import {Config} from '../src/common/config/private/Config';
|
|
|
|
import {ObjectManagers} from '../src/backend/model/ObjectManagers';
|
|
|
|
import {DiskMangerWorker} from '../src/backend/model/threading/DiskMangerWorker';
|
2023-01-06 00:11:58 +02:00
|
|
|
import {IndexingManager} from '../src/backend/model/database/IndexingManager';
|
2020-12-30 22:54:07 +02:00
|
|
|
import * as path from 'path';
|
2021-01-04 11:32:19 +02:00
|
|
|
import * as fs from 'fs';
|
2020-12-30 22:13:19 +02:00
|
|
|
import {Utils} from '../src/common/Utils';
|
2021-04-18 15:48:35 +02:00
|
|
|
import {DatabaseType, ReIndexingSensitivity} from '../src/common/config/private/PrivateConfig';
|
2020-12-30 22:13:19 +02:00
|
|
|
import {ProjectPath} from '../src/backend/ProjectPath';
|
|
|
|
import {Benchmark} from './Benchmark';
|
|
|
|
import {IndexingJob} from '../src/backend/model/jobs/jobs/IndexingJob';
|
|
|
|
import {IJob} from '../src/backend/model/jobs/jobs/IJob';
|
|
|
|
import {JobProgressStates} from '../src/common/entities/job/JobProgressDTO';
|
|
|
|
import {JobProgress} from '../src/backend/model/jobs/jobs/JobProgress';
|
|
|
|
import {ContentWrapper} from '../src/common/entities/ConentWrapper';
|
2023-01-06 00:11:58 +02:00
|
|
|
import {GalleryManager} from '../src/backend/model/database/GalleryManager';
|
|
|
|
import {PersonManager} from '../src/backend/model/database/PersonManager';
|
2020-12-31 13:35:28 +02:00
|
|
|
import {GalleryRouter} from '../src/backend/routes/GalleryRouter';
|
|
|
|
import {Express} from 'express';
|
|
|
|
import {PersonRouter} from '../src/backend/routes/PersonRouter';
|
2021-05-10 13:03:22 +02:00
|
|
|
import {
|
|
|
|
ANDSearchQuery,
|
|
|
|
ORSearchQuery,
|
|
|
|
SearchQueryDTO,
|
|
|
|
SearchQueryTypes,
|
|
|
|
SomeOfSearchQuery,
|
|
|
|
TextSearch,
|
|
|
|
TextSearchQueryMatchTypes,
|
|
|
|
TextSearchQueryTypes
|
|
|
|
} from '../src/common/entities/SearchQueryDTO';
|
2023-08-01 23:04:04 +02:00
|
|
|
import {defaultQueryKeywords, QueryKeywords, SearchQueryParser} from '../src/common/SearchQueryParser';
|
2022-01-15 12:32:46 +02:00
|
|
|
import {ParentDirectoryDTO} from '../src/common/entities/DirectoryDTO';
|
2020-12-30 22:13:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
export interface BenchmarkResult {
|
|
|
|
name: string;
|
2021-05-13 09:56:36 +02:00
|
|
|
experiment?: string;
|
2020-12-30 22:13:19 +02:00
|
|
|
duration: number;
|
|
|
|
contentWrapper?: ContentWrapper;
|
2022-03-21 23:29:00 +02:00
|
|
|
items?: any[];
|
2020-12-30 22:13:19 +02:00
|
|
|
subBenchmarks?: BenchmarkResult[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BMIndexingManager extends IndexingManager {
|
|
|
|
|
2021-06-27 19:33:37 +02:00
|
|
|
public async saveToDB(scannedDirectory: ParentDirectoryDTO): Promise<void> {
|
2020-12-30 22:13:19 +02:00
|
|
|
return super.saveToDB(scannedDirectory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:35:28 +02:00
|
|
|
|
|
|
|
class BMGalleryRouter extends GalleryRouter {
|
2021-04-18 15:48:35 +02:00
|
|
|
public static addDirectoryList(app: Express): void {
|
2020-12-31 13:35:28 +02:00
|
|
|
GalleryRouter.addDirectoryList(app);
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public static addSearch(app: Express): void {
|
2020-12-31 13:35:28 +02:00
|
|
|
GalleryRouter.addSearch(app);
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
public static addAutoComplete(app: Express): void {
|
2020-12-31 13:35:28 +02:00
|
|
|
GalleryRouter.addAutoComplete(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BMPersonRouter extends PersonRouter {
|
2021-04-18 15:48:35 +02:00
|
|
|
public static addGetPersons(app: Express): void {
|
2020-12-31 13:35:28 +02:00
|
|
|
PersonRouter.addGetPersons(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 22:13:19 +02:00
|
|
|
export class BenchmarkRunner {
|
2020-12-30 22:54:07 +02:00
|
|
|
inited = false;
|
2020-12-31 13:35:28 +02:00
|
|
|
private biggestDirPath: string = null;
|
|
|
|
private readonly requestTemplate: any = {
|
|
|
|
requestPipe: null,
|
|
|
|
params: {},
|
|
|
|
query: {},
|
|
|
|
session: {}
|
|
|
|
};
|
2020-12-30 22:13:19 +02:00
|
|
|
|
|
|
|
constructor(public RUNS: number) {
|
2022-12-28 20:12:18 +02:00
|
|
|
Config.Users.authenticationRequired = false;
|
2020-12-30 22:13:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 09:56:36 +02:00
|
|
|
async bmSaveDirectory(): Promise<BenchmarkResult[]> {
|
2020-12-31 13:35:28 +02:00
|
|
|
const dir = await DiskMangerWorker.scanDirectory(this.biggestDirPath);
|
2022-02-23 00:50:28 +02:00
|
|
|
const bm = new Benchmark('Saving directory to DB', null,
|
|
|
|
(): Promise<void> => this.resetDB(), null,
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await this.init();
|
|
|
|
await this.setupDB();
|
|
|
|
});
|
2020-12-30 22:13:19 +02:00
|
|
|
bm.addAStep({
|
|
|
|
name: 'Saving directory to DB',
|
2021-04-18 15:48:35 +02:00
|
|
|
fn: (): Promise<void> => {
|
2020-12-30 22:13:19 +02:00
|
|
|
const im = new BMIndexingManager();
|
2021-06-27 19:33:37 +02:00
|
|
|
return im.saveToDB(dir as ParentDirectoryDTO);
|
2020-12-30 22:13:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return await bm.run(this.RUNS);
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:56:36 +02:00
|
|
|
async bmScanDirectory(): Promise<BenchmarkResult[]> {
|
2020-12-30 22:54:07 +02:00
|
|
|
await this.init();
|
2022-02-23 00:50:28 +02:00
|
|
|
const bm = new Benchmark('Scanning directory', {}, null,
|
|
|
|
null,
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await this.init();
|
|
|
|
});
|
2020-12-30 22:13:19 +02:00
|
|
|
bm.addAStep({
|
|
|
|
name: 'Scanning directory',
|
2021-04-18 15:48:35 +02:00
|
|
|
fn: async (): Promise<ContentWrapper> => new ContentWrapper(await DiskMangerWorker.scanDirectory(this.biggestDirPath))
|
2020-12-30 22:13:19 +02:00
|
|
|
});
|
|
|
|
return await bm.run(this.RUNS);
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:56:36 +02:00
|
|
|
async bmListDirectory(): Promise<BenchmarkResult[]> {
|
2020-12-31 13:35:28 +02:00
|
|
|
const req = Utils.clone(this.requestTemplate);
|
|
|
|
req.params.directory = this.biggestDirPath;
|
|
|
|
const bm = new Benchmark('List directory', req,
|
2021-04-18 15:48:35 +02:00
|
|
|
async (): Promise<void> => {
|
2020-12-30 22:13:19 +02:00
|
|
|
await ObjectManagers.reset();
|
|
|
|
await ObjectManagers.InitSQLManagers();
|
2022-02-23 00:50:28 +02:00
|
|
|
}, null,
|
|
|
|
async (): Promise<void> => {
|
2022-12-28 20:12:18 +02:00
|
|
|
Config.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
|
2022-02-23 00:50:28 +02:00
|
|
|
await this.init();
|
|
|
|
await this.setupDB();
|
2020-12-30 22:13:19 +02:00
|
|
|
});
|
2020-12-31 13:35:28 +02:00
|
|
|
BMGalleryRouter.addDirectoryList(bm.BmExpressApp);
|
2020-12-30 22:13:19 +02:00
|
|
|
return await bm.run(this.RUNS);
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:56:36 +02:00
|
|
|
async bmListPersons(): Promise<BenchmarkResult[]> {
|
2021-04-18 15:48:35 +02:00
|
|
|
const bm = new Benchmark('Listing Faces', Utils.clone(this.requestTemplate), async (): Promise<void> => {
|
2022-02-23 00:50:28 +02:00
|
|
|
await ObjectManagers.reset();
|
|
|
|
await ObjectManagers.InitSQLManagers();
|
|
|
|
}, null,
|
|
|
|
async (): Promise<void> => {
|
2022-12-28 20:12:18 +02:00
|
|
|
Config.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
|
2022-02-23 00:50:28 +02:00
|
|
|
await this.setupDB();
|
|
|
|
});
|
2020-12-31 13:35:28 +02:00
|
|
|
BMPersonRouter.addGetPersons(bm.BmExpressApp);
|
2020-12-30 22:13:19 +02:00
|
|
|
return await bm.run(this.RUNS);
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:56:36 +02:00
|
|
|
async bmAllSearch(): Promise<{ result: BenchmarkResult[], searchQuery: SearchQueryDTO }[]> {
|
2020-12-30 22:13:19 +02:00
|
|
|
await this.setupDB();
|
|
|
|
|
2023-08-01 23:04:04 +02:00
|
|
|
const queryParser = new SearchQueryParser(defaultQueryKeywords);
|
2021-05-10 13:03:22 +02:00
|
|
|
const names = (await ObjectManagers.getInstance().PersonManager.getAll()).sort((a, b) => b.count - a.count);
|
|
|
|
const queries: { query: SearchQueryDTO, description: string }[] = TextSearchQueryTypes.map(t => {
|
|
|
|
const q = {
|
|
|
|
type: t, text: 'a'
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
query: q, description: queryParser.stringify(q)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
// searching for everything
|
|
|
|
queries.push({
|
|
|
|
query: {
|
|
|
|
type: SearchQueryTypes.any_text, text: '.'
|
|
|
|
} as TextSearch, description: queryParser.stringify({
|
|
|
|
type: SearchQueryTypes.any_text, text: '.'
|
|
|
|
} as TextSearch)
|
|
|
|
});
|
|
|
|
if (names.length > 0) {
|
|
|
|
queries.push({
|
|
|
|
query: {
|
|
|
|
type: SearchQueryTypes.person, text: names[0].name,
|
|
|
|
matchType: TextSearchQueryMatchTypes.exact_match
|
|
|
|
} as TextSearch, description: '<Most common name>'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (names.length > 1) {
|
|
|
|
queries.push({
|
|
|
|
query: {
|
|
|
|
type: SearchQueryTypes.AND, list: [
|
|
|
|
{
|
|
|
|
type: SearchQueryTypes.person, text: names[0].name,
|
|
|
|
matchType: TextSearchQueryMatchTypes.exact_match
|
|
|
|
} as TextSearch,
|
|
|
|
{
|
|
|
|
type: SearchQueryTypes.person, text: names[1].name,
|
|
|
|
matchType: TextSearchQueryMatchTypes.exact_match
|
|
|
|
} as TextSearch
|
|
|
|
]
|
|
|
|
} as ANDSearchQuery, description: '<Most AND second common names>'
|
|
|
|
});
|
|
|
|
queries.push({
|
|
|
|
query: {
|
|
|
|
type: SearchQueryTypes.OR, list: [
|
|
|
|
{
|
|
|
|
type: SearchQueryTypes.person, text: names[0].name,
|
|
|
|
matchType: TextSearchQueryMatchTypes.exact_match
|
|
|
|
} as TextSearch,
|
|
|
|
{
|
|
|
|
type: SearchQueryTypes.person, text: names[1].name,
|
|
|
|
matchType: TextSearchQueryMatchTypes.exact_match
|
|
|
|
} as TextSearch
|
|
|
|
]
|
|
|
|
} as ORSearchQuery, description: '<Most OR second common names>'
|
|
|
|
});
|
|
|
|
queries.push({
|
|
|
|
query: {
|
|
|
|
type: SearchQueryTypes.SOME_OF,
|
|
|
|
min: 2,
|
|
|
|
list: names.map(n => ({
|
|
|
|
type: SearchQueryTypes.person, text: n.name,
|
|
|
|
matchType: TextSearchQueryMatchTypes.exact_match
|
|
|
|
} as TextSearch))
|
|
|
|
} as SomeOfSearchQuery, description: '<Contain at least 2 out of all names>'
|
|
|
|
});
|
|
|
|
}
|
2021-05-13 09:56:36 +02:00
|
|
|
const results: { result: BenchmarkResult[], searchQuery: SearchQueryDTO }[] = [];
|
2021-05-10 13:03:22 +02:00
|
|
|
|
|
|
|
for (const entry of queries) {
|
2020-12-31 13:35:28 +02:00
|
|
|
const req = Utils.clone(this.requestTemplate);
|
2021-05-10 13:03:22 +02:00
|
|
|
req.params.searchQueryDTO = JSON.stringify(entry.query);
|
|
|
|
|
2022-02-23 00:50:28 +02:00
|
|
|
const bm = new Benchmark('Searching for `' + entry.description + '`', req, null, null,
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await this.setupDB();
|
|
|
|
});
|
2020-12-31 13:35:28 +02:00
|
|
|
BMGalleryRouter.addSearch(bm.BmExpressApp);
|
|
|
|
|
2021-05-10 13:03:22 +02:00
|
|
|
results.push({result: await bm.run(this.RUNS), searchQuery: entry.query});
|
2020-12-30 22:13:19 +02:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-13 09:56:36 +02:00
|
|
|
async bmAutocomplete(text: string): Promise<BenchmarkResult[]> {
|
2020-12-31 13:35:28 +02:00
|
|
|
const req = Utils.clone(this.requestTemplate);
|
|
|
|
req.params.text = text;
|
2022-02-23 00:50:28 +02:00
|
|
|
const bm = new Benchmark('Auto complete for `' + text + '`', req, null, null,
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await this.setupDB();
|
|
|
|
});
|
2020-12-31 13:35:28 +02:00
|
|
|
BMGalleryRouter.addAutoComplete(bm.BmExpressApp);
|
2020-12-30 22:13:19 +02:00
|
|
|
return await bm.run(this.RUNS);
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
async getStatistic(): Promise<string> {
|
2020-12-30 22:54:07 +02:00
|
|
|
await this.setupDB();
|
|
|
|
const gm = new GalleryManager();
|
|
|
|
const pm = new PersonManager();
|
|
|
|
|
2020-12-31 13:35:28 +02:00
|
|
|
|
2020-12-30 22:54:07 +02:00
|
|
|
return 'directories: ' + await gm.countDirectories() +
|
|
|
|
', photos: ' + await gm.countPhotos() +
|
|
|
|
', videos: ' + await gm.countVideos() +
|
2020-12-31 13:35:28 +02:00
|
|
|
', diskUsage : ' + Utils.renderDataSize(await gm.countMediaSize()) +
|
2020-12-30 22:54:07 +02:00
|
|
|
', persons : ' + await pm.countFaces() +
|
|
|
|
', unique persons (faces): ' + (await pm.getAll()).length;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
private async init(): Promise<string> {
|
2020-12-30 22:54:07 +02:00
|
|
|
if (this.inited === false) {
|
|
|
|
await this.setupDB();
|
|
|
|
|
|
|
|
const gm = new GalleryManager();
|
|
|
|
let biggest = 0;
|
|
|
|
let biggestPath = '/';
|
|
|
|
const queue = ['/'];
|
|
|
|
while (queue.length > 0) {
|
|
|
|
const dirPath = queue.shift();
|
|
|
|
const dir = await gm.listDirectory(dirPath);
|
2021-04-18 15:48:35 +02:00
|
|
|
dir.directories.forEach((d): number => queue.push(path.join(d.path + d.name)));
|
2020-12-30 22:54:07 +02:00
|
|
|
if (biggest < dir.media.length) {
|
|
|
|
biggestPath = path.join(dir.path + dir.name);
|
|
|
|
biggest = dir.media.length;
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 13:35:28 +02:00
|
|
|
this.biggestDirPath = biggestPath;
|
|
|
|
console.log('updating path of biggest dir to: ' + this.biggestDirPath);
|
2020-12-30 22:54:07 +02:00
|
|
|
this.inited = true;
|
|
|
|
}
|
2020-12-31 13:35:28 +02:00
|
|
|
return this.biggestDirPath;
|
2020-12-30 22:54:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-30 22:13:19 +02:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
private resetDB = async (): Promise<void> => {
|
2020-12-30 22:13:19 +02:00
|
|
|
Config.Server.Threading.enabled = false;
|
|
|
|
await ObjectManagers.reset();
|
2022-01-15 12:32:46 +02:00
|
|
|
await fs.promises.rm(ProjectPath.DBFolder, {recursive: true, force: true});
|
2022-12-28 20:12:18 +02:00
|
|
|
Config.Database.type = DatabaseType.sqlite;
|
|
|
|
Config.Jobs.scheduled = [];
|
2020-12-30 22:13:19 +02:00
|
|
|
await ObjectManagers.InitSQLManagers();
|
|
|
|
};
|
|
|
|
|
2021-01-04 11:32:19 +02:00
|
|
|
private async setupDB(): Promise<void> {
|
2020-12-30 22:13:19 +02:00
|
|
|
Config.Server.Threading.enabled = false;
|
2021-01-04 11:32:19 +02:00
|
|
|
await this.resetDB();
|
2021-04-18 15:48:35 +02:00
|
|
|
await new Promise<void>((resolve, reject): void => {
|
2020-12-30 22:13:19 +02:00
|
|
|
try {
|
|
|
|
const indexingJob = new IndexingJob();
|
|
|
|
|
|
|
|
indexingJob.JobListener = {
|
2021-04-18 15:48:35 +02:00
|
|
|
onJobFinished: (job: IJob<any>, state: JobProgressStates, soloRun: boolean): void => {
|
2020-12-30 22:13:19 +02:00
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
onProgressUpdate: (progress: JobProgress): void => {
|
2022-12-28 20:12:18 +02:00
|
|
|
// empty
|
2020-12-30 22:13:19 +02:00
|
|
|
}
|
|
|
|
};
|
2021-05-12 13:56:10 +02:00
|
|
|
indexingJob.start({indexChangesOnly: false}).catch(console.error);
|
2020-12-30 22:13:19 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|