enhance(backend): improve chart engine
This commit is contained in:
66
packages/backend/src/services/chart/charts/federation.ts
Normal file
66
packages/backend/src/services/chart/charts/federation.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import autobind from 'autobind-decorator';
|
||||
import Chart, { Obj, DeepPartial } from '../core';
|
||||
import { SchemaType } from '@/misc/schema';
|
||||
import { Instances } from '@/models/index';
|
||||
import { name, schema } from './entities/federation';
|
||||
|
||||
type FederationLog = SchemaType<typeof schema>;
|
||||
|
||||
/**
|
||||
* フェデレーションに関するチャート
|
||||
*/
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
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): Promise<void> {
|
||||
const update: Obj = {};
|
||||
|
||||
update.total = isAdditional ? 1 : -1;
|
||||
if (isAdditional) {
|
||||
update.inc = 1;
|
||||
} else {
|
||||
update.dec = 1;
|
||||
}
|
||||
|
||||
await this.inc({
|
||||
instance: update,
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user