summaryrefslogtreecommitdiff
path: root/src/services/chart/charts/classes/per-user-drive.ts
blob: 822f4eda0f38fe034c097014324efa913d92d231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import autobind from 'autobind-decorator';
import Chart, { Obj, DeepPartial } from '../../core';
import { SchemaType } from '../../../../misc/schema';
import { DriveFiles } from '../../../../models';
import { DriveFile } from '../../../../models/entities/drive-file';
import { name, schema } from '../schemas/per-user-drive';

type PerUserDriveLog = SchemaType<typeof schema>;

export default class PerUserDriveChart extends Chart<PerUserDriveLog> {
	constructor() {
		super(name, schema, true);
	}

	@autobind
	protected genNewLog(latest: PerUserDriveLog): DeepPartial<PerUserDriveLog> {
		return {
			totalCount: latest.totalCount,
			totalSize: latest.totalSize,
		};
	}

	@autobind
	protected async fetchActual(group: string): Promise<DeepPartial<PerUserDriveLog>> {
		const [count, size] = await Promise.all([
			DriveFiles.count({ userId: group }),
			DriveFiles.clacDriveUsageOf(group)
		]);

		return {
			totalCount: count,
			totalSize: size,
		};
	}

	@autobind
	public async update(file: DriveFile, isAdditional: boolean) {
		const update: Obj = {};

		update.totalCount = isAdditional ? 1 : -1;
		update.totalSize = isAdditional ? file.size : -file.size;
		if (isAdditional) {
			update.incCount = 1;
			update.incSize = file.size;
		} else {
			update.decCount = 1;
			update.decSize = file.size;
		}

		await this.inc(update, file.userId);
	}
}