Skip to content

Commit

Permalink
slapper query change
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Apr 4, 2024
1 parent 779cb94 commit 521dc8f
Showing 1 changed file with 57 additions and 57 deletions.
114 changes: 57 additions & 57 deletions src/main/java/telraam/logic/lapper/slapper/Slapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,77 +54,77 @@ private void calculateLaps() {
this.jdbi.useHandle(handle -> handle.execute(
"""
WITH switchovers AS (
SELECT teamid AS team_id,
newbatonid,
timestamp AS start_timestamp,
COALESCE(
LEAD(timestamp) OVER (PARTITION BY teamid ORDER BY timestamp),
timestamp + INTERVAL '6 MONTHS'
) AS next_baton_switch
FROM batonswitchover
SELECT teamid AS team_id,
newbatonid,
timestamp AS start_timestamp,
COALESCE(
LEAD(timestamp) OVER (PARTITION BY teamid ORDER BY timestamp),
timestamp + INTERVAL '1 MONTH'
) AS next_baton_switch
FROM batonswitchover
),
team_detections AS (
SELECT MIN(station_id) AS station_id,
MAX(rssi) as rssi,
date_trunc('second', timestamp) AS timestamp_seconds,
team_id
FROM detection d
LEFT JOIN switchovers s ON d.baton_id = s.newbatonid
AND d.timestamp BETWEEN s.start_timestamp AND s.next_baton_switch
WHERE station_id NOT BETWEEN 3 AND 5
AND rssi > -85
AND team_id IS NOT NULL
GROUP BY date_trunc('second', timestamp), team_id
SELECT MIN(station_id) AS station_id,
MAX(rssi) as rssi,
date_trunc('second', timestamp) AS timestamp_seconds,
team_id
FROM detection d
LEFT JOIN switchovers s ON d.baton_id = s.newbatonid
AND d.timestamp BETWEEN s.start_timestamp AND s.next_baton_switch
WHERE station_id NOT BETWEEN 3 AND 5
AND rssi > -84
AND team_id IS NOT NULL
GROUP BY date_trunc('second', timestamp), team_id
),
no_duplicates AS (
SELECT team_id,
timestamp_seconds,
FIRST_VALUE(timestamp_seconds) OVER (
PARTITION BY team_id
ORDER BY timestamp_seconds
) AS first_timestamp_seconds
FROM (
SELECT *,
LAG(station_id) OVER (
PARTITION BY team_id
ORDER BY timestamp_seconds
) AS prev_station_id
FROM team_detections
) AS previous
WHERE station_id - prev_station_id <= -5
start_times AS (
SELECT DISTINCT ON (team_id) team_id,
timestamp_seconds AS start_seconds
FROM team_detections
WHERE station_id BETWEEN 2 AND 3
ORDER BY team_id, timestamp_seconds
),
new_laps AS (
SELECT team_id,
timestamp_seconds
FROM no_duplicates n_d
WHERE timestamp_seconds > first_timestamp_seconds
SELECT previous.team_id,
timestamp_seconds
FROM (
SELECT *,
LAG(station_id) OVER (
PARTITION BY team_id
ORDER BY timestamp_seconds
) AS prev_station_id
FROM team_detections
) AS previous
LEFT JOIN start_times s_t
ON previous.team_id = s_t.team_id
WHERE station_id - prev_station_id < -4
AND timestamp_seconds > start_seconds
),
cst_source_id AS (
SELECT COALESCE(id, -1) AS source_id
FROM lap_source
WHERE name ILIKE 'slapper'
SELECT COALESCE(id, -1) AS source_id
FROM lap_source
WHERE name ILIKE 'slapper'
),
deletions AS (
DELETE FROM lap l
WHERE lap_source_id = (SELECT source_id FROM cst_source_id)
AND NOT EXISTS (
SELECT 1
FROM new_laps n_l
WHERE l.team_id = n_l.team_id
AND l.timestamp = n_l.timestamp_seconds
)
DELETE FROM lap l
WHERE lap_source_id = (SELECT source_id FROM cst_source_id)
AND NOT EXISTS (
SELECT 1
FROM new_laps n_l
WHERE l.team_id = n_l.team_id
AND l.timestamp = n_l.timestamp_seconds
)
)
INSERT INTO lap (team_id, timestamp, lap_source_id)
SELECT team_id,
timestamp_seconds,
source_id
timestamp_seconds,
source_id
FROM new_laps n_l, cst_source_id
WHERE NOT EXISTS (
SELECT 1
FROM lap l, cst_source_id
WHERE l.lap_source_id = source_id
AND l.team_id = n_l.team_id
AND l.timestamp = n_l.timestamp_seconds
SELECT 1
FROM lap l, cst_source_id
WHERE l.lap_source_id = source_id
AND l.team_id = n_l.team_id
AND l.timestamp = n_l.timestamp_seconds
)
"""
)
Expand Down

0 comments on commit 521dc8f

Please sign in to comment.