Update public.logbook_update_geojson_fn, expose more data

Update public.logbook_metrics_dwithin_fn, expend large zone for invalid logbook, increase to 15metres.
Disable public.logbook_update_gpx_fn, back to using api.export_logbook_gpx_fn.
Add public.get_app_url_fn, allow limited access to the ap url settings.
This commit is contained in:
xbgmsharp
2023-10-25 09:27:48 +02:00
parent f3168542fd
commit 3fed9e0b6a

View File

@@ -33,14 +33,14 @@ CREATE OR REPLACE FUNCTION public.logbook_metrics_dwithin_fn(
AND ST_DWithin( AND ST_DWithin(
Geography(ST_MakePoint(m.longitude, m.latitude)), Geography(ST_MakePoint(m.longitude, m.latitude)),
Geography(ST_MakePoint(lgn, lat)), Geography(ST_MakePoint(lgn, lat)),
10 15
); );
END; END;
$logbook_metrics_dwithin$ LANGUAGE plpgsql; $logbook_metrics_dwithin$ LANGUAGE plpgsql;
-- Description -- Description
COMMENT ON FUNCTION COMMENT ON FUNCTION
public.logbook_metrics_dwithin_fn public.logbook_metrics_dwithin_fn
IS 'Check if all entries for a logbook are in stationary movement with 10 meters'; IS 'Check if all entries for a logbook are in stationary movement with 15 meters';
-- Update a logbook with avg data -- Update a logbook with avg data
-- TODO using timescale function -- TODO using timescale function
@@ -121,18 +121,18 @@ CREATE FUNCTION public.logbook_update_geojson_fn(IN _id integer, IN _start text,
SELECT SELECT
ST_AsGeoJSON(log.*) into log_geojson ST_AsGeoJSON(log.*) into log_geojson
FROM FROM
( select ( SELECT
id,name, id,name,
distance, distance,
duration, duration,
avg_speed, avg_speed,
avg_speed, max_speed,
max_wind_speed, max_wind_speed,
_from_time, _from_time,
notes, notes,
track_geom track_geom
FROM api.logbook FROM api.logbook
WHERE id = _id WHERE id = _id
) AS log; ) AS log;
-- GeoJson Feature Metrics point -- GeoJson Feature Metrics point
SELECT SELECT
@@ -213,7 +213,7 @@ AS $logbook_update_gpx$
xmlelement(name desc, log_rec.notes), xmlelement(name desc, log_rec.notes),
xmlelement(name link, xmlattributes(concat(app_settings->>'app.url', '/log/', log_rec.id) as href), xmlelement(name link, xmlattributes(concat(app_settings->>'app.url', '/log/', log_rec.id) as href),
xmlelement(name text, log_rec.name)), xmlelement(name text, log_rec.name)),
xmlelement(name extensions, xmlelement(name "postgsail:log_id", 1), xmlelement(name extensions, xmlelement(name "postgsail:log_id", log_rec.id),
xmlelement(name "postgsail:link", concat(app_settings->>'app.url','/log/', log_rec.id)), xmlelement(name "postgsail:link", concat(app_settings->>'app.url','/log/', log_rec.id)),
xmlelement(name "opencpn:guid", uuid_generate_v4()), xmlelement(name "opencpn:guid", uuid_generate_v4()),
xmlelement(name "opencpn:viz", '1'), xmlelement(name "opencpn:viz", '1'),
@@ -509,11 +509,11 @@ CREATE OR REPLACE FUNCTION process_logbook_queue_fn(IN _id integer) RETURNS void
WHERE id = logbook_rec.id; WHERE id = logbook_rec.id;
-- GPX field -- GPX field
gpx := logbook_update_gpx_fn(logbook_rec.id, logbook_rec._from_time::TEXT, logbook_rec._to_time::TEXT); --gpx := logbook_update_gpx_fn(logbook_rec.id, logbook_rec._from_time::TEXT, logbook_rec._to_time::TEXT);
UPDATE api.logbook --UPDATE api.logbook
SET -- SET
track_gpx = gpx -- track_gpx = gpx
WHERE id = logbook_rec.id; -- WHERE id = logbook_rec.id;
-- Prepare notification, gather user settings -- Prepare notification, gather user settings
SELECT json_build_object('logbook_name', log_name, 'logbook_link', logbook_rec.id) into log_settings; SELECT json_build_object('logbook_name', log_name, 'logbook_link', logbook_rec.id) into log_settings;
@@ -853,9 +853,9 @@ COMMENT ON FUNCTION
public.process_vessel_queue_fn public.process_vessel_queue_fn
IS 'process new vessel notification'; IS 'process new vessel notification';
-- Get user settings details from a log entry -- Get application settings details from a log entry
DROP FUNCTION IF EXISTS get_app_settings_fn; DROP FUNCTION IF EXISTS get_app_settings_fn;
CREATE OR REPLACE FUNCTION get_app_settings_fn (OUT app_settings jsonb) CREATE OR REPLACE FUNCTION get_app_settings_fn(OUT app_settings jsonb)
RETURNS jsonb RETURNS jsonb
AS $get_app_settings$ AS $get_app_settings$
DECLARE DECLARE
@@ -865,17 +865,37 @@ BEGIN
FROM FROM
public.app_settings public.app_settings
WHERE WHERE
name LIKE '%app.email%' name LIKE 'app.email%'
OR name LIKE '%app.pushover%' OR name LIKE 'app.pushover%'
OR name LIKE '%app.url' OR name LIKE 'app.url'
OR name LIKE '%app.telegram%'; OR name LIKE 'app.telegram%';
END; END;
$get_app_settings$ $get_app_settings$
LANGUAGE plpgsql; LANGUAGE plpgsql;
-- Description -- Description
COMMENT ON FUNCTION COMMENT ON FUNCTION
public.get_app_settings_fn public.get_app_settings_fn
IS 'get app settings details, email, pushover, telegram'; IS 'get application settings details, email, pushover, telegram';
DROP FUNCTION IF EXISTS get_app_url_fn;
CREATE OR REPLACE FUNCTION get_app_url_fn(OUT app_settings jsonb)
RETURNS jsonb
AS $get_app_url$
DECLARE
BEGIN
SELECT
jsonb_object_agg(name, value) INTO app_settings
FROM
public.app_settings
WHERE
name = 'app.url';
END;
$get_app_url$
LANGUAGE plpgsql security definer;
-- Description
COMMENT ON FUNCTION
public.get_app_url_fn
IS 'get application url security definer';
-- Send notifications -- Send notifications
DROP FUNCTION IF EXISTS send_notification_fn; DROP FUNCTION IF EXISTS send_notification_fn;
@@ -1397,7 +1417,7 @@ BEGIN
perform public.cron_process_new_moorage_fn(); perform public.cron_process_new_moorage_fn();
perform public.cron_process_monitor_offline_fn(); perform public.cron_process_monitor_offline_fn();
END END
$$ language plpgsql security definer; $$ language plpgsql;
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
-- Delete all data for a account by email and vessel_id -- Delete all data for a account by email and vessel_id
@@ -1417,14 +1437,14 @@ BEGIN
delete from auth.accounts a where email = _email; delete from auth.accounts a where email = _email;
RETURN True; RETURN True;
END END
$delete_account$ language plpgsql security definer; $delete_account$ language plpgsql;
-- Dump all data for a account by email and vessel_id -- Dump all data for a account by email and vessel_id
CREATE OR REPLACE FUNCTION public.dump_account_fn(IN _email TEXT, IN _vessel_id TEXT) RETURNS BOOLEAN CREATE OR REPLACE FUNCTION public.dump_account_fn(IN _email TEXT, IN _vessel_id TEXT) RETURNS BOOLEAN
AS $dump_account$ AS $dump_account$
BEGIN BEGIN
-- TODO use COPY but we can't all in one?
RETURN True; RETURN True;
-- TODO use COPY but we can't all in one?
select count(*) from api.metrics m where vessel_id = _vessel_id; select count(*) from api.metrics m where vessel_id = _vessel_id;
select * from api.metadata m where vessel_id = _vessel_id; select * from api.metadata m where vessel_id = _vessel_id;
select * from api.logbook l where vessel_id = _vessel_id; select * from api.logbook l where vessel_id = _vessel_id;
@@ -1433,19 +1453,18 @@ BEGIN
select * from auth.vessels v where vessel_id = _vessel_id; select * from auth.vessels v where vessel_id = _vessel_id;
select * from auth.accounts a where email = _email; select * from auth.accounts a where email = _email;
END END
$dump_account$ language plpgsql security definer; $dump_account$ language plpgsql;
CREATE OR REPLACE FUNCTION public.delete_vessel_fn(IN _vessel_id TEXT) RETURNS BOOLEAN CREATE OR REPLACE FUNCTION public.delete_vessel_fn(IN _vessel_id TEXT) RETURNS BOOLEAN
AS $delete_account$ AS $delete_vessel$
BEGIN BEGIN
RETURN True;
select count(*) from api.metrics m where vessel_id = _vessel_id; select count(*) from api.metrics m where vessel_id = _vessel_id;
delete from api.metrics m where vessel_id = _vessel_id; delete from api.metrics m where vessel_id = _vessel_id;
select * from api.metadata m where vessel_id = _vessel_id; select * from api.metadata m where vessel_id = _vessel_id;
delete from api.metadata m where vessel_id = _vessel_id;
delete from api.logbook l where vessel_id = _vessel_id; delete from api.logbook l where vessel_id = _vessel_id;
delete from api.moorages m where vessel_id = _vessel_id; delete from api.moorages m where vessel_id = _vessel_id;
delete from api.stays s where vessel_id = _vessel_id; delete from api.stays s where vessel_id = _vessel_id;
delete from api.metadata m where vessel_id = _vessel_id;
-- TODO remove the badges?
RETURN True;
END END
$delete_account$ language plpgsql security definer; $delete_vessel$ language plpgsql;