iceshrimp/packages/backend/src/services/chart/charts/users.ts
syuilo 94228778c9 refactor: Use ESM (#8358)
* wip

* wip

* fix

* clean up

* Update tsconfig.json

* Update activitypub.ts

* wip
2022-02-27 11:07:39 +09:00

41 lines
1.1 KiB
TypeScript

import Chart, { KVs } from '../core.js';
import { Users } from '@/models/index.js';
import { Not, IsNull } from 'typeorm';
import { User } from '@/models/entities/user.js';
import { name, schema } from './entities/users.js';
/**
* ユーザー数に関するチャート
*/
// eslint-disable-next-line import/no-default-export
export default class UsersChart extends Chart<typeof schema> {
constructor() {
super(name, schema);
}
protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
const [localCount, remoteCount] = await Promise.all([
Users.count({ host: null }),
Users.count({ host: Not(IsNull()) }),
]);
return {
'local.total': localCount,
'remote.total': remoteCount,
};
}
protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
return {};
}
public async update(user: { id: User['id'], host: User['host'] }, isAdditional: boolean): Promise<void> {
const prefix = Users.isLocalUser(user) ? 'local' : 'remote';
await this.commit({
[`${prefix}.total`]: isAdditional ? 1 : -1,
[`${prefix}.inc`]: isAdditional ? 1 : 0,
[`${prefix}.dec`]: isAdditional ? 0 : 1,
});
}
}