summaryrefslogtreecommitdiff
path: root/src/services/chart/charts/classes/drive.ts
blob: c3bcacb7df32b8dc7d1e1189da3bae158e933c7e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import autobind from 'autobind-decorator';
import Chart, { Obj, DeepPartial } from '../../core';
import { SchemaType } from '../../../../misc/schema';
import { DriveFiles } from '../../../../models';
import { Not, IsNull } from 'typeorm';
import { DriveFile } from '../../../../models/entities/drive-file';
import { name, schema } from '../schemas/drive';

type DriveLog = SchemaType<typeof schema>;

export default class DriveChart extends Chart<DriveLog> {
	constructor() {
		super(name, schema);
	}

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

	@autobind
	protected async fetchActual(): Promise<DeepPartial<DriveLog>> {
		const [localCount, remoteCount, localSize, remoteSize] = await Promise.all([
			DriveFiles.count({ userHost: null }),
			DriveFiles.count({ userHost: Not(IsNull()) }),
			DriveFiles.clacDriveUsageOfLocal(),
			DriveFiles.clacDriveUsageOfRemote()
		]);

		return {
			local: {
				totalCount: localCount,
				totalSize: localSize,
			},
			remote: {
				totalCount: remoteCount,
				totalSize: remoteSize,
			}
		};
	}

	@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({
			[file.userHost === null ? 'local' : 'remote']: update
		});
	}
}