optimizations
This commit is contained in:
@@ -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;
|
||||
}
|
||||
return wins
|
||||
}
|
||||
winCount += 1;
|
||||
|
||||
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,28 +87,81 @@ 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<AllStats> {
|
||||
|
||||
let startTime = Date()
|
||||
|
||||
var date = getStartDate()
|
||||
var previousMonths:[CODDate] = []
|
||||
|
||||
@@ -118,15 +171,22 @@ struct StatsController: RouteCollection {
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user