Skip to content

Commit

Permalink
Add substitute period validity range to schedule calculations
Browse files Browse the repository at this point in the history
This was two birds with one stone, first of all the initial vsf.validity_start and vsf.validity_end were
wrong, and caused the substitute operating day to show the validity of the target day instead
of the superseded_date. But also now we have the substitute operating period  implemented
and those can last multiple days and that is the real validity range.
 Also added the used internal_utils functions to calculate the begin and end date here.

Resolves HSLdevcom/jore4#1390
  • Loading branch information
HenrikHartiala committed Aug 17, 2023
1 parent a3c289f commit e7004b6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ DROP SCHEMA vehicle_service;
DROP SCHEMA vehicle_journey;
DROP SCHEMA passing_times;
DROP SCHEMA return_value;
DROP SCHEMA internal_service_calendar;
27 changes: 16 additions & 11 deletions migrations/generic/timetables/1000000000000_R_before_migrate/up.sql
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
-- create schemas if they don't yet exist, because the drop function below reference them
CREATE SCHEMA IF NOT EXISTS route;
COMMENT ON SCHEMA route
COMMENT ON SCHEMA route
IS 'The route model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=2:1:3:416';

CREATE SCHEMA IF NOT EXISTS journey_pattern;
COMMENT ON SCHEMA journey_pattern
COMMENT ON SCHEMA journey_pattern
IS 'The journey pattern model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=2:3:1:683 ';

CREATE SCHEMA IF NOT EXISTS service_pattern;
COMMENT ON SCHEMA service_pattern
COMMENT ON SCHEMA service_pattern
IS 'The service pattern model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=2:3:4:723 ';

CREATE SCHEMA IF NOT EXISTS service_calendar;
COMMENT ON SCHEMA service_calendar
COMMENT ON SCHEMA service_calendar
IS 'The service calendar model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=1:6:3:294 ';

CREATE SCHEMA IF NOT EXISTS vehicle_schedule;
COMMENT ON SCHEMA vehicle_schedule
COMMENT ON SCHEMA vehicle_schedule
IS 'The vehicle schedule frame adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:7:2:993 ';

CREATE SCHEMA IF NOT EXISTS vehicle_service;
COMMENT ON SCHEMA vehicle_service
COMMENT ON SCHEMA vehicle_service
IS 'The vehicle service model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:5:947 ';

CREATE SCHEMA IF NOT EXISTS vehicle_journey;
COMMENT ON SCHEMA vehicle_journey
COMMENT ON SCHEMA vehicle_journey
IS 'The vehicle journey model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:1:1:824 ';

CREATE SCHEMA IF NOT EXISTS passing_times;
COMMENT ON SCHEMA passing_times
COMMENT ON SCHEMA passing_times
IS 'The passing times model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:4:939 ';

CREATE SCHEMA IF NOT EXISTS internal_utils;
COMMENT ON SCHEMA internal_utils
IS 'General utilities';
COMMENT ON SCHEMA internal_utils
IS 'Internal general utilities. Functions in this schema are not exposed to GraphQL API, hence the prefix.';

CREATE SCHEMA IF NOT EXISTS return_value;
COMMENT ON SCHEMA return_value
COMMENT ON SCHEMA return_value
IS 'This schema is used for all the SQL functions that need to have a table as return value. Nothing is stored in the tables in this schema.';

CREATE SCHEMA IF NOT EXISTS internal_service_calendar;
COMMENT ON SCHEMA internal_service_calendar
IS 'This schema is used for the internal SQL functions that are related to service_calendar schema. These internal functions are
not exposed to GraphQL API, hence the prefix.';

-- drop all triggers in jore4 schemas
-- note: information_schema.triggers is missing TRUNCATE triggers
-- note2: pg_catalog.pg_triggers contains the TRUNCATE triggers but many other things too
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
CREATE OR REPLACE FUNCTION internal_service_calendar.get_substitute_operating_period_begin_date(substitute_operating_period_uuid uuid)
RETURNS date
LANGUAGE SQL
STABLE
AS $$
SELECT MIN(superseded_date)
FROM service_calendar.substitute_operating_day_by_line_type
WHERE substitute_operating_period_id = substitute_operating_period_uuid;
$$
;

