tier times working now

This commit is contained in:
Joachim 2018-09-13 21:31:55 +02:00
parent 9fdb66e92b
commit 738977800d

View File

@ -30,26 +30,40 @@ class RouteDefiner(router: Router, vertx: Vertx, postgreSQLClient: AsyncSQLClien
* Get the tier-overview times.
*/
router.get("/championship/:idchampionship/tier/:tier/times").handler { rc ->
val query = "with rankedtimes as (with penalisedtimes as (select idrace,\n" +
" laptime + interval '1 second' *\n" +
" (select coalesce(sum(penaltyseconds), 0)\n" +
" from comment\n" +
" where idrace = valid_laptimes.idrace)as penalisedlaptime\n" +
" from valid_laptimes),\n" +
" summedtimes as (select idrace, idteam, poule, sum(penalisedlaptime) laptimesum\n" +
" from penalisedtimes\n" +
" inner join race using (idrace)\n" +
" where tier = ?\n" +
" and race.idchampionship = ?\n" +
" and publishresults\n" +
" group by idrace, idteam, poule\n" +
" having count(*) > 1)\n" +
" select *, ROW_NUMBER() OVER (PARTITION BY idteam\n" +
" ORDER BY laptimesum) rownr\n" +
" from summedtimes)\n" +
"select idteam, team.name, poule, extract(epoch from avg(laptimesum)) result from rankedtimes inner join team using (idteam) where rownr < 3 " +
"group by idteam, poule, team.name order by 4"
rc.end(postgreSQLClient.rxQueryWithParams(query, JsonArray(
val query2 = """with racetimes as (select idrace, sum(laptime)+ interval '1 second' *
coalesce((select sum(penaltyseconds) from comment where idrace = valid_laptimes.idrace),0) penalisedtime
from valid_laptimes inner join race using (idrace) where publishresults group by idrace),
selectedtimes as (select *, ROW_NUMBER() OVER (PARTITION BY idteam, idchampionship, tier, poule
ORDER BY penalisedtime) rownr from racetimes inner join race using (idrace) where penalisedtime is not null order by idteam, rownr),
finaltimes as (
select idchampionship, tier, poule, idteam, avg(penalisedtime) result
from selectedtimes
where rownr < 3
group by idchampionship, tier, poule, idteam
having count(*) = 2
)
select idteam, name, poule, extract(epoch from result) result from finaltimes inner join team using (idteam) where tier = ? and finaltimes.idchampionship = ? order by 4
"""
// val query = "with rankedtimes as (with penalisedtimes as (select idrace,\n" +
// " laptime + interval '1 second' *\n" +
// " (select coalesce(sum(penaltyseconds), 0)\n" +
// " from comment\n" +
// " where idrace = valid_laptimes.idrace)as penalisedlaptime\n" +
// " from valid_laptimes),\n" +
// " summedtimes as (select idrace, idteam, poule, sum(penalisedlaptime) laptimesum\n" +
// " from penalisedtimes\n" +
// " inner join race using (idrace)\n" +
// " where tier = ?\n" +
// " and race.idchampionship = ?\n" +
// " and publishresults\n" +
// " group by idrace, idteam, poule\n" +
// " having count(*) > 1)\n" +
// " select *, ROW_NUMBER() OVER (PARTITION BY idteam\n" +
// " ORDER BY laptimesum) rownr\n" +
// " from summedtimes)\n" +
// "select idteam, team.name, poule, extract(epoch from avg(laptimesum)) result from rankedtimes inner join team using (idteam) where rownr < 3 " +
// "group by idteam, poule, team.name order by 4"
rc.end(postgreSQLClient.rxQueryWithParams(query2, JsonArray(
rc.pathParam("tier").toInt(),
rc.pathParam("idchampionship").toInt()
)).map { it.rows })