Skip to content

Commit

Permalink
Fixes #23967: Rudder score backend and API
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceMacBuche committed Jan 15, 2024
1 parent 7eb72e4 commit df8b61d
Show file tree
Hide file tree
Showing 26 changed files with 1,113 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,20 @@ CREATE TABLE NodeFacts (
, acceptRefuseFact jsonb -- the big node fact data structure
, deleteEvent jsonb -- { 'date': 'rfc3339 timestamp', 'actor': 'actor name' }
);


Create table GlobalScore (
nodeId text primary key
, score text
, message text
, details jsonb
);

Create table scoreDetails (
nodeId text
, scoreId text
, score text
, message text
, details jsonb
, PRIMARY KEY (nodeId, scoreId)
);
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,44 @@ final case class CompliancePercent(
val compliance = success + repaired + notApplicable + compliant + auditNotApplicable
}

final case class ComplianceSerializable(
applying: Option[Double],
successNotApplicable: Option[Double],
successAlreadyOK: Option[Double],
successRepaired: Option[Double],
error: Option[Double],
auditCompliant: Option[Double],
auditNonCompliant: Option[Double],
auditError: Option[Double],
auditNotApplicable: Option[Double],
unexpectedUnknownComponent: Option[Double],
unexpectedMissingComponent: Option[Double],
noReport: Option[Double],
reportsDisabled: Option[Double],
badPolicyMode: Option[Double]
)

object ComplianceSerializable {
def fromPercent(compliancePercent: CompliancePercent) = {
ComplianceSerializable(
if (compliancePercent.pending == 0) None else Some(compliancePercent.pending),
if (compliancePercent.notApplicable == 0) None else Some(compliancePercent.notApplicable),
if (compliancePercent.success == 0) None else Some(compliancePercent.success),
if (compliancePercent.repaired == 0) None else Some(compliancePercent.repaired),
if (compliancePercent.error == 0) None else Some(compliancePercent.error),
if (compliancePercent.compliant == 0) None else Some(compliancePercent.compliant),
if (compliancePercent.nonCompliant == 0) None else Some(compliancePercent.nonCompliant),
if (compliancePercent.auditError == 0) None else Some(compliancePercent.auditError),
if (compliancePercent.auditNotApplicable == 0) None else Some(compliancePercent.auditNotApplicable),
if (compliancePercent.unexpected == 0) None else Some(compliancePercent.unexpected),
if (compliancePercent.missing == 0) None else Some(compliancePercent.missing),
if (compliancePercent.noAnswer == 0) None else Some(compliancePercent.noAnswer),
if (compliancePercent.reportsDisabled == 0) None else Some(compliancePercent.reportsDisabled),
if (compliancePercent.badPolicyMode == 0) None else Some(compliancePercent.badPolicyMode)
)
}
}

object CompliancePercent {

// a correspondance array between worse order in `ReportType` and the order of fields in `ComplianceLevel`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import com.normation.rudder.facts.nodes.QueryContext
import com.normation.rudder.hooks.HookEnvPairs
import com.normation.rudder.hooks.PureHooksLogger
import com.normation.rudder.hooks.RunHooks
import com.normation.rudder.score.InventoryScoreEvent
import com.normation.rudder.score.ScoreServiceManager
import com.normation.utils.StringUuidGenerator
import com.normation.zio.currentTimeMillis
import zio._
Expand Down Expand Up @@ -175,3 +177,10 @@ class TriggerPolicyGenerationPostCommit[A](
} else ZIO.unit) *> records.succeed
}
}

