1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/src/backend/model/database/sql/PersonManager.ts

125 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-01-14 00:38:39 +08:00
import {SQLConnection} from './SQLConnection';
2019-02-05 06:46:27 +08:00
import {PersonEntry} from './enitites/PersonEntry';
2019-02-15 07:25:55 +08:00
import {FaceRegionEntry} from './enitites/FaceRegionEntry';
import {PersonDTO} from '../../../../common/entities/PersonDTO';
2020-12-31 04:54:07 +08:00
import {ISQLPersonManager} from './IPersonManager';
2019-01-14 00:38:39 +08:00
2020-12-31 04:54:07 +08:00
export class PersonManager implements ISQLPersonManager {
// samplePhotos: { [key: string]: PhotoDTO } = {};
persons: PersonEntry[] = null;
2019-01-14 00:38:39 +08:00
2019-03-04 04:17:42 +08:00
async updatePerson(name: string, partialPerson: PersonDTO): Promise<PersonEntry> {
const connection = await SQLConnection.getConnection();
const repository = connection.getRepository(PersonEntry);
const person = await repository.createQueryBuilder('person')
.limit(1)
.where('person.name LIKE :name COLLATE utf8_general_ci', {name: name}).getOne();
if (typeof partialPerson.name !== 'undefined') {
person.name = partialPerson.name;
}
if (typeof partialPerson.isFavourite !== 'undefined') {
person.isFavourite = partialPerson.isFavourite;
}
await repository.save(person);
await this.loadAll();
return person;
}
2019-02-15 07:25:55 +08:00
private async loadAll(): Promise<void> {
2019-02-05 06:46:27 +08:00
const connection = await SQLConnection.getConnection();
const personRepository = connection.getRepository(PersonEntry);
this.persons = await personRepository.find({
relations: ['sampleRegion',
'sampleRegion.media',
'sampleRegion.media.directory']
});
2019-02-05 06:46:27 +08:00
}
public async getAll(): Promise<PersonEntry[]> {
if (this.persons === null) {
await this.loadAll();
}
2019-02-15 07:25:55 +08:00
return this.persons;
}
2019-02-05 06:46:27 +08:00
/**
* Used for statistic
*/
public async countFaces(): Promise<number> {
2020-12-31 04:54:07 +08:00
const connection = await SQLConnection.getConnection();
return await connection.getRepository(FaceRegionEntry)
.createQueryBuilder('faceRegion')
.getCount();
}
public async get(name: string): Promise<PersonEntry> {
if (this.persons === null) {
await this.loadAll();
2019-01-14 00:38:39 +08:00
}
return this.persons.find(p => p.name === name);
2019-01-14 00:38:39 +08:00
}
public async saveAll(names: string[]): Promise<void> {
2019-01-14 00:38:39 +08:00
const toSave: { name: string }[] = [];
const connection = await SQLConnection.getConnection();
const personRepository = connection.getRepository(PersonEntry);
2019-02-05 06:46:27 +08:00
await this.loadAll();
2019-01-14 00:38:39 +08:00
for (let i = 0; i < names.length; i++) {
const person = this.persons.find(p => p.name === names[i]);
if (!person) {
toSave.push({name: names[i]});
}
}
if (toSave.length > 0) {
for (let i = 0; i < toSave.length / 200; i++) {
await personRepository.insert(toSave.slice(i * 200, (i + 1) * 200));
}
await this.loadAll();
2019-01-14 00:38:39 +08:00
}
}
2019-03-11 03:57:27 +08:00
public async onGalleryIndexUpdate() {
await this.updateCounts();
await this.updateSamplePhotos();
2019-03-11 03:57:27 +08:00
}
private async updateCounts() {
2019-02-15 07:25:55 +08:00
const connection = await SQLConnection.getConnection();
await connection.query('update person_entry set count = ' +
' (select COUNT(1) from face_region_entry where face_region_entry.personId = person_entry.id)');
// remove persons without photo
await connection.getRepository(PersonEntry)
.createQueryBuilder()
.where('count = 0')
.delete()
.execute();
2019-02-15 07:25:55 +08:00
}
private async updateSamplePhotos() {
const connection = await SQLConnection.getConnection();
await connection.query('update person_entry set sampleRegionId = ' +
'(Select face_region_entry.id from media_entity ' +
'left join face_region_entry on media_entity.id = face_region_entry.mediaId ' +
'where face_region_entry.personId=person_entry.id ' +
'order by media_entity.metadataCreationdate desc ' +
'limit 1)');
}
2019-01-14 00:38:39 +08:00
}