summaryrefslogtreecommitdiff
path: root/packages/backend/src/services/chart/charts/active-users.ts
blob: 9490101e36ba462dc028f0aed0be0cce89c16391 (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 { User } from '@/models/entities/user';
import { SchemaType } from '@/misc/schema';
import { Users } from '@/models/index';
import { name, schema } from './entities/active-users';

type ActiveUsersLog = SchemaType<typeof schema>;

/**
 * アクティブユーザーに関するチャート
 */
// eslint-disable-next-line import/no-default-export
export default class ActiveUsersChart extends Chart<ActiveUsersLog> {
	constructor() {
		super(name, schema);
	}

	@autobind
	protected genNewLog(latest: ActiveUsersLog): DeepPartial<ActiveUsersLog> {
		return {};
	}

	@autobind
	protected aggregate(logs: ActiveUsersLog[]): ActiveUsersLog {
		return {
			local: {
				users: logs.reduce((a, b) => a.concat(b.local.users), [] as ActiveUsersLog['local']['users']),
			},
			remote: {
				users: logs.reduce((a, b) => a.concat(b.remote.users), [] as ActiveUsersLog['remote']['users']),
			},
		};
	}

	@autobind
	protected async fetchActual(): Promise<DeepPartial<ActiveUsersLog>> {
		return {};
	}

	@autobind
	public async update(user: { id: User['id'], host: User['host'] }): Promise<void> {
		const update: Obj = {
			users: [user.id],
		};

		await this.inc({
			[Users.isLocalUser(user) ? 'local' : 'remote']: update,
		});
	}
}