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

Fixes #15041: The computation of changes by rules, at start of web interface, is quite slow #2246

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ trait ReportsRepository {
//return the reports between the two ids, limited to limit number of reports, in asc order of id.
def getReportsByKindBeetween(lower: Long, upper: Long, limit: Int, kinds: List[String]) : Box[Seq[(Long, Reports)]]

def countChangeReportsByBatch(intervals : List[Interval]) : Box[Map[RuleId, Map[Interval, Int]]]

//nodechangesServices
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,31 @@ class ReportsJdbcRepository(doobie: Doobie) extends ReportsRepository with Logga
}).transact(xa).attempt.unsafeRunSync ?~! s"Could not fetch the last completed runs from database."
}

/**
* returns the changes between startTime and now, using intervalInHour interval size
*/

override def countChangeReportsByBatch(intervals : List[Interval]) : Box[Map[RuleId, Map[Interval, Int]]] = {
import com.normation.utils.Control.sequence
logger.debug(s"Fetching all changes for intervals ${intervals.mkString(",")}")
val beginTime = System.currentTimeMillis()
val rules: Box[Seq[Vector[(RuleId, Interval, Int)]]] = sequence(intervals) { interval =>
(query[(RuleId, Int)](
s"""select ruleid, count(*) as number
from ruddersysevents
where eventtype = 'result_repaired' and executionTimeStamp > '${new Timestamp(interval.getStartMillis)}' and executionTimeStamp <= '${new Timestamp(interval.getEndMillis)}'
group by ruleid;
"""
).to[Vector].transact(xa).attempt.unsafeRunSync ?~! s"Error when trying to retrieve change reports on interval ${interval.toString}").map { res =>

res.map { case (ruleid, count) => (ruleid, interval, count) }
}
}
val endQuery = System.currentTimeMillis()
logger.debug(s"Fetched all changes in intervals in ${(endQuery - beginTime)} ms")
rules.map(x => x.flatten.groupBy(_._1).mapValues( _.groupBy(_._2).mapValues(_.map ( _._3).head))) //head non empty due to groupBy, and seq == 1 by query
}

override def countChangeReports(startTime: DateTime, intervalInHour: Int): Box[Map[RuleId, Map[Interval, Int]]] = {
//special mapper to retrieve correct interval. It is dependant of starttime / intervalInHour
implicit val intervalMeta: Meta[Interval] = Meta[Int].xmap(
Expand All @@ -404,6 +429,9 @@ class ReportsJdbcRepository(doobie: Doobie) extends ReportsRepository with Logga
//be careful, extract from 'epoch' gives seconds, not millis
val mod = intervalInHour * 3600
val start = startTime.getMillis / 1000



((query[(RuleId, Int, Interval)](
s"""select ruleid, count(*) as number, ( extract('epoch' from executiontimestamp)::bigint - ${start})/${mod} as interval
from ruddersysevents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ class NodeChangesServiceImpl(
* regroup them by rule, and by interval of 6 hours.
*/
override def countChangesByRuleByInterval(): Box[ChangesByRule] = {
val start = getCurrentValidIntervals(None).map(_.getStart).minBy(_.getMillis)
logger.debug(s"Get all changes on all rules")
val beginTime = System.currentTimeMillis()
val intervals = getCurrentValidIntervals(None)
// Regroup changes by interval
for {
changes <- reportsRepository.countChangeReports(start, 6)
changes <- reportsRepository.countChangeReportsByBatch(intervals)
} yield {
val endTime = System.currentTimeMillis()
logger.debug(s"Fetched all changes for all rules in ${(endTime - beginTime)} ms")
changes
}
}
Expand Down