puyoskey/src/services/chart/charts/classes/federation.ts
syuilo b9cb6d1c10 refactor: refactoring imports
将来ESMに移行しやすいように
Related: #7658

なんかmochaが起動しなくなってるけど理由不明
すぐ直したい
2021-08-19 18:33:41 +09:00

62 lines
1.3 KiB
TypeScript

import autobind from 'autobind-decorator';
import Chart, { Obj, DeepPartial } from '../../core.js';
import { SchemaType } from '@/misc/schema.js';
import { Instances } from '@/models/index.js';
import { name, schema } from '../schemas/federation.js';
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 aggregate(logs: FederationLog[]): FederationLog {
return {
instance: {
total: logs[0].instance.total,
inc: logs.reduce((a, b) => a + b.instance.inc, 0),
dec: logs.reduce((a, b) => a + b.instance.dec, 0),
},
};
}
@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
});
}
}