class TriggerInventoryScorePostCommit[A](scoreServiceManager: ScoreServiceManager) extends PostCommit[A] {
override def name: String = "trigger Score computation on inventory update"
override def apply(inventory: Inventory, records: A): IOResult[A] = {
scoreServiceManager.handleEvent(InventoryScoreEvent(inventory.node.main.id, inventory)) *> records.succeed
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
*************************************************************************************
* Copyright 2024 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/

package com.normation.rudder.score

import com.normation.errors.PureResult
import com.normation.inventory.domain.InventoryError.Inconsistency
import com.normation.inventory.domain.NodeId
import com.normation.rudder.domain.reports.ComplianceSerializable
import com.normation.rudder.score.ScoreValue.A
import com.normation.rudder.score.ScoreValue.B
import com.normation.rudder.score.ScoreValue.C
import com.normation.rudder.score.ScoreValue.D
import com.normation.rudder.score.ScoreValue.E
import zio.json.DeriveJsonEncoder
import zio.json.EncoderOps
import zio.json.JsonEncoder

object ComplianceScoreEventHandler extends ScoreEventHandler {
implicit val compliancePercentEncoder: JsonEncoder[ComplianceSerializable] = DeriveJsonEncoder.gen
def handle(event: ScoreEvent): PureResult[List[(NodeId, List[Score])]] = {

event match {
case ComplianceScoreEvent(n, percent) =>
(for {
p <- ComplianceSerializable.fromPercent(percent).toJsonAST
} yield {
val scoreId = "compliance"
val score = if (percent.compliance >= 100) {
Score(scoreId, A, "Node is compliant at 100%", p)
} else if (percent.compliance >= 75) {
Score(scoreId, B, "Node is compliant at least at 75%", p)
} else if (percent.compliance >= 50) {
ComplianceSerializable.fromPercent(percent)
Score(scoreId, C, "Node is compliant at least at 50%", p)
} else if (percent.compliance >= 25) {
Score(scoreId, D, "Node is compliant at least at 25%", p)
} else {
Score(scoreId, E, "Node is compliant at less then 25%", p)
}
((n, score :: Nil) :: Nil)
}) match {
case Left(err) => Left(Inconsistency(err))
case Right(r) => Right(r)
}
case _ => Right(Nil)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
*************************************************************************************
* Copyright 2024 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/

package com.normation.rudder.score

import com.normation.errors.IOResult
import com.normation.inventory.domain.NodeId
import com.normation.rudder.db.Doobie
import doobie.implicits._
import doobie.implicits.toSqlInterpolator
import zio.interop.catz._

trait GlobalScoreRepository {
def getAll(): IOResult[Map[NodeId, GlobalScore]]
def get(id: NodeId): IOResult[Option[GlobalScore]]
def delete(id: NodeId): IOResult[Unit]
def save(nodeId: NodeId, globalScore: GlobalScore): IOResult[(NodeId, GlobalScore)]
}

object GlobalScoreRepositoryImpl {

import ScoreSerializer._
import com.normation.rudder.db.json.implicits._
import doobie._
implicit val getScoreValue: Get[ScoreValue] = Get[String].temap(ScoreValue.fromString)
implicit val putScoreValue: Put[ScoreValue] = Put[String].contramap(_.value)

implicit val stateWrite: Meta[List[NoDetailsScore]] = new Meta(pgDecoderGet, pgEncoderPut)

implicit val globalScoreWrite: Write[(NodeId, GlobalScore)] = {
Write[(String, ScoreValue, String, List[NoDetailsScore])].contramap {
case (nodeId: NodeId, score: GlobalScore) =>
(nodeId.value, score.value, score.message, score.details)
}
}

implicit val globalScoreRead: Read[GlobalScore] = {
Read[(ScoreValue, String, List[NoDetailsScore])].map { d: (ScoreValue, String, List[NoDetailsScore]) =>
GlobalScore(d._1, d._2, d._3)
}
}
implicit val globalScoreWithIdRead: Read[(NodeId, GlobalScore)] = {
Read[(String, ScoreValue, String, List[NoDetailsScore])].map { d: (String, ScoreValue, String, List[NoDetailsScore]) =>
(NodeId(d._1), GlobalScore(d._2, d._3, d._4))
}
}

}

class GlobalScoreRepositoryImpl(doobie: Doobie) extends GlobalScoreRepository {
import GlobalScoreRepositoryImpl._
import doobie._

def save(nodeId: NodeId, globalScore: GlobalScore): IOResult[(NodeId, GlobalScore)] = {
val query = {
sql"""insert into GlobalScore (nodeId, score, message, details) values (${(nodeId, globalScore)})
| ON CONFLICT (nodeId) DO UPDATE
| SET score = ${globalScore.value}, message = ${globalScore.message}, details = ${globalScore.details} ; """.stripMargin
}

transactIOResult(s"error when inserting global score for node '${nodeId.value}''")(xa => query.update.run.transact(xa)).map(
_ => (nodeId, globalScore)
)
}

override def getAll(): IOResult[Map[NodeId, GlobalScore]] = {
val q = sql"select nodeId, score, message, details from GlobalScore"
transactIOResult(s"error when getting global scores for node")(xa => q.query[(NodeId, GlobalScore)].to[List].transact(xa))
.map(_.groupMapReduce(_._1)(_._2) { case (_, res) => res })
}

override def get(id: NodeId): IOResult[Option[GlobalScore]] = {
val q = sql"select score, message, details from GlobalScore where nodeId = ${id.value}"
transactIOResult(s"error when getting global score for node ${id.value}")(xa => q.query[GlobalScore].option.transact(xa))
}

override def delete(id: NodeId): IOResult[Unit] = {
val q = sql"delete from GlobalScore nodeId = ${id.value}"
transactIOResult(s"error when getting global score for node ${id.value}")(xa => q.update.run.transact(xa).unit)
}
}
Loading

0 comments on commit df8b61d

Please sign in to comment.