Skip to content

Commit

Permalink
feat(status): unified profile stats
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 26, 2021
1 parent b009502 commit f8f8d6e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
8 changes: 7 additions & 1 deletion packages/plugin-assets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ class SmmsAssets implements Assets {
}

async stats() {
const { data } = await axios.post(this.config.endpoint + '/profile', null, this.config.axiosConfig)
const { token, endpoint, axiosConfig } = this.config
const { data } = await axios.post(endpoint + '/profile', null, {
...axiosConfig,
headers: {
authorization: token,
},
})
return {
assetSize: data.data.disk_usage_raw,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-status/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare module 'koishi-core' {
}

interface Database {
getProfile(): Promise<Profile.Meta>
getProfile(): Promise<Profile.Stats>
Synchronizer: new (db: Database) => Synchronizer
}

Expand Down
21 changes: 14 additions & 7 deletions packages/plugin-status/server/profile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bot, Context, Platform } from 'koishi-core'
import { Assets, Bot, Context, Platform } from 'koishi-core'
import { cpus } from 'os'
import { mem } from 'systeminformation'
import { Dialogue } from 'koishi-plugin-teach'

export type LoadRate = [app: number, total: number]
export type MessageRate = [send: number, receive: number]
Expand Down Expand Up @@ -77,14 +78,16 @@ export namespace Profile {
refreshInterval?: number
}

export interface Meta {
export interface Stats {
allUsers: number
activeUsers: number
allGroups: number
activeGroups: number
storageSize: number
}

export interface Meta extends Stats, Dialogue.Stats, Assets.Stats {}

export async function get(ctx: Context, config: Config) {
const [memory, bots] = await Promise.all([
memoryRate(),
Expand All @@ -95,13 +98,17 @@ export namespace Profile {
}

let timestamp = 0
let cachedMeta: Promise<Meta>
let cachedMeta: Promise<Partial<Meta>>

async function getMeta(ctx: Context, config: Config) {
const next = Date.now() + config.refreshInterval
if (timestamp > next) return cachedMeta
timestamp = next
return cachedMeta = ctx.database.getProfile()
const now = Date.now()
if (timestamp > now) return cachedMeta
timestamp = now + config.refreshInterval
return cachedMeta = Promise.all([
ctx.assets?.stats(),
ctx.database.getProfile(),
ctx.database?.getDialogueStats(),
]).then(data => Object.assign({}, ...data))
}

export function initBot(bot: Bot) {
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-teach/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ declare module 'koishi-core' {
interface Database {
getDialoguesByTest(test: DialogueTest): Promise<Dialogue[]>
updateDialogues(dialogues: Observed<Dialogue>[], argv: Dialogue.Argv): Promise<void>
getDialogueStats(): Promise<DialogueStats>
getDialogueStats(): Promise<Dialogue.Stats>
}
}

Tables.extend('dialogue')

interface DialogueStats {
questions: number
dialogues: number
}

export interface Dialogue {
id?: number
question: string
Expand Down Expand Up @@ -80,6 +75,11 @@ export namespace Dialogue {
validateRegExp?: RegExpValidator.Options
}

export interface Stats {
questions: number
dialogues: number
}

export enum Flag {
/** 冻结:只有 4 级以上权限者可修改 */
frozen = 1,
Expand Down

0 comments on commit f8f8d6e

Please sign in to comment.