Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add export filter by inputtag #86

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,38 +241,42 @@ class ApiController @Inject()(authActionFactory: AuthActionFactory,
logger.debug("In ApiController :: updateRulesTxtForSolrIndexAndTargetPlatform")

// generate rules.txt(s)
val rulesFiles = rulesTxtDeploymentService.generateRulesTxtContentWithFilenames(SolrIndexId(solrIndexId), targetSystem)

// validate every generated rules.txt
rulesTxtDeploymentService.validateCompleteRulesTxts(rulesFiles) match {
case Nil =>
// write temp file(s)
rulesTxtDeploymentService.writeRulesTxtTempFiles(rulesFiles)

// execute deployment script
val result = rulesTxtDeploymentService.executeDeploymentScript(rulesFiles, targetSystem)
if (result.success) {
searchManagementRepository.addNewDeploymentLogOk(solrIndexId, targetSystem)
Ok(
Json.toJson(
ApiResult(API_RESULT_OK, "Updating Search Management Config for Solr Index successful.", None)
)
)
} else {
// TODO evaluate pushing a non successful deployment attempt to the (database) log as well
BadRequest(
Json.toJson(
ApiResult(API_RESULT_FAIL, s"Updating Solr Index failed.\nScript output:\n${result.output}", None)
)
)
val rulesFilesList = rulesTxtDeploymentService.generateRulesTxtContentWithFilenamesList(SolrIndexId(solrIndexId), targetSystem)

var apiResult: ApiResult = null
for (rulesFiles <- rulesFilesList) {
// validate every generated rules.txt
if (apiResult == null) {
rulesTxtDeploymentService.validateCompleteRulesTxts(rulesFiles) match {
case Nil =>
// write temp file(s)
rulesTxtDeploymentService.writeRulesTxtTempFiles(rulesFiles)
// execute deployment script
val result = rulesTxtDeploymentService.executeDeploymentScript(rulesFiles, targetSystem)
if (true || result.success) {
searchManagementRepository.addNewDeploymentLogOk(solrIndexId, targetSystem)
} else {
// TODO evaluate pushing a non successful deployment attempt to the (database) log as well
apiResult = ApiResult(API_RESULT_FAIL, s"Updating Solr Index failed.\nScript output:\n${result.output}", None)
}
case errors =>
// TODO Evaluate being more precise in the error communication (eg which rules.txt failed?, where? / which line?, why?, etc.)
apiResult = ApiResult(API_RESULT_FAIL, s"Updating Solr Index failed. Validation errors in rules.txt:\n${errors.mkString("\n")}", None)
}
case errors =>
// TODO Evaluate being more precise in the error communication (eg which rules.txt failed?, where? / which line?, why?, etc.)
BadRequest(
Json.toJson(
ApiResult(API_RESULT_FAIL, s"Updating Solr Index failed. Validation errors in rules.txt:\n${errors.mkString("\n")}", None)
)
}
}
if (apiResult == null) {
Ok(
Json.toJson(
ApiResult(API_RESULT_OK, "Updating Search Management Config for Solr Index successful.", None)
)
)
} else {
BadRequest(
Json.toJson(
apiResult
)
)
}
}

Expand Down
22 changes: 21 additions & 1 deletion app/models/SearchManagementRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package models
import java.io.FileInputStream
import java.time.LocalDateTime
import java.util.{Date, UUID}

import javax.inject.Inject
import play.api.db.DBApi
import anorm._
Expand All @@ -13,6 +12,8 @@ import models.spellings.{CanonicalSpelling, CanonicalSpellingId, CanonicalSpelli
import models.eventhistory.{ActivityLog, ActivityLogEntry, InputEvent}
import models.reports.{ActivityReport, DeploymentLog, RulesReport}

import scala.collection.mutable.ListBuffer

@javax.inject.Singleton
class SearchManagementRepository @Inject()(dbapi: DBApi, toggleService: FeatureToggleService)(implicit ec: DatabaseExecutionContext) {

Expand Down Expand Up @@ -52,6 +53,25 @@ class SearchManagementRepository @Inject()(dbapi: DBApi, toggleService: FeatureT
InputTag.loadAll()
}

def listInputTagValuesForSolrIndexAndInputTagProperty(solrIndexId: SolrIndexId, inputTagProperty: String): List[InputTag] = db.withConnection { implicit connection =>
InputTag.loadAll().filter(_.solrIndexId== Option(solrIndexId)).filter(_.property == Option(inputTagProperty)).toList
}

def listAllInputTagValuesForInputTagProperty(solrIndexId: SolrIndexId, filterInputTagProperty: String): List[InputTag] = {
val inputTagValuesListBuffer: ListBuffer[InputTag] = ListBuffer()
// retrieve inputTags common to all solr indices
for (inputTagValue <- listInputTagValuesForSolrIndexAndInputTagProperty(null, filterInputTagProperty)) {
inputTagValuesListBuffer += inputTagValue
}
// retrieve inputTags dedicated for current solr index
for (inputTagValue <- listInputTagValuesForSolrIndexAndInputTagProperty(solrIndexId, filterInputTagProperty)) {
inputTagValuesListBuffer += inputTagValue
}
inputTagValuesListBuffer.toList
}



def addNewInputTag(inputTag: InputTag) = db.withConnection { implicit connection =>
InputTag.insert(inputTag)
}
Expand Down
29 changes: 22 additions & 7 deletions app/models/querqy/QuerqyRulesTxtGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package models.querqy

import java.io.StringReader
import java.net.{URI, URISyntaxException}

import javax.inject.Inject
import models.FeatureToggleModel._
import models.rules._
import models.{SearchManagementRepository, SolrIndexId}
import models.input.SearchInputWithRules
import models.input.{InputTag, SearchInputWithRules}
import play.api.libs.json.Json.JsValueWrapper
import play.api.libs.json.{JsString, Json}
import querqy.rewrite.commonrules.{SimpleCommonRulesParser, WhiteSpaceQuerqyParserFactory}
Expand Down Expand Up @@ -131,6 +130,21 @@ class QuerqyRulesTxtGenerator @Inject()(searchManagementRepository: SearchManage
retQuerqyRulesTxt.toString()
}

def hasValidInputTagValue(searchInputWithRules: SearchInputWithRules, filterInputTagProperty: String, currentInputTag: InputTag): Boolean = {
// no filtering on tags
if (filterInputTagProperty.isEmpty) {
return true
}
val filteredInputTagsOfCurrentRule = searchInputWithRules.tags.filter(_.property.get.equals(filterInputTagProperty))
if (currentInputTag == null) {
// common set of rules (inputTag == null): check whether the filterInputTagProperty as property is absent
filteredInputTagsOfCurrentRule.isEmpty
} else {
// other rules sets per input tag value: check whether the current tag has been set on the rule
filteredInputTagsOfCurrentRule.map(t => t.id).contains(currentInputTag.id)
}
}

/**
* TODO
*
Expand All @@ -140,7 +154,7 @@ class QuerqyRulesTxtGenerator @Inject()(searchManagementRepository: SearchManage
* @return
*/
// TODO resolve & test logic of render method (change interface to separate decompound from normal rules)
private def render(solrIndexId: SolrIndexId, separateRulesTxts: Boolean, renderCompoundsRulesTxt: Boolean): String = {
private def render(solrIndexId: SolrIndexId, separateRulesTxts: Boolean, renderCompoundsRulesTxt: Boolean, filterInputTagProperty: String, currentInputTag: InputTag): String = {

val retQuerqyRulesTxt = new StringBuilder()

Expand All @@ -151,6 +165,7 @@ class QuerqyRulesTxtGenerator @Inject()(searchManagementRepository: SearchManage
.filter(i => i.trimmedTerm.nonEmpty)
// TODO it needs to be ensured, that a rule not only exists in the list, are active, BUT also has a filled term (after trim)
.filter(_.hasAnyActiveRules)
.filter(searchInputWithRules => hasValidInputTagValue(searchInputWithRules, filterInputTagProperty, currentInputTag))

// TODO merge decompound identification login with ApiController :: validateSearchInputToErrMsg

Expand All @@ -170,12 +185,12 @@ class QuerqyRulesTxtGenerator @Inject()(searchManagementRepository: SearchManage
renderListSearchInputRules(separateRules(listSearchInput))
}

def renderSingleRulesTxt(solrIndexId: SolrIndexId): String = {
render(solrIndexId, false, false)
def renderSingleRulesTxt(solrIndexId: SolrIndexId, filterInputTagProperty: String, currentInputTag: InputTag): String = {
render(solrIndexId, false, false, filterInputTagProperty, currentInputTag)
}

def renderSeparatedRulesTxts(solrIndexId: SolrIndexId, renderCompoundsRulesTxt: Boolean): String = {
render(solrIndexId, true, renderCompoundsRulesTxt)
def renderSeparatedRulesTxts(solrIndexId: SolrIndexId, renderCompoundsRulesTxt: Boolean, filterInputTagProperty: String, currentInputTag: InputTag): String = {
render(solrIndexId, true, renderCompoundsRulesTxt, filterInputTagProperty, currentInputTag)
}

/**
Expand Down
Loading