Adds migrations 006/007/008 that wrap tracksolid.v_trips_enriched (introduced upstream in tracksolid_timescale_grafana_prod migration 09) and DROP+CREATE the trips_for_day / trips_for_range RPCs to return the new metadata columns: vehicle_plate, start_address, end_address, driving_time_s, idle_time_s, fuel_consumed_l, and daily_seq. Path/timestamps logic is unchanged — position_history is still scanned to produce the per-vertex timestamps_rel that drive deck.gl TripsLayer animation. route_geom can't replace this since it carries no per-vertex time, but the GIST index on it is available for future area filters. Day filter switches from start_time::date to v.trip_date_eat (the Africa/Nairobi local date already computed by v_trips_enriched), which fixes the latent bug where trips spanning midnight UTC could be misbucketed. Frontend Trip type still receives all original fields in compatible positions; the new fields are additive and ignored by the existing TripsLayer code. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
84 lines
2.5 KiB
PL/PgSQL
84 lines
2.5 KiB
PL/PgSQL
-- 008_trips_for_range_v2.sql
|
|
-- Multi-day variant of 007_trips_for_day_v2. Same enriched return shape;
|
|
-- 14-day cap retained.
|
|
|
|
DROP FUNCTION IF EXISTS public.trips_for_range(date, date, text[]);
|
|
|
|
CREATE FUNCTION public.trips_for_range(
|
|
p_start date,
|
|
p_end date,
|
|
p_cost_centres text[] DEFAULT NULL
|
|
) RETURNS TABLE (
|
|
trip_id bigint,
|
|
imei text,
|
|
vehicle_name text,
|
|
vehicle_number text,
|
|
cost_centre text,
|
|
start_time timestamptz,
|
|
end_time timestamptz,
|
|
distance_km numeric,
|
|
vehicle_plate text,
|
|
start_address text,
|
|
end_address text,
|
|
driving_time_s integer,
|
|
idle_time_s integer,
|
|
fuel_consumed_l numeric,
|
|
daily_seq bigint,
|
|
path_geojson json,
|
|
timestamps_rel int[]
|
|
)
|
|
LANGUAGE plpgsql STABLE
|
|
SECURITY DEFINER
|
|
SET search_path = public, tracksolid
|
|
AS $$
|
|
BEGIN
|
|
IF p_start IS NULL OR p_end IS NULL OR p_end < p_start THEN
|
|
RAISE EXCEPTION 'Invalid date range: p_start=%, p_end=%', p_start, p_end;
|
|
END IF;
|
|
IF (p_end - p_start) > 13 THEN
|
|
RAISE EXCEPTION 'Range too large: max 14 days, got %', (p_end - p_start + 1);
|
|
END IF;
|
|
|
|
RETURN QUERY
|
|
WITH range_trips AS (
|
|
SELECT v.*
|
|
FROM public.trips_viz_v1 v
|
|
WHERE v.trip_date_eat BETWEEN p_start AND p_end
|
|
AND (p_cost_centres IS NULL OR v.cost_centre = ANY(p_cost_centres))
|
|
)
|
|
SELECT
|
|
rt.trip_id,
|
|
rt.imei,
|
|
rt.vehicle_name,
|
|
rt.vehicle_number,
|
|
rt.cost_centre,
|
|
rt.start_time,
|
|
rt.end_time,
|
|
rt.distance_km,
|
|
rt.vehicle_plate,
|
|
rt.start_address,
|
|
rt.end_address,
|
|
rt.driving_time_s,
|
|
rt.idle_time_s,
|
|
rt.fuel_consumed_l,
|
|
rt.daily_seq,
|
|
ST_AsGeoJSON(ST_MakeLine(ph.geom ORDER BY ph.gps_time))::json AS path_geojson,
|
|
array_agg(
|
|
EXTRACT(EPOCH FROM ph.gps_time - rt.start_time)::int
|
|
ORDER BY ph.gps_time
|
|
) AS timestamps_rel
|
|
FROM range_trips rt
|
|
JOIN tracksolid.position_history ph
|
|
ON ph.imei = rt.imei
|
|
AND ph.gps_time BETWEEN rt.start_time AND rt.end_time
|
|
AND ph.geom IS NOT NULL
|
|
GROUP BY rt.trip_id, rt.imei, rt.vehicle_name, rt.vehicle_number,
|
|
rt.cost_centre, rt.start_time, rt.end_time, rt.distance_km,
|
|
rt.vehicle_plate, rt.start_address, rt.end_address,
|
|
rt.driving_time_s, rt.idle_time_s, rt.fuel_consumed_l,
|
|
rt.daily_seq
|
|
HAVING count(ph.geom) >= 2;
|
|
END;
|
|
$$;
|
|
|
|
GRANT EXECUTE ON FUNCTION public.trips_for_range(date, date, text[]) TO viz_anon;
|