optimizations

This commit is contained in:
Michael Simard
2020-11-16 18:18:41 -06:00
parent 1f7afdff68
commit 05055d7682

View File

@@ -26,21 +26,21 @@ struct StatsController: RouteCollection {
func getStats(matches:[Match]) -> Stats{ 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 { 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 { 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)) return Stats(winLoss: ratio, totalWins: Int(winCount), totalLosses: Int(lossCount))
} }
@@ -87,46 +87,106 @@ struct StatsController: RouteCollection {
func getCumulativeWinLossRatios(matches:[Match]) -> [DataPoint] { func getCumulativeWinLossRatios(matches:[Match]) -> [DataPoint] {
let startTime = Date()
print ("c \(Date().timeIntervalSince(startTime))")
let daysPlayed = getDaysPlayed(matches: matches) let daysPlayed = getDaysPlayed(matches: matches)
print ("c \(Date().timeIntervalSince(startTime))")
var cumulativeRatios : [DataPoint] = [] var cumulativeRatios : [DataPoint] = []
var cumulativeWins:Int = 0 var cumulativeWins:Int = 0
var cumulativeLosses:Int = 0 var cumulativeLosses:Int = 0
for (i, day) in daysPlayed.enumerated() { var dayMatches:[[Match]] = []
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 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; cumulativeWins = cumulativeWins + stats.totalWins;
cumulativeLosses = cumulativeLosses + stats.totalLosses; 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 return cumulativeRatios
} }
func test(req: Request) throws -> EventLoopFuture<AllStats> { func test(req: Request) throws -> EventLoopFuture<AllStats> {
let startTime = Date()
var date = getStartDate() var date = getStartDate()
var previousMonths:[CODDate] = [] var previousMonths:[CODDate] = []
repeat { repeat {
//let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats> //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0)) 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)! date = Calendar.current.date(byAdding: .month, value: 1, to: date)!
} while (date.month != (Date().month + 1) || date.year != Date().year) } 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 return Match.query(on: req.db).all().map { (matches) -> AllStats in
let overallStats = self.getStats(matches: matches) let overallStats = self.getStats(matches: matches)
print ( Date().timeIntervalSince(startTime))
let mwStats = self.getStats(matches: matches.filter({ (match) -> Bool in let mwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw" return match.codGame == "mw"
})) }))
print ( Date().timeIntervalSince(startTime))
let bocwStats = self.getStats(matches: matches.filter({ (match) -> Bool in let bocwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw" return match.codGame == "bocw"
})) }))
print ( Date().timeIntervalSince(startTime))
let monthlyStats = previousMonths.reversed().map { (codDate) -> MonthStats in let monthlyStats = previousMonths.reversed().map { (codDate) -> MonthStats in
let relevantMatches = matches.filter { (match) -> Bool 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 )) return MonthStats(month: codDate.month, year: codDate.year, stats: self.getStats(matches: relevantMatches ))
} }
print ( Date().timeIntervalSince(startTime))
let cumulativeWinLossRatios = self.getCumulativeWinLossRatios(matches: matches) let cumulativeWinLossRatios = self.getCumulativeWinLossRatios(matches: matches)
print ( Date().timeIntervalSince(startTime))
let highestWinLossRatio = cumulativeWinLossRatios.reduce("0") { (highestRatio, dataPoint) -> String in let highestWinLossRatio = cumulativeWinLossRatios.reduce("0") { (highestRatio, dataPoint) -> String in
if dataPoint.y > Double(highestRatio)!{ if dataPoint.y > Double(highestRatio)!{
return String(dataPoint.y) return String(dataPoint.y)
} }
return highestRatio return highestRatio
} }
print ( Date().timeIntervalSince(startTime))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches) let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)