From 72a04f88230014e321cb4e2d1054c20f51c30d43 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Tue, 16 Jun 2020 16:51:19 -0500 Subject: [PATCH] include data points --- Sources/App/Controllers/StatsController.swift | 42 +++++++++++-------- Sources/App/Models/AllStats.swift | 20 +++++++-- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index b3fdf68..949688e 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -10,6 +10,8 @@ struct CODDate { } + + struct StatsController: RouteCollection { func boot(routes: RoutesBuilder) throws { let statsRoute = routes.grouped("cod-tracker","api", "stats") @@ -176,7 +178,6 @@ struct StatsController: RouteCollection { 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)) @@ -205,13 +206,19 @@ struct StatsController: RouteCollection { var stats:[MonthStats] = [] let monthstats = getMonthStats(previousMonths[0.. AllStats in + return try overall(req: req).and(monthstats).and(cumulativeRatios).map { arg -> AllStats in - let ((overall, monthlyStats), highestWinLoss) = arg + let ((overall, monthlyStats), cumulativeRatios) = 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, byMonth: monthlyStats, highestWinLossRatio: highestWinLoss) + return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios ) } } @@ -310,15 +317,15 @@ struct StatsController: RouteCollection { } - func getHighestWinLossRatio(req:Request) -> EventLoopFuture { + func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> { - var previousDays = getDaysPlayed().reversed() + let previousDays = getDaysPlayed().reversed() - func getHighestRatio (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], highestRatio: inout String, eventLoop: EventLoop) -> EventLoopFuture { + func getRatios (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> { var remaining = remaining if let first = remaining.popLast() { - return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, highestRatio] (stats) -> EventLoopFuture in + return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, cumulativeWinLossRatios] (stats) -> EventLoopFuture<[DataPoint]> in var allDailyStats = allDailyStats let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in @@ -328,25 +335,24 @@ struct StatsController: RouteCollection { return total + Double(dailyStats.stats.totalLosses) } - let ratio = self.getRatio(num: totalWins, den: totalLosses) - var highestRatio = highestRatio - if Double(ratio)! > Double(highestRatio)! { - highestRatio = ratio + var cumulativeWinLossRatios = cumulativeWinLossRatios + if !(stats.totalWins == 0 && stats.totalLosses == 0) { + cumulativeWinLossRatios.append(DataPoint(x: "\(Utilities.monthToString(month: first.month)) \(first.day) \(first.year)", y: (totalWins/totalLosses).truncate(places: 2))) } allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) - return getHighestRatio(remaining, allDailyStats:&allDailyStats, highestRatio:&highestRatio, eventLoop: eventLoop) + return getRatios(remaining, allDailyStats:&allDailyStats, cumulativeWinLossRatios:&cumulativeWinLossRatios, eventLoop: eventLoop) } } else { - return req.eventLoop.makeSucceededFuture(highestRatio) + return req.eventLoop.makeSucceededFuture(cumulativeWinLossRatios) } } var stats:[DailyStats] = [] - var highestRatio = "0" + var cumulativeWinLossRatios:[DataPoint] = [DataPoint]() + - - return getHighestRatio(Array(previousDays)[0..