From 05055d76823984dfc4b5583fe96783af49c41c45 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Mon, 16 Nov 2020 18:18:41 -0600 Subject: [PATCH] optimizations --- Sources/App/Controllers/StatsController.swift | 98 ++++++++++++++++--- 1 file changed, 82 insertions(+), 16 deletions(-) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 9fcf7d0..3158bb6 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -26,21 +26,21 @@ struct StatsController: RouteCollection { func getStats(matches:[Match]) -> Stats{ - let winCount:Double = matches.reduce(0) { (wins, match) -> Double in + var winCount = 0 + var lossCount = 0 + + for match in matches { if match.win == true { - return wins + 1; + winCount += 1; + } - return wins - } - - let lossCount:Double = matches.reduce(0) { (losses, match) -> Double in if match.win == false { - return losses + 1; + lossCount += 1; } - return losses } + - let ratio = self.getRatio(num: winCount, den: lossCount) + let ratio = self.getRatio(num: Double(winCount), den: Double(lossCount)) return Stats(winLoss: ratio, totalWins: Int(winCount), totalLosses: Int(lossCount)) } @@ -87,46 +87,106 @@ struct StatsController: RouteCollection { func getCumulativeWinLossRatios(matches:[Match]) -> [DataPoint] { + let startTime = Date() + + print ("c \(Date().timeIntervalSince(startTime))") + let daysPlayed = getDaysPlayed(matches: matches) - + print ("c \(Date().timeIntervalSince(startTime))") + var cumulativeRatios : [DataPoint] = [] var cumulativeWins:Int = 0 var cumulativeLosses:Int = 0 - for (i, day) in daysPlayed.enumerated() { - let stats = self.getStats(matches: matches.filter({ (match) -> Bool in - return match.date.day == day.day && match.date.year == day.year && match.date.month == day.month - })) + var dayMatches:[[Match]] = [] + + var currentDay = daysPlayed.first?.day ?? 0 + var currentMonth = daysPlayed.first?.month ?? 0 + var currentYear = daysPlayed.first?.year ?? 0 + + let sortedMatches = matches.sorted { (m1, m2) -> Bool in + return m1.date < m2.date + } + + var currentMatches:[Match] = [] + + for match in sortedMatches { + if match.date.year == currentYear && match.date.month == currentMonth && match.date.day == currentDay { + currentMatches.append(match) + } + else { + dayMatches.append(currentMatches) + currentMatches = [match] + currentDay = match.date.day + currentYear = match.date.year + currentMonth = match.date.month + } + } + + for (i, matchGroup) in dayMatches.enumerated() { + + + let stats = self.getStats(matches: matchGroup) cumulativeWins = cumulativeWins + stats.totalWins; cumulativeLosses = cumulativeLosses + stats.totalLosses; - cumulativeRatios.append( DataPoint(x: Double(i), y: (Double(cumulativeWins) / (Double(cumulativeLosses) )), label: ("\(Utilities.monthToString(month: day.month)) \(day.day)"))) + cumulativeRatios.append( DataPoint(x: Double(i), y: (Double(cumulativeWins) / (Double(cumulativeLosses) )), label: ("\(Utilities.monthToString(month: matchGroup.first!.date.month)) \( matchGroup.first!.date.day)"))) + } + +// for (i, day) in daysPlayed.enumerated() { +// print ("ca \(Date().timeIntervalSince(startTime))") +// +// +// let stats = self.getStats(matches: matches.filter({ (match) -> Bool in +// return match.date.day == day.day && match.date.year == day.year && match.date.month == day.month +// })) +// +// print ("cb \(Date().timeIntervalSince(startTime))") +// +// cumulativeWins = cumulativeWins + stats.totalWins; +// cumulativeLosses = cumulativeLosses + stats.totalLosses; +// cumulativeRatios.append( DataPoint(x: Double(i), y: (Double(cumulativeWins) / (Double(cumulativeLosses) )), label: ("\(Utilities.monthToString(month: day.month)) \(day.day)"))) +// +// print ("cc \(Date().timeIntervalSince(startTime))") +// +// } +// print ("c \(Date().timeIntervalSince(startTime))") + return cumulativeRatios } func test(req: Request) throws -> EventLoopFuture { + let startTime = Date() + var date = getStartDate() var previousMonths:[CODDate] = [] - + repeat { //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0)) date = Calendar.current.date(byAdding: .month, value: 1, to: date)! } while (date.month != (Date().month + 1) || date.year != Date().year) + print (date - Date().timeIntervalSince(startTime)) return Match.query(on: req.db).all().map { (matches) -> AllStats in let overallStats = self.getStats(matches: matches) + print ( Date().timeIntervalSince(startTime)) + let mwStats = self.getStats(matches: matches.filter({ (match) -> Bool in return match.codGame == "mw" })) + print ( Date().timeIntervalSince(startTime)) + let bocwStats = self.getStats(matches: matches.filter({ (match) -> Bool in return match.codGame == "bocw" })) + print ( Date().timeIntervalSince(startTime)) + let monthlyStats = previousMonths.reversed().map { (codDate) -> MonthStats in let relevantMatches = matches.filter { (match) -> Bool in @@ -134,15 +194,21 @@ struct StatsController: RouteCollection { } return MonthStats(month: codDate.month, year: codDate.year, stats: self.getStats(matches: relevantMatches )) } + print ( Date().timeIntervalSince(startTime)) + let cumulativeWinLossRatios = self.getCumulativeWinLossRatios(matches: matches) + print ( Date().timeIntervalSince(startTime)) + let highestWinLossRatio = cumulativeWinLossRatios.reduce("0") { (highestRatio, dataPoint) -> String in if dataPoint.y > Double(highestRatio)!{ return String(dataPoint.y) } return highestRatio } + print ( Date().timeIntervalSince(startTime)) + let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)