From 8d94b42439403537d353b4d51f2b738a3e8a1061 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Mon, 16 Nov 2020 07:48:21 -0600 Subject: [PATCH] Faster stat processing --- Sources/App/Controllers/StatsController.swift | 184 ++++++++++++++++-- Sources/App/Migrations/AddCODGame.swift | 32 +++ Sources/App/Migrations/CreateMatch.swift | 19 -- Sources/App/Models/AllStats.swift | 7 +- Sources/App/Models/Match.swift | 5 +- Sources/App/configure.swift | 4 +- 6 files changed, 213 insertions(+), 38 deletions(-) create mode 100644 Sources/App/Migrations/AddCODGame.swift delete mode 100644 Sources/App/Migrations/CreateMatch.swift diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index e8fb424..a0c4442 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -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 { + + 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) + + + 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, 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.. 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() - + ) } } diff --git a/Sources/App/Migrations/AddCODGame.swift b/Sources/App/Migrations/AddCODGame.swift new file mode 100644 index 0000000..cf9f8f8 --- /dev/null +++ b/Sources/App/Migrations/AddCODGame.swift @@ -0,0 +1,32 @@ +import Fluent + +//struct CreateMatch: Migration { +// func prepare(on database: Database) -> EventLoopFuture { +// return database.schema("match") +// .id() +// .field("map", .string) +// .field("win", .bool) +// .field("date", .datetime) +// .field("roundsWon", .int) +// .field("roundsLost", .int) +// +// .create() +// } +// +// func revert(on database: Database) -> EventLoopFuture { +// return database.schema("matches").delete() +// } +//} + +struct AddCODGame: Migration { + + + func prepare(on database: Database) -> EventLoopFuture { + return database.schema("match").field("codGame",.string).create() + } + + func revert(on database: Database) -> EventLoopFuture { + return database.schema("matches").delete() + } +} + diff --git a/Sources/App/Migrations/CreateMatch.swift b/Sources/App/Migrations/CreateMatch.swift deleted file mode 100644 index 613e871..0000000 --- a/Sources/App/Migrations/CreateMatch.swift +++ /dev/null @@ -1,19 +0,0 @@ -import Fluent - -struct CreateMatch: Migration { - func prepare(on database: Database) -> EventLoopFuture { - return database.schema("match") - .id() - .field("map", .string) - .field("win", .bool) - .field("date", .datetime) - .field("roundsWon", .int) - .field("roundsLost", .int) - - .create() - } - - func revert(on database: Database) -> EventLoopFuture { - return database.schema("matches").delete() - } -} diff --git a/Sources/App/Models/AllStats.swift b/Sources/App/Models/AllStats.swift index 75fa503..f904bc9 100644 --- a/Sources/App/Models/AllStats.swift +++ b/Sources/App/Models/AllStats.swift @@ -26,17 +26,22 @@ struct DataPoint : Content { final class AllStats: Content { var overall:Stats + var mwStats:Stats + var bocwStats:Stats var byMonth: [MonthStats] var highestWinLossRatio:String var dataPoints:[DataPoint] var mostRecentRecord:String - init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint], mostRecentRecord:String) { + init( overall:Stats, mwStats:Stats, bocwStats:Stats, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint], mostRecentRecord:String) { self.overall = overall self.byMonth = byMonth self.highestWinLossRatio = highestWinLossRatio self.dataPoints = dataPoints self.mostRecentRecord = mostRecentRecord + self.mwStats = mwStats; + self.bocwStats = bocwStats; + } } diff --git a/Sources/App/Models/Match.swift b/Sources/App/Models/Match.swift index 49bba54..6b0d482 100644 --- a/Sources/App/Models/Match.swift +++ b/Sources/App/Models/Match.swift @@ -22,16 +22,19 @@ final class Match: Model, Content { @Field(key: "roundsLost") var roundsLost: Int? + @Field(key: "codGame") + var codGame: String init() { } - init(id: UUID? = nil, map:String?, win:Bool, date:Date, roundsWon:Int?, roundsLost:Int?) { + init(id: UUID? = nil, map:String?, win:Bool, date:Date, roundsWon:Int?, roundsLost:Int?, codGame:String) { self.id = id self.map = map self.win = win self.date = date self.roundsWon = roundsWon self.roundsLost = roundsLost + self.codGame = codGame; } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index b02eb91..7d8d250 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -19,7 +19,9 @@ public func configure(_ app: Application) throws { database: Environment.get("DATABASE_NAME") ?? "cod_db" ), as: .psql) - app.migrations.add(CreateMatch()) + //app.migrations.add(CreateMatch()) + app.migrations.add(AddCODGame()) + // register routes try routes(app)