From 1382b38238305da35423e8f774dcfeab314ef549 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sat, 22 Jan 2022 12:04:20 +0800 Subject: [PATCH] refa(status): support incremental reload --- .../{status => logger}/client/index.scss | 0 plugins/frontend/logger/client/index.ts | 1 + .../client/{home => }/charts/command.ts | 0 .../status/client/{home => }/charts/guild.ts | 0 .../client/{home => }/charts/history.ts | 0 .../status/client/{home => }/charts/hour.ts | 0 .../status/client/{home => }/charts/index.ts | 0 .../status/client/{home => }/charts/utils.ts | 0 .../client/{home => components}/load-bar.vue | 0 .../{home => components}/load-chart.vue | 0 plugins/frontend/status/client/home/index.ts | 68 ---------------- plugins/frontend/status/client/index.ts | 78 +++++++++++++++---- plugins/frontend/status/src/index.ts | 21 +++-- plugins/frontend/status/src/meta.ts | 23 +++--- plugins/frontend/status/src/profile.ts | 6 +- plugins/frontend/status/src/stats.ts | 6 +- 16 files changed, 95 insertions(+), 108 deletions(-) rename plugins/frontend/{status => logger}/client/index.scss (100%) rename plugins/frontend/status/client/{home => }/charts/command.ts (100%) rename plugins/frontend/status/client/{home => }/charts/guild.ts (100%) rename plugins/frontend/status/client/{home => }/charts/history.ts (100%) rename plugins/frontend/status/client/{home => }/charts/hour.ts (100%) rename plugins/frontend/status/client/{home => }/charts/index.ts (100%) rename plugins/frontend/status/client/{home => }/charts/utils.ts (100%) rename plugins/frontend/status/client/{home => components}/load-bar.vue (100%) rename plugins/frontend/status/client/{home => components}/load-chart.vue (100%) delete mode 100644 plugins/frontend/status/client/home/index.ts diff --git a/plugins/frontend/status/client/index.scss b/plugins/frontend/logger/client/index.scss similarity index 100% rename from plugins/frontend/status/client/index.scss rename to plugins/frontend/logger/client/index.scss diff --git a/plugins/frontend/logger/client/index.ts b/plugins/frontend/logger/client/index.ts index f8a4d9a3ae..c17efa5e41 100644 --- a/plugins/frontend/logger/client/index.ts +++ b/plugins/frontend/logger/client/index.ts @@ -1,6 +1,7 @@ import { registerPage } from '~/client' import type {} from '@koishijs/plugin-logger/src' import Logs from './logs.vue' +import './index.scss' registerPage({ path: '/logs', diff --git a/plugins/frontend/status/client/home/charts/command.ts b/plugins/frontend/status/client/charts/command.ts similarity index 100% rename from plugins/frontend/status/client/home/charts/command.ts rename to plugins/frontend/status/client/charts/command.ts diff --git a/plugins/frontend/status/client/home/charts/guild.ts b/plugins/frontend/status/client/charts/guild.ts similarity index 100% rename from plugins/frontend/status/client/home/charts/guild.ts rename to plugins/frontend/status/client/charts/guild.ts diff --git a/plugins/frontend/status/client/home/charts/history.ts b/plugins/frontend/status/client/charts/history.ts similarity index 100% rename from plugins/frontend/status/client/home/charts/history.ts rename to plugins/frontend/status/client/charts/history.ts diff --git a/plugins/frontend/status/client/home/charts/hour.ts b/plugins/frontend/status/client/charts/hour.ts similarity index 100% rename from plugins/frontend/status/client/home/charts/hour.ts rename to plugins/frontend/status/client/charts/hour.ts diff --git a/plugins/frontend/status/client/home/charts/index.ts b/plugins/frontend/status/client/charts/index.ts similarity index 100% rename from plugins/frontend/status/client/home/charts/index.ts rename to plugins/frontend/status/client/charts/index.ts diff --git a/plugins/frontend/status/client/home/charts/utils.ts b/plugins/frontend/status/client/charts/utils.ts similarity index 100% rename from plugins/frontend/status/client/home/charts/utils.ts rename to plugins/frontend/status/client/charts/utils.ts diff --git a/plugins/frontend/status/client/home/load-bar.vue b/plugins/frontend/status/client/components/load-bar.vue similarity index 100% rename from plugins/frontend/status/client/home/load-bar.vue rename to plugins/frontend/status/client/components/load-bar.vue diff --git a/plugins/frontend/status/client/home/load-chart.vue b/plugins/frontend/status/client/components/load-chart.vue similarity index 100% rename from plugins/frontend/status/client/home/load-chart.vue rename to plugins/frontend/status/client/components/load-chart.vue diff --git a/plugins/frontend/status/client/home/index.ts b/plugins/frontend/status/client/home/index.ts deleted file mode 100644 index 049b9b764f..0000000000 --- a/plugins/frontend/status/client/home/index.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { Card, registerView } from '~/client' -import LoadChart from './load-chart.vue' - -import './charts' - -registerView({ - type: 'numeric', - component: Card.numeric({ - title: '近期消息频率', - icon: 'history', - fields: ['stats'], - content({ stats }) { - return Object.values(stats.botSend).reduce((sum, value) => sum + value, 0).toFixed(1) + ' / d' - }, - }), -}) - -registerView({ - id: 'database', - type: 'numeric', - component: Card.numeric({ - title: '数据库体积', - icon: 'database', - type: 'size', - fields: ['meta'], - content({ meta }) { - // @ts-ignore - return Object.values(meta.tables || {}).reduce((prev, curr) => prev + curr.size, 0) - }, - }), -}) - -registerView({ - id: 'assets', - type: 'numeric', - component: Card.numeric({ - title: '资源服务器', - icon: 'hdd', - type: 'size', - fields: ['meta'], - content: ({ meta }) => meta.assetSize, - }), -}) - -registerView({ - type: 'numeric', - component: Card.numeric({ - title: '活跃用户数量', - icon: 'heart', - fields: ['meta'], - content: ({ meta }) => meta.activeUsers, - }), -}) - -registerView({ - type: 'numeric', - component: Card.numeric({ - title: '活跃群数量', - icon: 'users', - fields: ['meta'], - content: ({ meta }) => meta.activeGuilds, - }), -}) - -registerView({ - type: 'home', - component: LoadChart, -}) diff --git a/plugins/frontend/status/client/index.ts b/plugins/frontend/status/client/index.ts index ab47952377..3f0d6c8ea5 100644 --- a/plugins/frontend/status/client/index.ts +++ b/plugins/frontend/status/client/index.ts @@ -1,14 +1,66 @@ -import { registerPage } from '~/client' -import Database from './database/index.vue' - -import './home' -import './index.scss' - -registerPage({ - path: '/database', - name: '数据库', - icon: 'database', - order: 410, - fields: ['meta'], - component: Database, +import { Card, registerView } from '~/client' +import {} from '@koishijs/plugin-status/src' +import LoadChart from './components/load-chart.vue' + +import './charts' + +registerView({ + type: 'numeric', + component: Card.numeric({ + title: '近期消息频率', + icon: 'history', + fields: ['stats'], + content({ stats }) { + return Object.values(stats.botSend).reduce((sum, value) => sum + value, 0).toFixed(1) + ' / d' + }, + }), +}) + +registerView({ + id: 'database', + type: 'numeric', + component: Card.numeric({ + title: '数据库体积', + icon: 'database', + type: 'size', + fields: ['meta'], + content: ({ meta }) => meta.databaseSize, + }), +}) + +registerView({ + id: 'assets', + type: 'numeric', + component: Card.numeric({ + title: '资源服务器', + icon: 'hdd', + type: 'size', + fields: ['meta'], + content: ({ meta }) => meta.assetSize, + }), +}) + +registerView({ + type: 'numeric', + component: Card.numeric({ + title: '活跃用户数量', + icon: 'heart', + fields: ['meta'], + content: ({ meta }) => meta.activeUsers, + }), +}) + +registerView({ + type: 'numeric', + component: Card.numeric({ + title: '活跃群数量', + icon: 'users', + fields: ['meta'], + content: ({ meta }) => meta.activeGuilds, + }), +}) + +registerView({ + type: 'home', + component: LoadChart, }) diff --git a/plugins/frontend/status/src/index.ts b/plugins/frontend/status/src/index.ts index c3bd480fdd..e1728a687a 100644 --- a/plugins/frontend/status/src/index.ts +++ b/plugins/frontend/status/src/index.ts @@ -1,25 +1,17 @@ import { Context, Schema } from 'koishi' import { resolve } from 'path' import {} from '@koishijs/plugin-console' -import { MetaProvider } from './meta' -import { ProfileProvider } from './profile' -import { StatisticsProvider } from './stats' +import MetaProvider from './meta' +import ProfileProvider from './profile' +import StatisticsProvider from './stats' export type Activity = Record declare module 'koishi' { - interface Database { - stats(): Promise - } - interface Channel { name: string activity: Activity } - - interface Modules { - manager: typeof import('.') - } } declare module '@koishijs/plugin-console' { @@ -30,6 +22,12 @@ declare module '@koishijs/plugin-console' { } } +export { + MetaProvider, + ProfileProvider, + StatisticsProvider, +} + export * from './meta' export * from './profile' export * from './stats' @@ -48,6 +46,7 @@ export const Config = Schema.intersect([ export function apply(ctx: Context, config: Config) { const filename = ctx.console.config.devMode ? '../client/index.ts' : '../dist/index.js' ctx.console.addEntry(resolve(__dirname, filename)) + ctx.plugin(MetaProvider, config) ctx.plugin(ProfileProvider, config) ctx.plugin(StatisticsProvider, config) diff --git a/plugins/frontend/status/src/meta.ts b/plugins/frontend/status/src/meta.ts index d826f135bd..69750b528a 100644 --- a/plugins/frontend/status/src/meta.ts +++ b/plugins/frontend/status/src/meta.ts @@ -1,4 +1,4 @@ -import { Argv, Assets, Context, Dict, noop, Schema, Time } from 'koishi' +import { Argv, Assets, Context, noop, Schema, Time } from 'koishi' import { DataSource } from '@koishijs/plugin-console' declare module 'koishi' { @@ -7,7 +7,7 @@ declare module 'koishi' { } } -export class MetaProvider extends DataSource { +class MetaProvider extends DataSource { timestamp = 0 cached: Promise callbacks: MetaProvider.Extension[] = [] @@ -16,7 +16,10 @@ export class MetaProvider extends DataSource { super(ctx, 'meta') this.extend(async () => ctx.assets?.stats()) - this.extend(async () => ctx.database?.stats()) + this.extend(async () => { + const stats = await ctx.database?.stats() + return { databaseSize: stats.size } + }) this.extend(async () => { const activeUsers = await ctx.database?.eval('user', { $count: 'id' }, { @@ -57,7 +60,7 @@ export class MetaProvider extends DataSource { } } -export namespace MetaProvider { +namespace MetaProvider { export interface Config { metaInterval?: number } @@ -66,17 +69,13 @@ export namespace MetaProvider { metaInternal: Schema.number().description('元数据推送的时间间隔。').default(Time.hour), }) - export interface Stats { - size: number + export interface Payload extends Assets.Stats { activeUsers: number activeGuilds: number - tables: Dict<{ - count: number - size: number - }> + databaseSize: number } - export interface Payload extends Stats, Assets.Stats {} - export type Extension = () => Promise> } + +export default MetaProvider diff --git a/plugins/frontend/status/src/profile.ts b/plugins/frontend/status/src/profile.ts index f416d842b7..9ec9ecdf9e 100644 --- a/plugins/frontend/status/src/profile.ts +++ b/plugins/frontend/status/src/profile.ts @@ -42,7 +42,7 @@ function updateCpuUsage() { usage = newUsage } -export class ProfileProvider extends DataSource { +class ProfileProvider extends DataSource { cached: ProfileProvider.Payload constructor(ctx: Context, private config: ProfileProvider.Config) { @@ -65,7 +65,7 @@ export class ProfileProvider extends DataSource { } } -export namespace ProfileProvider { +namespace ProfileProvider { export interface Config { tickInterval?: number } @@ -79,3 +79,5 @@ export namespace ProfileProvider { cpu: LoadRate } } + +export default ProfileProvider diff --git a/plugins/frontend/status/src/stats.ts b/plugins/frontend/status/src/stats.ts index f6ff763d17..4066553365 100644 --- a/plugins/frontend/status/src/stats.ts +++ b/plugins/frontend/status/src/stats.ts @@ -52,7 +52,7 @@ Session.prototype.send = function (this: Session, ...args) { const customTag = Symbol('custom-send') Session.prototype.send[customTag] = send -export class StatisticsProvider extends DataSource { +class StatisticsProvider extends DataSource { static using = ['database'] as const lastUpdate = new Date() @@ -305,7 +305,7 @@ export class StatisticsProvider extends DataSource { } } -export namespace StatisticsProvider { +namespace StatisticsProvider { export type DailyField = typeof dailyFields[number] export const dailyFields = [ 'command', 'dialogue', 'botSend', 'botReceive', 'group', @@ -348,3 +348,5 @@ export namespace StatisticsProvider { export type Extension = (payload: Payload, data: StatisticsProvider.Data) => Promise } + +export default StatisticsProvider