Faster stat processing

This commit is contained in:
Michael Simard
2020-11-16 07:48:21 -06:00
parent aeb6b16afb
commit 8d94b42439
6 changed files with 213 additions and 38 deletions

View File

@@ -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
}

View File

@@ -0,0 +1,32 @@
import Fluent
//struct CreateMatch: Migration {
// func prepare(on database: Database) -> EventLoopFuture<Void> {
// 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<Void> {
// return database.schema("matches").delete()
// }
//}
struct AddCODGame: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema("match").field("codGame",.string).create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema("matches").delete()
}
}

View File

@@ -1,19 +0,0 @@
import Fluent
struct CreateMatch: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
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<Void> {
return database.schema("matches").delete()
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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)