summaryrefslogtreecommitdiff
path: root/src/chart/federation.ts
blob: 5bb41f00a28b51931400bf9656c971a24e385d8f (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
import autobind from 'autobind-decorator';
import Chart, { Obj } from '.';
import Instance from '../models/instance';

/**
 * フェデレーションに関するチャート
 */
type FederationLog = {
	instance: {
		/**
		 * インスタンス数の合計
		 */
		total: number;

		/**
		 * 増加インスタンス数
		 */
		inc: number;

		/**
		 * 減少インスタンス数
		 */
		dec: number;
	};
};

class FederationChart extends Chart<FederationLog> {
	constructor() {
		super('federation');
	}

	@autobind
	protected async getTemplate(init: boolean, latest?: FederationLog): Promise<FederationLog> {
		const [total] = init ? await Promise.all([
			Instance.count({})
		]) : [
			latest ? latest.instance.total : 0
		];

		return {
			instance: {
				total: total,
				inc: 0,
				dec: 0
			}
		};
	}

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

		update.total = isAdditional ? 1 : -1;
		if (isAdditional) {
			update.inc = 1;
		} else {
			update.dec = 1;
		}

		await this.inc({
			instance: update
		});
	}
}

export default new FederationChart();