attempt to speed stuff up

This commit is contained in:
Michael Simard
2021-01-04 17:07:43 -06:00
parent 0e4c0ef7c1
commit ae66dfd1d6

View File

@@ -50,12 +50,9 @@ struct StatsController: RouteCollection {
return Match.query(on: req.db).count().flatMap { (totalMatches) -> EventLoopFuture<MatchHistory> in
return Match.query(on: req.db).sort(\.$date, .descending).limit(20).all().map { (matches) -> (MatchHistory) in
return MatchHistory(total:totalMatches, matches: matches, hasMorePages: totalMatches > 20)
}
}
}
}
@@ -550,20 +547,53 @@ struct StatsController: RouteCollection {
func overall(req: Request) throws -> EventLoopFuture<OverallStats> {
return Match.query(on: req.db).all().map { (matches) -> OverallStats in
let overallStats = self.getStatsWithMostRecentDailyRecord(matches: matches)
// print ( Date().timeIntervalSince(startTime))
let mwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw" && self.shouldCountMatch(match: match )
}))
let queue = DispatchQueue(label: "com.sledsoft.cod-tracker.queue", attributes: .concurrent)
let group = DispatchGroup()
let bocwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw" && self.shouldCountMatch(match: match )
}))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
var overallStats:StatsWithMostRecentDailyRecord?
var mwStats:StatsWithMostRecentDailyRecord?
var bocwStats:StatsWithMostRecentDailyRecord?
var mostRecentStats:Stats?
group.enter()
queue.async {
overallStats = self.getStatsWithMostRecentDailyRecord(matches: matches)
group.leave()
}
group.enter()
queue.async {
mwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw" && self.shouldCountMatch(match: match )
}))
group.leave()
}
return OverallStats(overall: overallStats, mwStats: mwStats, bocwStats: bocwStats, mostRecentRecord: "\(mostRecentDailyStats.totalWins) - \(mostRecentDailyStats.totalLosses)")
group.enter()
queue.async {
bocwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw" && self.shouldCountMatch(match: match )
}))
group.leave()
}
group.enter()
queue.async {
mostRecentStats = self.mostRecentDailyStats(matches: matches)
group.leave()
}
group.wait()
return OverallStats(overall: overallStats!, mwStats: mwStats!, bocwStats: bocwStats!, mostRecentRecord: "\(mostRecentStats!.totalWins) - \(mostRecentStats!.totalLosses)")
}
}