Skip to content

Commit

Permalink
feat(teach): migrate more api to orm, fix #185
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 25, 2021
1 parent ff470d5 commit cca4ded
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 64 deletions.
4 changes: 2 additions & 2 deletions packages/plugin-status/server/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ export namespace Statistics {
})

// dialogue
if (ctx.database.getDialoguesById) {
if (ctx.database.getDialogueStats) {
const dialogueMap = average(daily.map(data => data.dialogue))
const dialogues = await ctx.database.getDialoguesById(Object.keys(dialogueMap).map(i => +i), ['id', 'original'])
const dialogues = await ctx.database.get('dialogue', Object.keys(dialogueMap).map(i => +i), ['id', 'original'])
const questionMap: Record<string, QuestionData> = {}
for (const dialogue of dialogues) {
const { id, original: name } = dialogue
Expand Down
24 changes: 0 additions & 24 deletions packages/plugin-teach/src/database/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ declare module 'koishi-core' {
}

Database.extend('koishi-plugin-mongo', {
async getDialoguesById(ids, fields) {
if (!ids.length) return []
let cursor = this.db.collection('dialogue').find({ _id: { $in: ids } })
if (fields) cursor = cursor.project(Object.fromEntries(fields.map(k => [k, 1])))
const dialogues = await cursor.toArray()
dialogues.forEach(d => {
d._id = d.id
defineProperty(d, '_backup', clone(d))
})
return dialogues
},

async getDialoguesByTest(test: DialogueTest) {
const query: FilterQuery<Dialogue> = { $and: [] }
this.app.emit('dialogue/mongo', test, query.$and)
Expand Down Expand Up @@ -55,18 +43,6 @@ Database.extend('koishi-plugin-mongo', {
await this.update('dialogue', data)
},

async recoverDialogues(dialogues: Dialogue[], argv: Dialogue.Argv) {
if (!dialogues.length) return
const tasks = []
for (const dialogue of dialogues) {
tasks.push(await this.db.collection('dialogue').updateOne({ _id: dialogue.id }, { $set: dialogue }))
}
await Promise.all(tasks)
for (const dialogue of dialogues) {
Dialogue.addHistory(dialogue, '修改', argv, true)
}
},

async getDialogueStats() {
const [data, dialogues] = await Promise.all([
this.db.collection('dialogue').aggregate([
Expand Down
15 changes: 0 additions & 15 deletions packages/plugin-teach/src/database/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ declare module 'koishi-core' {
}

Database.extend('koishi-plugin-mysql', {
async getDialoguesById(ids, fields) {
if (!ids.length) return []
const dialogues = await this.select<Dialogue>('dialogue', fields, `\`id\` IN (${ids.join(',')})`)
dialogues.forEach(d => defineProperty(d, '_backup', clone(d)))
return dialogues
},

async getDialoguesByTest(test: DialogueTest) {
let query = 'SELECT * FROM `dialogue`'
const conditionals: string[] = []
Expand Down Expand Up @@ -51,14 +44,6 @@ Database.extend('koishi-plugin-mysql', {
await this.update('dialogue', data)
},

async recoverDialogues(dialogues: Dialogue[], argv: Dialogue.Argv) {
if (!dialogues.length) return
await this.update('dialogue', dialogues)
for (const dialogue of dialogues) {
Dialogue.addHistory(dialogue, '修改', argv, true)
}
},

async getDialogueStats() {
const [{
'COUNT(DISTINCT `question`)': questions,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-teach/src/plugins/successor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
const { succOverwrite, successors, dialogues } = argv
if (!successors) return
const predecessors = dialogues.map(dialogue => '' + dialogue.id)
const successorDialogues = await ctx.database.getDialoguesById(successors)
const successorDialogues = await Dialogue.get(ctx, successors)
const newTargets = successorDialogues.map(d => d.id)
argv.unknown = difference(successors, newTargets)

Expand Down Expand Up @@ -156,7 +156,7 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
}
}
const dialogueMap: Record<string, Dialogue> = {}
for (const dialogue of await ctx.database.getDialoguesById([...predecessors])) {
for (const dialogue of await Dialogue.get(ctx, [...predecessors])) {
dialogueMap[dialogue.id] = dialogue
}
for (const dialogue of dialogues) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-teach/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export async function update(argv: Dialogue.Argv) {
argv.skipped = []
const dialogues = argv.dialogues = revert || review
? Object.values(pick(argv.app.teachHistory, target)).filter(Boolean)
: await app.database.getDialoguesById(target)
: await Dialogue.get(app, target, null)
argv.dialogueMap = Object.fromEntries(dialogues.map(d => [d.id, { ...d }]))

if (search) {
Expand Down
21 changes: 16 additions & 5 deletions packages/plugin-teach/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Session, App, Tables } from 'koishi-core'
import { difference, observe, isInteger, defineProperty, Observed } from 'koishi-utils'
import { Session, App, Context, Tables } from 'koishi-core'
import { difference, observe, isInteger, defineProperty, Observed, clone } from 'koishi-utils'
import { RegExpValidator } from 'regexpp'

declare module 'koishi-core' {
Expand All @@ -17,10 +17,8 @@ declare module 'koishi-core' {
}

interface Database {
getDialoguesById<T extends Dialogue.Field>(ids: number[], fields?: T[]): Promise<Dialogue[]>
getDialoguesByTest(test: DialogueTest): Promise<Dialogue[]>
updateDialogues(dialogues: Observed<Dialogue>[], argv: Dialogue.Argv): Promise<void>
recoverDialogues(dialogues: Dialogue[], argv: Dialogue.Argv): Promise<void>
getDialogueStats(): Promise<DialogueStats>
}
}
Expand Down Expand Up @@ -95,6 +93,12 @@ export namespace Dialogue {
complement = 16,
}

export async function get<K extends Dialogue.Field>(ctx: Context, ids: number[], fields?: K[]) {
const dialogues = await ctx.database.get('dialogue', ids, fields)
dialogues.forEach(d => defineProperty(d, '_backup', clone(d)))
return dialogues
}

export async function remove(dialogues: Dialogue[], argv: Dialogue.Argv, revert = false) {
const ids = dialogues.map(d => d.id)
argv.app.database.remove('dialogue', ids)
Expand All @@ -108,10 +112,17 @@ export namespace Dialogue {
const created = dialogues.filter(d => d._type === '添加')
const edited = dialogues.filter(d => d._type !== '添加')
await Dialogue.remove(created, argv, true)
await argv.app.database.recoverDialogues(edited, argv)
await recover(edited, argv)
return `问答 ${dialogues.map(d => d.id).sort((a, b) => a - b)} 已回退完成。`
}

export async function recover(dialogues: Dialogue[], argv: Dialogue.Argv) {
await argv.app.database.update('dialogue', dialogues)
for (const dialogue of dialogues) {
Dialogue.addHistory(dialogue, '修改', argv, true)
}
}

export function addHistory(dialogue: Dialogue, type: Dialogue.ModifyType, argv: Dialogue.Argv, revert: boolean) {
if (revert) return delete argv.app.teachHistory[dialogue.id]
argv.app.teachHistory[dialogue.id] = dialogue
Expand Down
15 changes: 0 additions & 15 deletions packages/plugin-teach/tests/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ declare module 'koishi-core' {
}

Database.extend('koishi-test-utils', {
async getDialoguesById(ids) {
if (!ids.length) return []
const table = this.$table('dialogue')
const dialogues = table.filter(row => ids.includes(row.id)).map<Dialogue>(clone)
dialogues.forEach(d => defineProperty(d, '_backup', clone(d)))
return dialogues
},

async getDialoguesByTest(test: DialogueTest) {
const dialogues = this.$table('dialogue').filter((dialogue) => {
return !this.app.bail('dialogue/memory', dialogue, test)
Expand All @@ -44,13 +36,6 @@ Database.extend('koishi-test-utils', {
await this.update('dialogue', data)
},

async recoverDialogues(dialogues: Dialogue[], argv: Dialogue.Argv) {
for (const dialogue of dialogues) {
this.update('dialogue', [dialogue])
Dialogue.addHistory(dialogue, '修改', argv, true)
}
},

async getDialogueStats() {
const dialogues = this.$count('dialogue')
const questions = this.$count('dialogue', 'question')
Expand Down

0 comments on commit cca4ded

Please sign in to comment.