import autobind from 'autobind-decorator'; import Chart, { Obj, DeepPartial } from '../../core'; import { SchemaType } from '../../../../misc/schema'; import { Users } from '../../../../models'; import { Not, IsNull } from 'typeorm'; import { User } from '../../../../models/entities/user'; import { name, schema } from '../schemas/users'; type UsersLog = SchemaType; export default class UsersChart extends Chart { constructor() { super(name, schema); } @autobind protected genNewLog(latest: UsersLog): DeepPartial { return { local: { total: latest.local.total, }, remote: { total: latest.remote.total, } }; } @autobind protected async fetchActual(): Promise> { const [localCount, remoteCount] = await Promise.all([ Users.count({ host: null }), Users.count({ host: Not(IsNull()) }) ]); return { local: { total: localCount, }, remote: { total: remoteCount, } }; } @autobind public async update(user: User, isAdditional: boolean) { const update: Obj = {}; update.total = isAdditional ? 1 : -1; if (isAdditional) { update.inc = 1; } else { update.dec = 1; } await this.inc({ [Users.isLocalUser(user) ? 'local' : 'remote']: update }); } }