Associate a position with timed events if possible.

When inserting or updating a timed event, a trigger
searches the real time events table for a position
close (within one minute) of the event time and
adds it to the event's metadata.
This commit is contained in:
D. Berge
2020-09-30 22:45:50 +02:00
parent 79a751393c
commit 21439fdd3e

View File

@@ -63,6 +63,38 @@ If the hash matches that of an existing entry, update the path of that entry to
If the path matches that of an existing entry, delete that entry (which cascades) and insert the new one.';
--
-- Name: assoc_tstamp(); Type: FUNCTION; Schema: _SURVEY__TEMPLATE_; Owner: postgres
--
CREATE FUNCTION _SURVEY__TEMPLATE_.assoc_tstamp() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
geom jsonb;
BEGIN
IF NOT (NEW.meta ? 'geometry') THEN
-- Get the geometry from the real time event
-- closest in time and not more than one minute
-- away.
SELECT geometry::jsonb
INTO geom
FROM real_time_inputs rti
WHERE rti.tstamp <-> NEW.tstamp < interval '1 minute'
ORDER BY rti.tstamp <-> NEW.tstamp
LIMIT 1;
IF geom IS NOT NULL THEN
NEW.meta := jsonb_set(NEW.meta, '{geometry}', geom);
END IF;
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION _SURVEY__TEMPLATE_.assoc_tstamp() OWNER TO postgres;
--
-- Name: binning_parameters(); Type: FUNCTION; Schema: _SURVEY__TEMPLATE_; Owner: postgres
--
@@ -1489,6 +1521,13 @@ CREATE TRIGGER events_tg AFTER INSERT OR DELETE OR UPDATE ON _SURVEY__TEMPLATE_.
CREATE TRIGGER events_timed_seq_match_tg AFTER INSERT OR UPDATE ON _SURVEY__TEMPLATE_.events_timed FOR EACH ROW EXECUTE FUNCTION _SURVEY__TEMPLATE_.events_timed_seq_match();
--
-- Name: events_timed events_timed_tg; Type: TRIGGER; Schema: _SURVEY__TEMPLATE_; Owner: postgres
--
CREATE TRIGGER events_timed_tg BEFORE INSERT OR UPDATE ON _SURVEY__TEMPLATE_.events_timed FOR EACH ROW EXECUTE FUNCTION _SURVEY__TEMPLATE_.assoc_tstamp();
--
-- Name: events_seq_labels events_seq_labels_id_fkey; Type: FK CONSTRAINT; Schema: _SURVEY__TEMPLATE_; Owner: postgres
--