Skip to content

Commit

Permalink
fix(status): fix mysql table creation, fix #166
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 20, 2021
1 parent 8c2ce5a commit b7ce209
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/plugin-status/server/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ ON DUPLICATE KEY UPDATE ${updates.join(', ')}`)

namespace Stat {
export class Recorded<K extends string> extends Stat<K, StatRecord> {
constructor(table: string, fields: readonly K[]) {
constructor(table: string, fields: readonly K[], timeDomain: string) {
super(table, fields)

Database.extend('koishi-plugin-mysql', ({ tables, Domain }) => {
tables[table] = Object.assign([
'primary key (`time`)',
], Object.fromEntries(fields.map(key => [key, new Domain.Json('text')])))
tables[table].time = timeDomain
})
}

Expand All @@ -83,13 +84,14 @@ namespace Stat {
}

export class Numerical<K extends string> extends Stat<K, number> {
constructor(table: string, fields: readonly K[]) {
constructor(table: string, fields: readonly K[], timeDomain: string) {
super(table, fields)

Database.extend('koishi-plugin-mysql', ({ tables }) => {
tables[table] = Object.assign([
'primary key (`time`)',
], Object.fromEntries(fields.map(key => [key, 'int unsigned'])))
tables[table].time = timeDomain
})
}

Expand Down Expand Up @@ -125,9 +127,9 @@ Database.extend('koishi-plugin-mysql', {
},

Synchronizer: class {
private _daily = new Stat.Recorded('stats_daily', Synchronizer.dailyFields)
private _hourly = new Stat.Numerical('stats_hourly', Synchronizer.hourlyFields)
private _longterm = new Stat.Numerical('stats_longterm', Synchronizer.longtermFields)
private _daily = new Stat.Recorded('stats_daily', Synchronizer.dailyFields, 'date')
private _hourly = new Stat.Numerical('stats_hourly', Synchronizer.hourlyFields, 'datetime')
private _longterm = new Stat.Numerical('stats_longterm', Synchronizer.longtermFields, 'date')

groups: StatRecord = {}
daily = this._daily.data
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-status/server/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Channel, noop, Session, Logger, Bot, Platform, Time } from 'koishi'
import { Context, Channel, noop, Session, Logger, Bot, Platform, Time } from 'koishi-core'
import {} from 'koishi-plugin-teach'

export type StatRecord = Record<string, number>
Expand Down

0 comments on commit b7ce209

Please sign in to comment.