From ae66dfd1d690a6b9e6d353e08bcdf62e3d52d8f2 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Mon, 4 Jan 2021 17:07:43 -0600 Subject: [PATCH] attempt to speed stuff up --- Sources/App/Controllers/StatsController.swift | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 7c78db5..a33ea7c 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -50,12 +50,9 @@ struct StatsController: RouteCollection { return Match.query(on: req.db).count().flatMap { (totalMatches) -> EventLoopFuture 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 { + 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)") } }