add all api to sort by month
This commit is contained in:
@@ -6,74 +6,246 @@ import Vapor
|
||||
|
||||
|
||||
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)
|
||||
|
||||
//beaconsRoute.get("overallStats", use: index)
|
||||
//beaconsRoute.post("", use: create)
|
||||
// beaconsRoute.delete(":beaconID", use: delete)
|
||||
|
||||
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("march", use: getMarchStats)
|
||||
statsRoute.get("all", use: all)
|
||||
|
||||
//beaconsRoute.get("overallStats", use: index)
|
||||
//beaconsRoute.post("", use: create)
|
||||
// beaconsRoute.delete(":beaconID", use: delete)
|
||||
|
||||
}
|
||||
|
||||
func index(req: Request) throws -> EventLoopFuture<[Match]> {
|
||||
return Match.query(on: req.db).all()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func totalWins(req: Request) throws -> EventLoopFuture<Int> {
|
||||
return Match.query(on: req.db)
|
||||
.filter(\.$win == true)
|
||||
.count()
|
||||
.filter(\.$win == true)
|
||||
.count()
|
||||
}
|
||||
|
||||
|
||||
func totalLosses(req: Request) throws -> EventLoopFuture<Int> {
|
||||
return Match.query(on: req.db)
|
||||
.filter(\.$win == false)
|
||||
.count()
|
||||
}
|
||||
return Match.query(on: req.db)
|
||||
.filter(\.$win == false)
|
||||
.count()
|
||||
}
|
||||
|
||||
func getMarchStats(req:Request) throws -> EventLoopFuture<Stats> {
|
||||
return getStatsByMonth(year: 2020, month: 03, req: req)
|
||||
}
|
||||
|
||||
func overall(req: Request) throws -> EventLoopFuture<Stats> {
|
||||
let lossCount = Match.query(on: req.db)
|
||||
.filter(\.$win == false)
|
||||
.count()
|
||||
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
.filter(\.$win == true )
|
||||
.count()
|
||||
|
||||
func getStatsByMonth(year:Int, month:Int, req: Request) -> EventLoopFuture<Stats>{
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfMonth(month: month, year: year))
|
||||
.filter(\.$date <= getEndOfMonth(month: month, year: year))
|
||||
.filter(\.$win == true )
|
||||
.count()
|
||||
|
||||
|
||||
let lossCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfMonth(month: month, year: year))
|
||||
.filter(\.$date <= getEndOfMonth(month: month, year: year))
|
||||
.filter(\.$win == false )
|
||||
.count()
|
||||
|
||||
|
||||
let combined = winCount.and(lossCount)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func statsForRecent(numberGames:Int, req:Request) -> EventLoopFuture<Stats> {
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
.sort(\.$date)
|
||||
.range(lower: 0, upper: numberGames)
|
||||
.filter(\.$win == true )
|
||||
.count()
|
||||
|
||||
|
||||
let lossCount = Match.query(on: req.db)
|
||||
.sort(\.$date)
|
||||
.range(lower: 0, upper: numberGames)
|
||||
.filter(\.$win == false )
|
||||
.count()
|
||||
|
||||
let combined = winCount.and(lossCount)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private func getStartOfMonth(month:Int, year:Int) -> Date {
|
||||
return Date(year: year, month: month, day: 0, hour: 0, minute: 0, second: 0, nanosecond: 0, region: .current)
|
||||
}
|
||||
|
||||
private func getEndOfMonth(month:Int, year:Int) -> Date {
|
||||
return Date(year: year, month: month + 1, day: -1, hour: 0, minute: 0, second: 0, nanosecond: 0, region: .ISO)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// private func getAccumualtiveWLByDay(req:Request) {
|
||||
//
|
||||
// let winLossRatio:[String:Double] = [:]
|
||||
//
|
||||
// let matches = Match.query(on:req.db)
|
||||
// .sort(\.$date)
|
||||
// .all()
|
||||
//
|
||||
// var ratio = 0.0
|
||||
// var cumulativeWLRatio = req.eventLoop.future(ratio)
|
||||
//
|
||||
// matches.map { (matches) -> [String:Double] in
|
||||
//
|
||||
// let winLossRatio2:[String:Double] = [:]
|
||||
//
|
||||
// matches.reduce(Dictionary<String:Double>()) { (partialRatio, match) -> [String:Double] in
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
func all(req: Request) throws -> EventLoopFuture<AllStats> {
|
||||
|
||||
|
||||
struct MonthYear {
|
||||
let month:Int
|
||||
let year:Int
|
||||
}
|
||||
|
||||
var date = Date(year: 2020, month: 03, day: 01, hour: 0, minute: 0)
|
||||
var previousMonths:[MonthYear] = []
|
||||
|
||||
repeat {
|
||||
//let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
|
||||
previousMonths.append(MonthYear(month: date.month, year: date.year))
|
||||
date = Calendar.current.date(byAdding: .month, value: 1, to: date)!.date
|
||||
} while (date.month != (Date().month + 1) || date.year != Date().year)
|
||||
|
||||
|
||||
|
||||
func getMonthStats (_ remaining: ArraySlice<MonthYear>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
|
||||
|
||||
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
|
||||
return getStatsByMonth(year: first.year, month: first.month, req: req).flatMap { [remaining, allMonthlyStats] (stats) -> EventLoopFuture<[MonthStats]> in
|
||||
print ("remaining \(remaining)")
|
||||
var allMonthlyStats = allMonthlyStats
|
||||
allMonthlyStats.append(MonthStats(month: first.month, year: first.year, stats:stats ))
|
||||
return getMonthStats(remaining, allMonthlyStats:&allMonthlyStats, eventLoop: eventLoop)
|
||||
}
|
||||
|
||||
} else {
|
||||
print ("rett")
|
||||
return req.eventLoop.makeSucceededFuture(allMonthlyStats)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var stats:[MonthStats] = []
|
||||
let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop)
|
||||
|
||||
return try overall(req: req).and(monthstats).map { (overall, monthlyStats) -> AllStats in
|
||||
return AllStats.init(overall: overall, byMonth: monthlyStats)
|
||||
}
|
||||
// return getMonthStats(previousMonths, eventLoop: req.eventLoop)
|
||||
|
||||
|
||||
// var date = Date(year: 2020, month: 03, day: 01, hour: 0, minute: 0)
|
||||
// var monthlyStats = [String:Stats]()
|
||||
// repeat {
|
||||
// let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
|
||||
// monthlyStats["\(date.month) \(date.year)"] = stats.map({ (newStats) -> (Stats) in
|
||||
// return Stats(winLoss: newStats.winLossRatio, totalWins: newStats.totalWins, totalLosses: newStats.totalLosses)
|
||||
// })
|
||||
// date = Calendar.current.date(byAdding: .month, value: 1, to: date)!.date
|
||||
// } while (date.month != Date().month || date.year != Date().year)
|
||||
////
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// let combined = try overall(req: req).and(req.eventLoop.future(monthlyStats))
|
||||
// return combined.map { (stats, dict) -> (AllStats) in
|
||||
// return AllStats(overall: stats, byMonth: monthlyStats )
|
||||
//
|
||||
// }
|
||||
|
||||
// let combined = try overall(req: req).and(
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// )
|
||||
}
|
||||
|
||||
|
||||
|
||||
func overall(req: Request) throws -> EventLoopFuture<Stats> {
|
||||
|
||||
let lossCount = Match.query(on: req.db)
|
||||
.filter(\.$win == false)
|
||||
.count()
|
||||
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
.filter(\.$win == true )
|
||||
.count()
|
||||
|
||||
|
||||
|
||||
let combined = winCount.and(lossCount)
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//
|
||||
|
||||
// func create(req: Request) throws -> EventLoopFuture<Beacon> {
|
||||
// let newBeacon = try req.content.decode(Beacon.Create.self)
|
||||
// let beacon = Beacon(id: UUID(), ownerId: nil)
|
||||
// return beacon.save(on: req.db).map { beacon }
|
||||
// }
|
||||
//
|
||||
// func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
|
||||
// return Beacon.find(req.parameters.get("beaconID"), on: req.db)
|
||||
// .unwrap(or: Abort(.notFound))
|
||||
// .flatMap { $0.delete(on: req.db) }
|
||||
// .transform(to: .ok)
|
||||
// }
|
||||
// func create(req: Request) throws -> EventLoopFuture<Beacon> {
|
||||
// let newBeacon = try req.content.decode(Beacon.Create.self)
|
||||
// let beacon = Beacon(id: UUID(), ownerId: nil)
|
||||
// return beacon.save(on: req.db).map { beacon }
|
||||
// }
|
||||
//
|
||||
// func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
|
||||
// return Beacon.find(req.parameters.get("beaconID"), on: req.db)
|
||||
// .unwrap(or: Abort(.notFound))
|
||||
// .flatMap { $0.delete(on: req.db) }
|
||||
// .transform(to: .ok)
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user