summaryrefslogtreecommitdiff
path: root/src/services/chart/charts/classes/federation.ts
blob: bd2c497e7b8dfbdeaa05c4550908e536acaac255 (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
import autobind from 'autobind-decorator';
import Chart, { Obj, DeepPartial } from '../../core';
import { SchemaType } from '../../../../misc/schema';
import { Instances } from '../../../../models';
import { name, schema } from '../schemas/federation';

type FederationLog = SchemaType<typeof schema>;

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

	@autobind
	protected genNewLog(latest: FederationLog): DeepPartial<FederationLog> {
		return {
			instance: {
				total: latest.instance.total,
			}
		};
	}

	@autobind
	protected async fetchActual(): Promise<DeepPartial<FederationLog>> {
		const [total] = await Promise.all([
			Instances.count({})
		]);

		return {
			instance: {
				total: total,
			}
		};
	}

	@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
		});
	}
}