Faster stat processing
This commit is contained in:
@@ -11,22 +11,183 @@ struct CODDate {
|
||||
let minute:Int
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct StatsController: RouteCollection {
|
||||
func boot(routes: RoutesBuilder) throws {
|
||||
let statsRoute = routes.grouped("cod-tracker","api", "stats")
|
||||
|
||||
statsRoute.get("allMatches", use: index)
|
||||
statsRoute.get("totalWins", use: totalWins)
|
||||
statsRoute.get("totalLosses", use: totalLosses)
|
||||
statsRoute.get("overall", use: overall)
|
||||
statsRoute.get("all", use: all)
|
||||
statsRoute.get("all", use: test)
|
||||
statsRoute.get("allDaily", use: allDaily)
|
||||
statsRoute.get("test", use: test)
|
||||
}
|
||||
|
||||
|
||||
func getStats(matches:[Match]) -> Stats{
|
||||
|
||||
let winCount:Double = matches.reduce(0) { (wins, match) -> Double in
|
||||
if match.win == true {
|
||||
return wins + 1;
|
||||
}
|
||||
return wins
|
||||
}
|
||||
|
||||
let lossCount:Double = matches.reduce(0) { (losses, match) -> Double in
|
||||
if match.win == false {
|
||||
return losses + 1;
|
||||
}
|
||||
return losses
|
||||
}
|
||||
|
||||
let ratio = self.getRatio(num: winCount, den: lossCount)
|
||||
|
||||
return Stats(winLoss: ratio, totalWins: Int(winCount), totalLosses: Int(lossCount))
|
||||
}
|
||||
|
||||
|
||||
func mostRecentDailyStats (matches:[Match]) -> Stats{
|
||||
|
||||
let daysPlayed = getDaysPlayed(matches: matches)
|
||||
|
||||
return getStats(matches: matches.filter({ (match) -> Bool in
|
||||
return match.date.day == daysPlayed.first?.day && match.date.month == daysPlayed.first?.month && match.date.year == daysPlayed.first?.year && match.date.day == daysPlayed.first?.day
|
||||
}))
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private func getDaysPlayed(matches:[Match]) -> [CODDate] {
|
||||
|
||||
var sortedMatches = matches
|
||||
sortedMatches.sort { (match1, match2) -> Bool in
|
||||
return match1.date < match2.date
|
||||
}
|
||||
|
||||
return sortedMatches.map { (match) -> CODDate in
|
||||
return CODDate(month: match.date.month, year: match.date.year, day: match.date.day, hour: match.date.hour, minute: match.date.minute)
|
||||
}.reduce([CODDate]()) { (datesPlayed, codDate) -> [CODDate] in
|
||||
|
||||
if datesPlayed.contains(where: { (existingDate) -> Bool in
|
||||
if codDate.month == existingDate.month && codDate.year == existingDate.year && existingDate.day == codDate.day{
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}){
|
||||
return datesPlayed
|
||||
}else {
|
||||
|
||||
return datesPlayed + [codDate]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func getCumulativeWinLossRatios(matches:[Match]) -> [DataPoint] {
|
||||
|
||||
let daysPlayed = getDaysPlayed(matches: matches)
|
||||
|
||||
var cumulativeRatios : [DataPoint] = []
|
||||
var cumulativeWins:Int = 0
|
||||
var cumulativeLosses:Int = 0
|
||||
|
||||
for (i, day) in daysPlayed.enumerated() {
|
||||
let stats = getStats(matches: matches.filter({ (match) -> Bool in
|
||||
return match.date.day == day.day && match.date.year == day.year && match.date.month == day.month
|
||||
}))
|
||||
|
||||
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)")))
|
||||
}
|
||||
|
||||
return cumulativeRatios
|
||||
}
|
||||
|
||||
|
||||
func test(req: Request) throws -> EventLoopFuture<AllStats> {
|
||||
|
||||
var date = getStartDate()
|
||||
var previousMonths:[CODDate] = []
|
||||
|
||||
repeat {
|
||||
//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))
|
||||
date = Calendar.current.date(byAdding: .month, value: 1, to: date)!
|
||||
} while (date.month != (Date().month + 1) || date.year != Date().year)
|
||||
|
||||
|
||||
return Match.query(on: req.db).all().map { (matches) -> AllStats in
|
||||
let overallStats = getStats(matches: matches)
|
||||
let mwStats = getStats(matches: matches.filter({ (match) -> Bool in
|
||||
return match.codGame == "mw"
|
||||
}))
|
||||
let bocwStats = getStats(matches: matches.filter({ (match) -> Bool in
|
||||
return match.codGame == "bocw"
|
||||
}))
|
||||
|
||||
let monthlyStats = previousMonths.reversed().map { (codDate) -> MonthStats in
|
||||
let relevantMatches = matches.filter { (match) -> Bool in
|
||||
return match.date.month == codDate.month
|
||||
}
|
||||
return MonthStats(month: codDate.month, year: codDate.year, stats: getStats(matches: relevantMatches ))
|
||||
}
|
||||
|
||||
let cumulativeWinLossRatios = getCumulativeWinLossRatios(matches: matches)
|
||||
|
||||
let highestWinLossRatio = cumulativeWinLossRatios.reduce("0") { (highestRatio, dataPoint) -> String in
|
||||
if dataPoint.y > Double(highestRatio)!{
|
||||
return String(dataPoint.y)
|
||||
}
|
||||
return highestRatio
|
||||
}
|
||||
|
||||
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
|
||||
|
||||
return AllStats.init(overall: overallStats,mwStats: mwStats, bocwStats: bocwStats, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeWinLossRatios, mostRecentRecord: "\(mostRecentDailyStats.totalWins) - \(mostRecentDailyStats.totalLosses)")
|
||||
}
|
||||
|
||||
|
||||
// func getMonthStats (_ remaining: ArraySlice<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
|
||||
// var remaining = remaining
|
||||
// if let first = remaining.popLast() {
|
||||
//
|
||||
// return getstatsForMonth(year: first.year, month: first.month, req: req).flatMap { [remaining, allMonthlyStats] (stats) -> EventLoopFuture<[MonthStats]> in
|
||||
// var allMonthlyStats = allMonthlyStats
|
||||
// allMonthlyStats.append(MonthStats(month: first.month, year: first.year, stats:stats ))
|
||||
// return getMonthStats(remaining, allMonthlyStats:&allMonthlyStats, eventLoop: eventLoop)
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// return req.eventLoop.makeSucceededFuture(allMonthlyStats)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// var stats:[MonthStats] = []
|
||||
// let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop)
|
||||
// let cumulativeRatios = getCumulativeWinLossRatios(req: req)
|
||||
// let mostRecentDayStats = mostRecentDailyStats(req: req)
|
||||
//
|
||||
// return try overall(req: req).and(monthstats).and(cumulativeRatios).and(mostRecentDayStats).map { arg -> AllStats in
|
||||
//
|
||||
// let (((overall, monthlyStats), cumulativeRatios), mostRecentDayStats) = arg
|
||||
// let highestWinLossRatio = cumulativeRatios.reduce("0") { (highestRatio, dataPoint) -> String in
|
||||
// if dataPoint.y > Double(highestRatio)!{
|
||||
// return String(dataPoint.y)
|
||||
// }
|
||||
// return highestRatio
|
||||
// }
|
||||
//
|
||||
// return AllStats.init(overall: overall,mwStats: overall, bocwStats: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)")
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func index(req: Request) throws -> EventLoopFuture<[Match]> {
|
||||
return Match.query(on: req.db).sort(\.$date).all()
|
||||
}
|
||||
@@ -221,8 +382,6 @@ struct StatsController: RouteCollection {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
|
||||
|
||||
return getstatsForMonth(year: first.year, month: first.month, req: req).flatMap { [remaining, allMonthlyStats] (stats) -> EventLoopFuture<[MonthStats]> in
|
||||
var allMonthlyStats = allMonthlyStats
|
||||
allMonthlyStats.append(MonthStats(month: first.month, year: first.year, stats:stats ))
|
||||
@@ -249,7 +408,7 @@ struct StatsController: RouteCollection {
|
||||
return highestRatio
|
||||
}
|
||||
|
||||
return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)")
|
||||
return AllStats.init(overall: overall,mwStats: overall, bocwStats: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,13 +470,6 @@ struct StatsController: RouteCollection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// repeat {
|
||||
// previousDays.append(CODDate(month: date.month, year: date.year, day: date.day, hour: date.hour, minute: date.minute))
|
||||
// date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
|
||||
// } while (date < Date())
|
||||
//
|
||||
// return previousDays
|
||||
}
|
||||
|
||||
|
||||
@@ -360,7 +512,7 @@ struct StatsController: RouteCollection {
|
||||
}
|
||||
return true
|
||||
}).reversed()
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user