refactor(server): asset stats (#3253)

* refactor(server): asset stats

* chore: open api
This commit is contained in:
Jason Rasmussen
2023-07-14 09:30:17 -04:00
committed by GitHub
parent 05e1a6d949
commit f952bc0b64
29 changed files with 601 additions and 844 deletions

View File

@@ -1,5 +1,7 @@
import {
AssetSearchOptions,
AssetStats,
AssetStatsOptions,
IAssetRepository,
LivePhotoSearchOptions,
MapMarker,
@@ -321,4 +323,38 @@ export class AssetRepository implements IAssetRepository {
lon: asset.exifInfo!.longitude!,
}));
}
async getStatistics(ownerId: string, options: AssetStatsOptions): Promise<AssetStats> {
let builder = await this.repository
.createQueryBuilder('asset')
.select(`COUNT(asset.id)`, 'count')
.addSelect(`asset.type`, 'type')
.where('"ownerId" = :ownerId', { ownerId })
.andWhere('asset.isVisible = true')
.groupBy('asset.type');
const { isArchived, isFavorite } = options;
if (isArchived !== undefined) {
builder = builder.andWhere(`asset.isArchived = :isArchived`, { isArchived });
}
if (isFavorite !== undefined) {
builder = builder.andWhere(`asset.isFavorite = :isFavorite`, { isFavorite });
}
const items = await builder.getRawMany();
const result: AssetStats = {
[AssetType.AUDIO]: 0,
[AssetType.IMAGE]: 0,
[AssetType.VIDEO]: 0,
[AssetType.OTHER]: 0,
};
for (const item of items) {
result[item.type as AssetType] = Number(item.count) || 0;
}
return result;
}
}