speed update, new table for cached stats
This commit is contained in:
56
Sources/App/db/DBHelpers.swift
Normal file
56
Sources/App/db/DBHelpers.swift
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// DBHelpers.swift
|
||||
// App
|
||||
//
|
||||
// Created by Michael Simard on 8/4/21.
|
||||
//
|
||||
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
class DBHelpers {
|
||||
|
||||
static func relcalulateRecords(db:Database, completion:(_ message:String)->()){
|
||||
|
||||
let group = DispatchGroup()
|
||||
_ = try? StatsController().forceCalculatedStats(db: db).map({ statsDict in
|
||||
|
||||
|
||||
|
||||
for key in statsDict.keys {
|
||||
print (key)
|
||||
group.enter()
|
||||
}
|
||||
|
||||
for key in statsDict.keys {
|
||||
|
||||
let s:EventLoopFuture<Void> = WinLossRecords.query(on: db).filter(\.$codTrackerId == key).first()
|
||||
.unwrap(or: Abort(.notFound))
|
||||
.flatMap {
|
||||
|
||||
$0.update(statistics: WinLossRecords(id: $0.id, title: $0.title, wins: statsDict[key]?.totalWins ?? 0, losses: statsDict[key]?.totalLosses ?? 0, codTrackerId: $0.codTrackerId))
|
||||
|
||||
return $0.save(on: db)
|
||||
}
|
||||
|
||||
|
||||
s.whenComplete { result in
|
||||
group.leave()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
})
|
||||
.wait()
|
||||
|
||||
|
||||
group.wait()
|
||||
|
||||
completion("complete")
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user