Promise has sequenced import tasks in .gbkb.
This commit is contained in:
parent
88c190254d
commit
8ca77a4a63
1 changed files with 102 additions and 110 deletions
|
@ -359,100 +359,93 @@ export class KBService {
|
||||||
instanceId: number,
|
instanceId: number,
|
||||||
packageId: number
|
packageId: number
|
||||||
): Promise<GuaribasQuestion[]> {
|
): Promise<GuaribasQuestion[]> {
|
||||||
return new Promise<GuaribasQuestion[]>(
|
|
||||||
(resolve, reject) => {
|
|
||||||
|
|
||||||
let file = Fs.readFileSync(filePath, "UCS-2")
|
let file = Fs.readFileSync(filePath, "UCS-2")
|
||||||
let opts = {
|
let opts = {
|
||||||
delimiter: "\t"
|
delimiter: "\t"
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = await parse(file, opts);
|
||||||
|
return asyncPromise.eachSeries(data, async line => {
|
||||||
|
|
||||||
|
// Extracts values from columns in the current line.
|
||||||
|
|
||||||
|
let subjectsText = line[0]
|
||||||
|
var from = line[1]
|
||||||
|
var to = line[2]
|
||||||
|
var question = line[3]
|
||||||
|
var answer = line[4]
|
||||||
|
|
||||||
|
// Skips the first line.
|
||||||
|
|
||||||
|
if (!(subjectsText === "subjects" && from == "from")) {
|
||||||
|
let format = ".txt"
|
||||||
|
|
||||||
|
// Extracts answer from external media if any.
|
||||||
|
|
||||||
|
if (answer.indexOf(".md") > -1) {
|
||||||
|
let mediaFilename = UrlJoin(path.dirname(filePath), "..", "articles", answer)
|
||||||
|
if (Fs.existsSync(mediaFilename)) {
|
||||||
|
answer = Fs.readFileSync(mediaFilename, "utf8")
|
||||||
|
format = ".md"
|
||||||
|
} else {
|
||||||
|
logger.info(`[GBImporter] File not found: ${mediaFilename}.`)
|
||||||
|
answer =
|
||||||
|
"Por favor, contate a administração para rever esta pergunta."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parse(file, opts).then((data) => {
|
// Processes subjects hierarchy splitting by dots.
|
||||||
return asyncPromise.eachSeries(data, line => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
|
|
||||||
// Extracts values from columns in the current line.
|
let subjectArray = subjectsText.split(".")
|
||||||
|
let subject1: string, subject2: string, subject3: string,
|
||||||
|
subject4: string
|
||||||
|
var indexer = 0
|
||||||
|
|
||||||
let subjectsText = line[0]
|
subjectArray.forEach(element => {
|
||||||
var from = line[1]
|
if (indexer == 0) {
|
||||||
var to = line[2]
|
subject1 = subjectArray[indexer].substring(0, 63)
|
||||||
var question = line[3]
|
} else if (indexer == 1) {
|
||||||
var answer = line[4]
|
subject2 = subjectArray[indexer].substring(0, 63)
|
||||||
|
} else if (indexer == 2) {
|
||||||
|
subject3 = subjectArray[indexer].substring(0, 63)
|
||||||
|
} else if (indexer == 3) {
|
||||||
|
subject4 = subjectArray[indexer].substring(0, 63)
|
||||||
|
}
|
||||||
|
indexer++
|
||||||
|
})
|
||||||
|
|
||||||
// Skips the first line.
|
// Now with all the data ready, creates entities in the store.
|
||||||
|
|
||||||
if (!(subjectsText === "subjects" && from == "from")) {
|
let answer1 = await GuaribasAnswer.create({
|
||||||
let format = ".txt"
|
instanceId: instanceId,
|
||||||
|
content: answer,
|
||||||
|
format: format,
|
||||||
|
packageId: packageId
|
||||||
|
});
|
||||||
|
await GuaribasQuestion.create({
|
||||||
|
from: from,
|
||||||
|
to: to,
|
||||||
|
subject1: subject1,
|
||||||
|
subject2: subject2,
|
||||||
|
subject3: subject3,
|
||||||
|
subject4: subject4,
|
||||||
|
content: question,
|
||||||
|
instanceId: instanceId,
|
||||||
|
answerId: answer1.answerId,
|
||||||
|
packageId: packageId
|
||||||
|
});
|
||||||
|
logger.info(`Question created: ${question.questionId}`)
|
||||||
|
return Promise.resolve(question)
|
||||||
|
|
||||||
// Extracts answer from external media if any.
|
} else {
|
||||||
|
|
||||||
if (answer.indexOf(".md") > -1) {
|
// Skips the header.
|
||||||
let mediaFilename = UrlJoin(path.dirname(filePath), "..", "articles", answer)
|
|
||||||
if (Fs.existsSync(mediaFilename)) {
|
|
||||||
answer = Fs.readFileSync(mediaFilename, "utf8")
|
|
||||||
format = ".md"
|
|
||||||
} else {
|
|
||||||
logger.info(`[GBImporter] File not found: ${mediaFilename}.`)
|
|
||||||
answer =
|
|
||||||
"Por favor, contate a administração para rever esta pergunta."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Processes subjects hierarchy splitting by dots.
|
return Promise.resolve(null)
|
||||||
|
}
|
||||||
|
|
||||||
let subjectArray = subjectsText.split(".")
|
})
|
||||||
let subject1: string, subject2: string, subject3: string,
|
|
||||||
subject4: string
|
|
||||||
var indexer = 0
|
|
||||||
|
|
||||||
subjectArray.forEach(element => {
|
|
||||||
if (indexer == 0) {
|
|
||||||
subject1 = subjectArray[indexer].substring(0, 63)
|
|
||||||
} else if (indexer == 1) {
|
|
||||||
subject2 = subjectArray[indexer].substring(0, 63)
|
|
||||||
} else if (indexer == 2) {
|
|
||||||
subject3 = subjectArray[indexer].substring(0, 63)
|
|
||||||
} else if (indexer == 3) {
|
|
||||||
subject4 = subjectArray[indexer].substring(0, 63)
|
|
||||||
}
|
|
||||||
indexer++
|
|
||||||
})
|
|
||||||
|
|
||||||
// Now with all the data ready, creates entities in the store.
|
|
||||||
|
|
||||||
GuaribasAnswer.create({
|
|
||||||
instanceId: instanceId,
|
|
||||||
content: answer,
|
|
||||||
format: format,
|
|
||||||
packageId: packageId
|
|
||||||
}).then((answer: GuaribasAnswer) => {
|
|
||||||
return GuaribasQuestion.create({
|
|
||||||
from: from,
|
|
||||||
to: to,
|
|
||||||
subject1: subject1,
|
|
||||||
subject2: subject2,
|
|
||||||
subject3: subject3,
|
|
||||||
subject4: subject4,
|
|
||||||
content: question,
|
|
||||||
instanceId: instanceId,
|
|
||||||
answerId: answer.answerId,
|
|
||||||
packageId: packageId
|
|
||||||
}).then((question: GuaribasQuestion) => {
|
|
||||||
resolve(question)
|
|
||||||
}).error(reason => reject(reason))
|
|
||||||
}).error(reason => reject(reason))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
let msg = `[GBImporter] Missing header in file: ${filePath}`;
|
|
||||||
logger.info(msg)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}).error(reason => reject(reason))
|
|
||||||
|
|
||||||
resolve(null)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendAnswer(conversationalService: IGBConversationalService,
|
sendAnswer(conversationalService: IGBConversationalService,
|
||||||
|
@ -540,36 +533,35 @@ export class KBService {
|
||||||
packageId: number,
|
packageId: number,
|
||||||
filename: string,
|
filename: string,
|
||||||
instance: IGBInstance
|
instance: IGBInstance
|
||||||
): Promise<GuaribasQuestion[]> {
|
): Promise<any> {
|
||||||
return new Promise<GuaribasQuestion[]>(
|
var subjects = JSON.parse(Fs.readFileSync(filename, "utf8"))
|
||||||
(resolve, reject) => {
|
|
||||||
|
|
||||||
var subjects = JSON.parse(Fs.readFileSync(filename, "utf8"))
|
const doIt = async (subjects: GuaribasSubject[], parentSubjectId: number) => {
|
||||||
|
return asyncPromise.eachSeries(subjects, async item => {
|
||||||
|
let mediaFilename = item.id + ".png"
|
||||||
|
|
||||||
const doIt = (subjects: GuaribasSubject[], parentSubjectId: number) =>
|
let value = await GuaribasSubject.create({
|
||||||
new Promise((resolve, reject) => {
|
internalId: item.id,
|
||||||
asyncPromise.eachSeries(subjects, item => {
|
parentSubjectId: parentSubjectId,
|
||||||
let mediaFilename = item.id + ".png"
|
instanceId: instance.instanceId,
|
||||||
GuaribasSubject.create({
|
from: item.from,
|
||||||
internalId: item.id,
|
to: item.to,
|
||||||
parentSubjectId: parentSubjectId,
|
title: item.title,
|
||||||
instanceId: instance.instanceId,
|
description: item.description,
|
||||||
from: item.from,
|
packageId: packageId
|
||||||
to: item.to,
|
});
|
||||||
title: item.title,
|
|
||||||
description: item.description,
|
if (item.children) {
|
||||||
packageId: packageId
|
return Promise.resolve(doIt(item.children, value.subjectId))
|
||||||
}).then((value: any) => {
|
}
|
||||||
if (item.children) {
|
else {
|
||||||
doIt(item.children, value.subjectId)
|
return Promise.resolve(item);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
doIt(subjects.children, null)
|
|
||||||
resolve()
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return doIt(subjects.children, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
undeployKbFromStorage(
|
undeployKbFromStorage(
|
||||||
|
@ -607,7 +599,7 @@ export class KBService {
|
||||||
async deployKb(core: IGBCoreService, deployer: GBDeployer, localPath: string) {
|
async deployKb(core: IGBCoreService, deployer: GBDeployer, localPath: string) {
|
||||||
let packageType = Path.extname(localPath)
|
let packageType = Path.extname(localPath)
|
||||||
let packageName = Path.basename(localPath)
|
let packageName = Path.basename(localPath)
|
||||||
logger.info("[GBDeployer] Opening package: ", packageName)
|
logger.info("[GBDeployer] Opening package: ", localPath)
|
||||||
let packageObject = JSON.parse(
|
let packageObject = JSON.parse(
|
||||||
Fs.readFileSync(UrlJoin(localPath, "package.json"), "utf8")
|
Fs.readFileSync(UrlJoin(localPath, "package.json"), "utf8")
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue