forked from Normation/rudder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #23967: Rudder score backend and API
- Loading branch information
1 parent
7eb72e4
commit df8b61d
Showing
26 changed files
with
1,113 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...ources/rudder/rudder-core/src/main/scala/com/normation/rudder/score/ComplianceScore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
.../rudder/rudder-core/src/main/scala/com/normation/rudder/score/GlobalScoreRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.