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

Bug 5279/all rules compliance calculate at each rule #585

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,95 +309,97 @@ class RuleGrid(
, groupsLib: FullNodeGroupCategory
, nodes: Map[NodeId, NodeInfo]
, rules : List[Rule]
) : List[Line] = { rules.map {
rule =>
) : List[Line] = {

// we compute beforehand the compliance, so that we have a single big query
// to the database
val complianceMap = computeCompliances(rules.toSet)

val trackerVariables: Box[Seq[(Directive, ActiveTechnique, Technique)]] = {
sequence(rule.directiveIds.toSeq) { id =>
directivesLib.allDirectives.get(id) match {
case Some((activeTechnique, directive)) =>
techniqueRepository.getLastTechniqueByName(activeTechnique.techniqueName) match {
case None =>
Failure(s"Can not find Technique for activeTechnique with name ${activeTechnique.techniqueName} referenced in Rule with ID ${rule.id.value}")
case Some(technique) =>
Full((directive, activeTechnique.toActiveTechnique, technique))
}
case None => //it's an error if the directive ID is defined and found but it is not attached to an activeTechnique
val error = Failure(s"Can not find Directive with ID '${id.value}' referenced in Rule with ID '${rule.id.value}'")
logger.debug(error.messageChain, error)
error
rules.map { rule =>

val trackerVariables: Box[Seq[(Directive, ActiveTechnique, Technique)]] = {
sequence(rule.directiveIds.toSeq) { id =>
directivesLib.allDirectives.get(id) match {
case Some((activeTechnique, directive)) =>
techniqueRepository.getLastTechniqueByName(activeTechnique.techniqueName) match {
case None =>
Failure(s"Can not find Technique for activeTechnique with name ${activeTechnique.techniqueName} referenced in Rule with ID ${rule.id.value}")
case Some(technique) =>
Full((directive, activeTechnique.toActiveTechnique, technique))
}
case None => //it's an error if the directive ID is defined and found but it is not attached to an activeTechnique
val error = Failure(s"Can not find Directive with ID '${id.value}' referenced in Rule with ID '${rule.id.value}'")
logger.debug(error.messageChain, error)
error
}
}
}
}

val targetsInfo = sequence(rule.targets.toSeq) {
case json:CompositeRuleTarget =>
val ruleTargetInfo = RuleTargetInfo(json,"","",true,false)
Full(ruleTargetInfo)
case target =>
groupsLib.allTargets.get(target) match {
case Some(t) =>
Full(t.toTargetInfo)
case None =>
Failure(s"Can not find full information for target '${target}' referenced in Rule with ID '${rule.id.value}'")
}
}.map(x => x.toSet)

(trackerVariables, targetsInfo) match {
case (Full(seq), Full(targets)) =>
val applicationStatus = getRuleApplicationStatus(rule, groupsLib, directivesLib, nodes)
val compliance = applicationStatus match {
case _:NotAppliedStatus =>
Full(None)
case _ =>
complianceMap.getOrElse(rule.id, Failure(s"Error when getting compliance for Rule ${rule.name}"))
}
compliance match {
case e:EmptyBox =>
ErrorLine(rule, trackerVariables, targetsInfo)
case Full(value) =>
OKLine(rule, value, applicationStatus, seq, targets)
}
case (x,y) =>
if(rule.isEnabledStatus) {
//the Rule has some error, try to disable it
//and be sure to not get a Rules from a modification pop-up, because we don't want to commit changes along
//with the disable.
//it's only a try, so it may fails, we won't try again
( for {
r <- roRuleRepository.get(rule.id)
_ <- woRuleRepository.update(
r.copy(isEnabledStatus=false)
, ModificationId(uuidGen.newUuid)
, RudderEventActor
, Some("Rule automatically disabled because it contains error (bad target or bad directives)")
)
} yield {
logger.warn(s"Disabling rule '${rule.name}' (ID: '${rule.id.value}') because it refers missing objects. Go to rule's details and save, then enable it back to correct the problem.")
x match {
case f: Failure =>
logger.warn(s"Rule '${rule.name}' (ID: '${rule.id.value}' directive problem: " + f.messageChain)
case _ => // Directive Ok!
}
y match {
case f: Failure =>
logger.warn(s"Rule '${rule.name}' (ID: '${rule.id.value}' target problem: " + f.messageChain)
case _ => // Group Ok!
}
} ) match {
case eb: EmptyBox =>
val e = eb ?~! s"Error when to trying to disable the rule '${rule.name}' (ID: '${rule.id.value}') because it's data are unconsistant."
logger.warn(e.messageChain)
e.rootExceptionCause.foreach { ex =>
logger.warn("Exception was: ", ex)
val targetsInfo = sequence(rule.targets.toSeq) {
case json:CompositeRuleTarget =>
val ruleTargetInfo = RuleTargetInfo(json,"","",true,false)
Full(ruleTargetInfo)
case target =>
groupsLib.allTargets.get(target) match {
case Some(t) =>
Full(t.toTargetInfo)
case None =>
Failure(s"Can not find full information for target '${target}' referenced in Rule with ID '${rule.id.value}'")
}
}.map(x => x.toSet)

(trackerVariables, targetsInfo) match {
case (Full(seq), Full(targets)) =>
val applicationStatus = getRuleApplicationStatus(rule, groupsLib, directivesLib, nodes)
val compliance = applicationStatus match {
case _:NotAppliedStatus =>
Full(None)
case _ =>
complianceMap.getOrElse(rule.id, Failure(s"Error when getting compliance for Rule ${rule.name}"))
}
compliance match {
case e:EmptyBox =>
ErrorLine(rule, trackerVariables, targetsInfo)
case Full(value) =>
OKLine(rule, value, applicationStatus, seq, targets)
}
case (x,y) =>
if(rule.isEnabledStatus) {
//the Rule has some error, try to disable it
//and be sure to not get a Rules from a modification pop-up, because we don't want to commit changes along
//with the disable.
//it's only a try, so it may fails, we won't try again
( for {
r <- roRuleRepository.get(rule.id)
_ <- woRuleRepository.update(
r.copy(isEnabledStatus=false)
, ModificationId(uuidGen.newUuid)
, RudderEventActor
, Some("Rule automatically disabled because it contains error (bad target or bad directives)")
)
} yield {
logger.warn(s"Disabling rule '${rule.name}' (ID: '${rule.id.value}') because it refers missing objects. Go to rule's details and save, then enable it back to correct the problem.")
x match {
case f: Failure =>
logger.warn(s"Rule '${rule.name}' (ID: '${rule.id.value}' directive problem: " + f.messageChain)
case _ => // Directive Ok!
}
y match {
case f: Failure =>
logger.warn(s"Rule '${rule.name}' (ID: '${rule.id.value}' target problem: " + f.messageChain)
case _ => // Group Ok!
}
case _ => //ok
} ) match {
case eb: EmptyBox =>
val e = eb ?~! s"Error when to trying to disable the rule '${rule.name}' (ID: '${rule.id.value}') because it's data are unconsistant."
logger.warn(e.messageChain)
e.rootExceptionCause.foreach { ex =>
logger.warn("Exception was: ", ex)
}
case _ => //ok
}
}
}
ErrorLine(rule, x, y)
ErrorLine(rule, x, y)
}
}
}
Expand Down