Sharkey/packages/backend/src/core/chart/ChartManagementService.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-09-17 20:27:08 +02:00
import { Injectable, Inject } from '@nestjs/common';
import FederationChart from './charts/federation.js';
import NotesChart from './charts/notes.js';
import UsersChart from './charts/users.js';
import ActiveUsersChart from './charts/active-users.js';
import InstanceChart from './charts/instance.js';
import PerUserNotesChart from './charts/per-user-notes.js';
import DriveChart from './charts/drive.js';
import PerUserReactionsChart from './charts/per-user-reactions.js';
import HashtagChart from './charts/hashtag.js';
import PerUserFollowingChart from './charts/per-user-following.js';
import PerUserDriveChart from './charts/per-user-drive.js';
import ApRequestChart from './charts/ap-request.js';
import type { OnApplicationShutdown } from '@nestjs/common';
import { bindThis } from '@/decorators.js';
2022-09-17 20:27:08 +02:00
@Injectable()
export class ChartManagementService implements OnApplicationShutdown {
private charts;
private saveIntervalId: NodeJS.Timer;
2022-09-17 20:27:08 +02:00
constructor(
private federationChart: FederationChart,
private notesChart: NotesChart,
private usersChart: UsersChart,
private activeUsersChart: ActiveUsersChart,
private instanceChart: InstanceChart,
private perUserNotesChart: PerUserNotesChart,
private driveChart: DriveChart,
private perUserReactionsChart: PerUserReactionsChart,
private hashtagChart: HashtagChart,
private perUserFollowingChart: PerUserFollowingChart,
private perUserDriveChart: PerUserDriveChart,
private apRequestChart: ApRequestChart,
) {
this.charts = [
2022-09-17 20:27:08 +02:00
this.federationChart,
this.notesChart,
this.usersChart,
this.activeUsersChart,
this.instanceChart,
this.perUserNotesChart,
this.driveChart,
this.perUserReactionsChart,
this.hashtagChart,
this.perUserFollowingChart,
this.perUserDriveChart,
this.apRequestChart,
];
}
@bindThis
public async run() {
2022-09-17 20:27:08 +02:00
// 20分おきにメモリ情報をDBに書き込み
this.saveIntervalId = setInterval(() => {
for (const chart of this.charts) {
2022-09-17 20:27:08 +02:00
chart.save();
}
}, 1000 * 60 * 20);
}
async onApplicationShutdown(signal: string): Promise<void> {
clearInterval(this.saveIntervalId);
await Promise.all(
this.charts.map(chart => chart.save()),
);
2022-09-17 20:27:08 +02:00
}
}