Skip to content

Commit

Permalink
#612 refresh complete state
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Buret <jburet@voyages-sncf.com>
  • Loading branch information
vsct-jburet committed Jan 2, 2020
1 parent c09475c commit b31d188
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 52 deletions.
102 changes: 56 additions & 46 deletions bot/admin/server/src/main/kotlin/BotAdminService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import ai.tock.bot.admin.story.dump.StoryDefinitionConfigurationDump
import ai.tock.bot.admin.story.dump.StoryDefinitionConfigurationDumpController
import ai.tock.bot.admin.story.dump.StoryDefinitionConfigurationFeatureDump
import ai.tock.bot.admin.user.UserReportDAO
import ai.tock.bot.definition.Intent
import ai.tock.bot.engine.dialog.DialogFlowDAO
import ai.tock.bot.engine.feature.FeatureDAO
import ai.tock.bot.engine.feature.FeatureState
Expand All @@ -74,6 +75,7 @@ import ai.tock.nlp.front.shared.config.EntityTypeDefinition
import ai.tock.nlp.front.shared.config.IntentDefinition
import ai.tock.nlp.front.shared.config.SentencesQuery
import ai.tock.shared.Dice
import ai.tock.shared.defaultLocale
import ai.tock.shared.injector
import ai.tock.shared.provide
import ai.tock.shared.vertx.UnauthorizedException
Expand Down Expand Up @@ -106,7 +108,9 @@ object BotAdminService {

private class BotStoryDefinitionConfigurationDumpController(
override val targetNamespace: String,
override val botId: String)
override val botId: String,
val story: StoryDefinitionConfigurationDump,
val application: ApplicationDefinition)
: StoryDefinitionConfigurationDumpController {

override fun keepFeature(feature: StoryDefinitionConfigurationFeatureDump): Boolean =
Expand Down Expand Up @@ -138,6 +142,22 @@ object BotAdminService {
)
}
}

override fun checkIntent(intent: Intent?): Intent? {
if (intent != null) {
AdminService.createOrGetIntent(
targetNamespace,
IntentDefinition(
intent.name,
targetNamespace,
setOf(application._id),
emptySet(),
category = story.category
)
)
}
return intent
}
}

fun getBots(namespace: String, botId: String): List<BotConfiguration> {
Expand Down Expand Up @@ -277,59 +297,48 @@ object BotAdminService {
}

fun importStories(namespace: String, botId: String, stories: List<StoryDefinitionConfigurationDump>) {
stories.map {
it.toStoryDefinitionConfiguration(
BotStoryDefinitionConfigurationDumpController(namespace, botId)
)
}
.forEach {
saveStory(namespace, it)
val botConf = getBotConfigurationsByNamespaceAndBotId(namespace, botId).firstOrNull()

if (botConf == null) {
badRequest("No bot configuration is defined yet")
} else {
val application = front.getApplicationByNamespaceAndName(namespace, botConf.nlpModel)!!
stories.map {
it.toStoryDefinitionConfiguration(
BotStoryDefinitionConfigurationDumpController(namespace, botId, it, application)
)
}
.forEach {
saveStory(namespace, it, botConf, application)
}
}
}

private fun saveStory(
namespace: String,
story: StoryDefinitionConfiguration
story: StoryDefinitionConfiguration,
botConf: BotApplicationConfiguration,
application: ApplicationDefinition
) {
storyDefinitionDAO.getStoryDefinitionByNamespaceAndBotIdAndIntent(
namespace,
botConf.botId,
story.intent.name
)?.also {
storyDefinitionDAO.delete(it)
}
storyDefinitionDAO.getStoryDefinitionByNamespaceAndBotIdAndStoryId(
namespace,
botConf.botId,
story.storyId
)?.also {
storyDefinitionDAO.delete(it)
}

val botConf = getBotConfigurationsByNamespaceAndBotId(namespace, story.botId).firstOrNull()
return if (botConf != null) {

val application = front.getApplicationByNamespaceAndName(namespace, botConf.nlpModel)!!

AdminService.createOrGetIntent(
namespace,
IntentDefinition(
story.intent.name,
namespace,
setOf(application._id),
emptySet(),
category = story.category
)
)

storyDefinitionDAO.getStoryDefinitionByNamespaceAndBotIdAndIntent(
namespace,
botConf.botId,
story.intent.name
)?.also {
storyDefinitionDAO.delete(it)
}
storyDefinitionDAO.getStoryDefinitionByNamespaceAndBotIdAndStoryId(
namespace,
botConf.botId,
story.storyId
)?.also {
storyDefinitionDAO.delete(it)
}

storyDefinitionDAO.save(story)
storyDefinitionDAO.save(story)

//save all intents of steps
story.steps.forEach { saveUserSentenceOfStep(application, it) }
} else {
badRequest("No bot configuration is defined yet")
}
//save all intents of steps
story.steps.forEach { saveUserSentenceOfStep(application, it) }
}

fun findStoryByBotIdAndIntent(namespace: String, botId: String, intent: String): BotStoryDefinitionConfiguration? {
Expand Down Expand Up @@ -670,6 +679,7 @@ object BotAdminService {
if (label != null && step.intent != null) {
application.supportedLocales.forEach { locale ->
val text = label.findLabel(locale)?.label
?: label.findLabel(defaultLocale)?.label
if (text != null) {
val existingSentence = front.search(
SentencesQuery(
Expand Down
2 changes: 1 addition & 1 deletion bot/admin/web/src/app/bot/story/search-story.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class SearchStoryComponent implements OnInit, OnDestroy {
this.uploader.onCompleteItem =
(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders) => {
this.dialog.notify(`Dump uploaded`, "Dump");
this.load();
this.state.resetConfiguration();
};
this.displayUpload = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ data class StoryDefinitionConfigurationDump(
StoryDefinitionConfiguration(
storyId,
controller.botId,
intent,
controller.checkIntent(intent)!!,
currentType,
answers.map { it.toAnswer(currentType, controller) },
version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package ai.tock.bot.admin.story.dump

import ai.tock.bot.admin.answer.ScriptAnswerVersionedConfiguration
import ai.tock.bot.definition.Intent

interface StoryDefinitionConfigurationDumpController {
val targetNamespace: String
val botId:String
fun keepFeature(feature: StoryDefinitionConfigurationFeatureDump): Boolean
fun buildScript(script: ScriptAnswerVersionedConfigurationDump, compile: Boolean): ScriptAnswerVersionedConfiguration
fun checkIntent(intent: Intent?): Intent?
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ data class StoryDefinitionConfigurationMandatoryEntityDump(
StoryDefinitionConfigurationMandatoryEntity(
role,
entityType,
intent,
controller.checkIntent(intent)!!,
answers.map { it.toAnswer(currentType, controller) },
currentType
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ data class StoryDefinitionConfigurationStepDump(

fun toStep(controller: StoryDefinitionConfigurationDumpController): StoryDefinitionConfigurationStep =
StoryDefinitionConfigurationStep(
name,
intent,
targetIntent,
name.takeUnless { it.isBlank() } ?: "${intent?.name}_$level",
controller.checkIntent(intent),
controller.checkIntent(targetIntent),
answers.map { it.toAnswer(currentType, controller) },
currentType,
"",
Expand Down

0 comments on commit b31d188

Please sign in to comment.