cumulative test trial
This commit is contained in:
@@ -3,11 +3,11 @@ import Vapor
|
||||
|
||||
|
||||
|
||||
struct CODDate {
|
||||
let month:Int
|
||||
let year:Int
|
||||
let day: Int
|
||||
}
|
||||
struct CODDate {
|
||||
let month:Int
|
||||
let year:Int
|
||||
let day: Int
|
||||
}
|
||||
|
||||
|
||||
struct StatsController: RouteCollection {
|
||||
@@ -70,27 +70,27 @@ struct StatsController: RouteCollection {
|
||||
func getStatsForDay(year:Int, month:Int, day:Int, req: Request) -> EventLoopFuture<Stats>{
|
||||
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfDay(day:day, month: month, year: year))
|
||||
.filter(\.$date <= getEndOfDay(day: day, month: month, year: year))
|
||||
.filter(\.$win == true )
|
||||
.count()
|
||||
let winCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfDay(day:day, month: month, year: year))
|
||||
.filter(\.$date <= getEndOfDay(day: day, month: month, year: year))
|
||||
.filter(\.$win == true )
|
||||
.count()
|
||||
|
||||
|
||||
let lossCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfDay(day:day, month: month, year: year))
|
||||
.filter(\.$date <= getEndOfDay(day: day, month: month, year: year))
|
||||
.filter(\.$win == false )
|
||||
.count()
|
||||
let lossCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfDay(day:day, month: month, year: year))
|
||||
.filter(\.$date <= getEndOfDay(day: day, month: month, year: year))
|
||||
.filter(\.$win == false )
|
||||
.count()
|
||||
|
||||
let combined = winCount.and(lossCount)
|
||||
let combined = winCount.and(lossCount)
|
||||
|
||||
return combined.map { (winCount, lossCount) -> (Stats) in
|
||||
return combined.map { (winCount, lossCount) -> (Stats) in
|
||||
|
||||
let ratio:Double = (Double(winCount) / Double(lossCount)).truncate(places: 2)
|
||||
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
|
||||
}
|
||||
}
|
||||
let ratio:Double = (Double(winCount) / Double(lossCount)).truncate(places: 2)
|
||||
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -134,36 +134,36 @@ struct StatsController: RouteCollection {
|
||||
|
||||
private func getEndOfMonth(month:Int, year:Int) -> Date {
|
||||
let calendar = Calendar.current
|
||||
var components = DateComponents()
|
||||
components.day = -1
|
||||
components.month = month + 1
|
||||
components.year = year
|
||||
components.hour = 23
|
||||
components.minute = 59
|
||||
return calendar.date(from: components)!
|
||||
var components = DateComponents()
|
||||
components.day = -1
|
||||
components.month = month + 1
|
||||
components.year = year
|
||||
components.hour = 23
|
||||
components.minute = 59
|
||||
return calendar.date(from: components)!
|
||||
}
|
||||
|
||||
|
||||
private func getStartOfDay(day:Int, month:Int, year:Int) -> Date {
|
||||
let calendar = Calendar.current
|
||||
var components = DateComponents()
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
components.hour = 0
|
||||
components.minute = 0
|
||||
return calendar.date(from: components)!
|
||||
var components = DateComponents()
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
components.hour = 0
|
||||
components.minute = 0
|
||||
return calendar.date(from: components)!
|
||||
}
|
||||
|
||||
private func getEndOfDay(day:Int, month:Int, year:Int) -> Date {
|
||||
let calendar = Calendar.current
|
||||
var components = DateComponents()
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
components.hour = 23
|
||||
components.minute = 59
|
||||
return calendar.date(from: components)!
|
||||
var components = DateComponents()
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
components.hour = 23
|
||||
components.minute = 59
|
||||
return calendar.date(from: components)!
|
||||
}
|
||||
|
||||
|
||||
@@ -230,13 +230,16 @@ struct StatsController: RouteCollection {
|
||||
|
||||
|
||||
return combined.map { (winCount, lossCount) -> (Stats) in
|
||||
let ratio:Double = (Double(winCount) / Double(lossCount)).truncate(places: 2)
|
||||
|
||||
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
|
||||
return Stats.init(winLoss: self.getRatio(num: Double(winCount), den: Double(lossCount)), totalWins: winCount, totalLosses: lossCount)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func getRatio( num:Double, den:Double) -> String {
|
||||
|
||||
return String((Double(num) / Double(den)).truncate(places: 2))
|
||||
|
||||
}
|
||||
func allDaily(req:Request) -> EventLoopFuture<AllDailyStats>{
|
||||
|
||||
var date = getStartDate()
|
||||
@@ -249,58 +252,71 @@ struct StatsController: RouteCollection {
|
||||
|
||||
} while (date < Date())
|
||||
|
||||
|
||||
func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
previousDays = previousDays.reversed()
|
||||
|
||||
|
||||
func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in
|
||||
var allDailyStats = allDailyStats
|
||||
allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats))
|
||||
return getDailyStats(remaining, allDailyStats:&allDailyStats, eventLoop: eventLoop)
|
||||
}
|
||||
return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in
|
||||
var allDailyStats = allDailyStats
|
||||
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(allDailyStats)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var stats:[DailyStats] = []
|
||||
let dailyStats = getDailyStats(previousDays[0..<previousDays.count], allDailyStats:&stats, eventLoop: req.eventLoop)
|
||||
|
||||
return dailyStats.map { (dailyStats) -> AllDailyStats in
|
||||
return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in
|
||||
if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 {
|
||||
return false
|
||||
let totalWins = allDailyStats.reduce(0) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalWins)
|
||||
}
|
||||
return true
|
||||
}))
|
||||
|
||||
let totalLosses = allDailyStats.reduce(0) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalLosses)
|
||||
}
|
||||
|
||||
allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses)))
|
||||
return getDailyStats(remaining, allDailyStats:&allDailyStats, eventLoop: eventLoop)
|
||||
}
|
||||
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(allDailyStats)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var stats:[DailyStats] = []
|
||||
let dailyStats = getDailyStats(previousDays[0..<previousDays.count], allDailyStats:&stats, eventLoop: req.eventLoop)
|
||||
|
||||
return dailyStats.map { (dailyStats) -> AllDailyStats in
|
||||
return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in
|
||||
|
||||
|
||||
if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}).reversed()
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// func allCumulativeStats(req:Request) -> EventLoopFuture<AllRollingStats> {
|
||||
//
|
||||
//
|
||||
// }
|
||||
// func allCumulativeStats(req:Request) -> EventLoopFuture<AllRollingStats> {
|
||||
//
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
private func getStartDate() -> Date {
|
||||
|
||||
let calendar = Calendar.current
|
||||
let calendar = Calendar.current
|
||||
|
||||
var components = DateComponents()
|
||||
components.day = 20
|
||||
components.month = 03
|
||||
components.year = 2020
|
||||
components.hour = 0
|
||||
components.minute = 0
|
||||
var components = DateComponents()
|
||||
components.day = 20
|
||||
components.month = 03
|
||||
components.year = 2020
|
||||
components.hour = 0
|
||||
components.minute = 0
|
||||
|
||||
return calendar.date(from: components)!
|
||||
return calendar.date(from: components)!
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,20 +62,20 @@ final class AllRollingStats: Content {
|
||||
var cumulativeStats:[DailyStats]
|
||||
}
|
||||
|
||||
|
||||
|
||||
final class DailyStats: Content {
|
||||
|
||||
var day: String
|
||||
var month:String
|
||||
var year: String
|
||||
var cumulativeRatio:String
|
||||
var stats:Stats
|
||||
|
||||
init(day:Int, month:Int, year:Int, stats:Stats) {
|
||||
init(day:Int, month:Int, year:Int, stats:Stats, cumulativeRatio:String) {
|
||||
self.day = String(day)
|
||||
self.month = Utilities.monthToString(month: month)
|
||||
self.year = String(year)
|
||||
self.stats = stats
|
||||
self.cumulativeRatio = cumulativeRatio
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user