include data points
This commit is contained in:
@@ -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<Stats>
|
||||
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..<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
|
||||
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
|
||||
|
||||
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..<previousDays.count], allDailyStats: &stats, highestRatio: &highestRatio, eventLoop: req.eventLoop)
|
||||
return getRatios(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, cumulativeWinLossRatios: &cumulativeWinLossRatios, eventLoop: req.eventLoop)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -11,15 +11,29 @@ import Foundation
|
||||
import Fluent
|
||||
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 {
|
||||
var overall:Stats
|
||||
var byMonth: [MonthStats]
|
||||
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.byMonth = byMonth
|
||||
self.highestWinLossRatio = highestWinLossRatio
|
||||
self.dataPoints = dataPoints
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user