Skip to content

Commit

Permalink
chore(qna): sync NLU less often and in background
Browse files Browse the repository at this point in the history
  • Loading branch information
epaminond committed Jul 26, 2018
1 parent ce88c93 commit 19360cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 5 additions & 2 deletions packages/functionals/botpress-qna/src/index.js
Expand Up @@ -54,7 +54,9 @@ module.exports = {
*/
import(questions, { format = 'json' } = {}) {
const questionsToSave = typeof questions === 'string' ? parsers[`${format}Parse`](questions) : questions
return Promise.each(questionsToSave, question => storage.saveQuestion({ ...question, enabled: true }))
return Promise.each(questionsToSave, question =>
storage.saveQuestion({ ...question, enabled: true }, null, false)
)
},

/**
Expand Down Expand Up @@ -162,11 +164,12 @@ module.exports = {
router.post('/csv', upload.single('csv'), async (req, res) => {
if (yn(req.body.isReplace)) {
const questions = await storage.getQuestions()
await Promise.each(questions, ({ id }) => storage.deleteQuestion(id))
await Promise.each(questions, ({ id }) => storage.deleteQuestion(id, false))
}

try {
await bp.qna.import(req.file.buffer.toString(), { format: 'csv' })
bp.nlu.provider.sync()
res.end()
} catch (e) {
logger.error('QnA Error:', e)
Expand Down
12 changes: 8 additions & 4 deletions packages/functionals/botpress-qna/src/storage.js
Expand Up @@ -47,7 +47,7 @@ export default class Storage {
}
}

async saveQuestion(data, id = null) {
async saveQuestion(data, id = null, syncNlu = true) {
id = id || getQuestionId(data)
if (data.enabled) {
await this.bp.nlu.storage.saveIntent(getIntentId(id), {
Expand All @@ -57,7 +57,9 @@ export default class Storage {
} else {
await this.bp.nlu.storage.deleteIntent(getIntentId(id))
}
await this.syncNlu()
if (syncNlu) {
this.syncNlu()
}
await this.ghost.upsertFile(this.qnaDir, `${id}.json`, JSON.stringify({ id, data }, null, 2))
return id
}
Expand All @@ -79,11 +81,13 @@ export default class Storage {
return Promise.map(questions, question => this.getQuestion({ filename: question }))
}

async deleteQuestion(id) {
async deleteQuestion(id, syncNlu = true) {
const data = await this.getQuestion(id)
if (data.data.enabled) {
await this.bp.nlu.storage.deleteIntent(getIntentId(id))
await this.syncNlu()
if (syncNlu) {
this.syncNlu()
}
}
await this.ghost.deleteFile(this.qnaDir, `${id}.json`)
}
Expand Down

0 comments on commit 19360cd

Please sign in to comment.