include data points

This commit is contained in:
Michael Simard
2020-06-16 16:51:19 -05:00
parent 40e23960f1
commit 72a04f8823
2 changed files with 41 additions and 21 deletions

View File

@@ -10,6 +10,8 @@ struct CODDate {
} }
struct StatsController: RouteCollection { struct StatsController: RouteCollection {
func boot(routes: RoutesBuilder) throws { func boot(routes: RoutesBuilder) throws {
let statsRoute = routes.grouped("cod-tracker","api", "stats") let statsRoute = routes.grouped("cod-tracker","api", "stats")
@@ -176,7 +178,6 @@ struct StatsController: RouteCollection {
var date = getStartDate() var date = getStartDate()
var previousMonths:[CODDate] = [] var previousMonths:[CODDate] = []
repeat { repeat {
//let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats> //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)) previousMonths.append(CODDate(month: date.month, year: date.year, day: 15))
@@ -205,13 +206,19 @@ struct StatsController: RouteCollection {
var stats:[MonthStats] = [] var stats:[MonthStats] = []
let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop) let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop)
let highestRatio = getHighestWinLossRatio(req: req) let cumulativeRatios = getCumulativeWinLossRatios(req: req)
return try overall(req: req).and(monthstats).and(highestRatio).map { arg -> 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<String> { func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> {
var previousDays = getDaysPlayed().reversed() let previousDays = getDaysPlayed().reversed()
func getHighestRatio (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], highestRatio: inout String, eventLoop: EventLoop) -> EventLoopFuture<String> { func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> {
var remaining = remaining var remaining = remaining
if let first = remaining.popLast() { if let first = remaining.popLast() {
return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, highestRatio] (stats) -> EventLoopFuture<String> 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 var allDailyStats = allDailyStats
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
@@ -328,25 +335,24 @@ struct StatsController: RouteCollection {
return total + Double(dailyStats.stats.totalLosses) return total + Double(dailyStats.stats.totalLosses)
} }
let ratio = self.getRatio(num: totalWins, den: totalLosses) var cumulativeWinLossRatios = cumulativeWinLossRatios
var highestRatio = highestRatio if !(stats.totalWins == 0 && stats.totalLosses == 0) {
if Double(ratio)! > Double(highestRatio)! { cumulativeWinLossRatios.append(DataPoint(x: "\(Utilities.monthToString(month: first.month)) \(first.day) \(first.year)", y: (totalWins/totalLosses).truncate(places: 2)))
highestRatio = ratio
} }
allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) 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 { } else {
return req.eventLoop.makeSucceededFuture(highestRatio) return req.eventLoop.makeSucceededFuture(cumulativeWinLossRatios)
} }
} }
var stats:[DailyStats] = [] var stats:[DailyStats] = []
var highestRatio = "0" var cumulativeWinLossRatios:[DataPoint] = [DataPoint]()
return getRatios(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, cumulativeWinLossRatios: &cumulativeWinLossRatios, eventLoop: req.eventLoop)
return getHighestRatio(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, highestRatio: &highestRatio, eventLoop: req.eventLoop)
} }

View File

@@ -11,16 +11,30 @@ import Foundation
import Fluent import Fluent
import Vapor import Vapor
struct DataPoint : Content {
var x:String
var y:Double
init(x:String, y:Double) {
self.x = x
self.y = y
}
}
final class AllStats: Content { final class AllStats: Content {
var overall:Stats var overall:Stats
var byMonth: [MonthStats] var byMonth: [MonthStats]
var highestWinLossRatio:String var highestWinLossRatio:String
var dataPoints:[DataPoint]
init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String) {
init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint]) {
self.overall = overall self.overall = overall
self.byMonth = byMonth self.byMonth = byMonth
self.highestWinLossRatio = highestWinLossRatio self.highestWinLossRatio = highestWinLossRatio
} self.dataPoints = dataPoints
}
} }