COMMENT ON FUNCTION internal_service_calendar.get_substitute_operating_period_begin_date(substitute_operating_period_uuid uuid)
IS 'Returns the begin date of a substitute operating period that consists of substitute_operating_day_by_line_types
(superseded_date).
';

CREATE OR REPLACE FUNCTION internal_service_calendar.get_substitute_operating_period_end_date(substitute_operating_period_uuid uuid)
RETURNS date
LANGUAGE SQL
STABLE
AS $$
SELECT MAX(superseded_date)
FROM service_calendar.substitute_operating_day_by_line_type
WHERE substitute_operating_period_id = substitute_operating_period_uuid;
$$
;

COMMENT ON FUNCTION internal_service_calendar.get_substitute_operating_period_end_date(substitute_operating_period_uuid uuid)
IS 'Returns the last date of a substitute operating period that consists of substitute_operating_day_by_line_types
(superseded_date).
';

CREATE OR REPLACE FUNCTION vehicle_journey.get_vehicle_schedules_on_date(journey_pattern_uuid uuid, observation_date date)
RETURNS SETOF return_value.vehicle_schedule
LANGUAGE SQL
Expand All @@ -6,8 +38,8 @@ AS $$
WITH substitute_operating_day_by_line_type_vehicle_schedules AS
(
SELECT DISTINCT vj.vehicle_journey_id,
vsf.validity_start,
vsf.validity_end,
internal_service_calendar.get_substitute_operating_period_begin_date(sodblt.substitute_operating_period_id),
internal_service_calendar.get_substitute_operating_period_end_date(sodblt.substitute_operating_period_id),
internal_utils.const_timetables_priority_substitute_by_line_type(), -- priority
( -- calculate what is the correct day type id for the superseded date
SELECT day_type_id
Expand Down Expand Up @@ -113,3 +145,4 @@ the time range does not return any vehicle journeys (or if the substitute_operat
a row which does not have vehicle_journey set. This is an indicator that the day type does not have operation on the given day. This is of course overruled
by special priority schedules, it being a higher priority.
';

18 changes: 17 additions & 1 deletion migrations/timetablesdb-dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ COMMENT ON EXTENSION btree_gist IS 'support for indexing common datatypes in GiS

COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';

--
-- Name: SCHEMA internal_service_calendar; Type: COMMENT; Schema: -; Owner: dbhasura
--

COMMENT ON SCHEMA internal_service_calendar IS 'This schema is used for the internal SQL functions that are related to service_calendar schema. These internal functions are
not exposed to GraphQL API, hence the prefix.';

--
-- Name: SCHEMA internal_utils; Type: COMMENT; Schema: -; Owner: dbhasura
--

COMMENT ON SCHEMA internal_utils IS 'General utilities';
COMMENT ON SCHEMA internal_utils IS 'Internal general utilities. Functions in this schema are not exposed to GraphQL API, hence the prefix.';

--
-- Name: SCHEMA journey_pattern; Type: COMMENT; Schema: -; Owner: dbhasura
Expand Down Expand Up @@ -1663,6 +1670,15 @@ CREATE INDEX idx_vehicle_service_vehicle_schedule_frame ON vehicle_service.vehic

CREATE UNIQUE INDEX vehicle_type_label_idx ON vehicle_type.vehicle_type USING btree (label);

--
-- Name: internal_service_calendar; Type: SCHEMA; Schema: -; Owner: dbhasura
--

CREATE SCHEMA internal_service_calendar;


ALTER SCHEMA internal_service_calendar OWNER TO dbhasura;

--
-- Name: internal_utils; Type: SCHEMA; Schema: -; Owner: dbhasura
--
Expand Down

0 comments on commit e7004b6

Please sign in to comment.