Skip to content

Commit

Permalink
Fixes #15041: The computation of changes by rules, at start of web in…
Browse files Browse the repository at this point in the history
…terface, is quite slow
  • Loading branch information
ncharles committed Jun 9, 2019
1 parent 6dc649a commit f2421d2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
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,34 @@ 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]]] = {
logger.debug(s"Fetching all changes for intervals ${intervals.mkString(",")}")
val beginTime = System.currentTimeMillis()
for {
(interval, i) <- intervals.zipWithIndex

rules = (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)}
}
endQuery = System.currentTimeMillis()
_ = logger.debug(s"Fetched all changes in intervals in ${(endQuery - beginTime)} ms")
result <- rules.map(x => x.groupBy(_._1).mapValues( _.groupBy(_._2).mapValues(_.map ( _._3).head))) //head non empty due to groupBy, and seq == 1 by query
} yield {
result
}
}

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 +432,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

0 comments on commit f2421d2

Please sign in to comment.