From 59209e81c865e5bd7a952167355296eef9f50d47 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 15 Nov 2023 11:11:49 +0200 Subject: [PATCH] Add: Use a 3 arg iso_time for GET iterators --- src/manage_pg.c | 28 ++++++++++++++++++++++++++++ src/manage_sql.c | 27 +++++++++++++++++++++++++++ src/manage_sql.h | 11 +++++++---- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index 6f2dd9500..f5f6c52da 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -538,6 +538,34 @@ manage_create_sql_functions () sql ("DROP FUNCTION IF EXISTS iso_time (seconds integer);"); + sql ("CREATE OR REPLACE FUNCTION iso_time (seconds bigint, user_zone text, user_offset interval)" + " RETURNS text AS $$" + " BEGIN" + " RETURN CASE" + " WHEN $1 IS NULL OR $1 = 0" + " THEN ''" + " WHEN user_zone IS NULL" + " OR EXTRACT (EPOCH FROM user_offset) = 0" + " THEN to_char (to_timestamp ($1) AT TIME ZONE 'UTC'," + " 'FMYYYY-MM-DD')" + " || to_char (to_timestamp ($1) AT TIME ZONE 'UTC'," + " 'FMTHH24:MI:SSZ')" + " ELSE to_char (to_timestamp ($1) AT TIME ZONE user_zone," + " 'FMYYYY-MM-DD')" + " || to_char (to_timestamp ($1) AT TIME ZONE user_zone," + " 'FMTHH24:MI:SS')" + " || CASE WHEN (extract (epoch FROM user_offset) > 0)" + " THEN '+' ELSE '' END" + " || to_char (extract (hours FROM user_offset)::integer," + " 'FM00')" + " || ':'" + " || to_char (abs (extract (minutes FROM user_offset)" + " ::integer)," + " 'FM00')" + " END;" + " END;" + "$$ LANGUAGE plpgsql;"); + sql ("CREATE OR REPLACE FUNCTION iso_time (seconds bigint, user_zone text)" " RETURNS text AS $$" " DECLARE" diff --git a/src/manage_sql.c b/src/manage_sql.c index 4dde2b803..1ad5b8294 100644 --- a/src/manage_sql.c +++ b/src/manage_sql.c @@ -4998,6 +4998,33 @@ init_get_iterator2_with (iterator_t* iterator, const char *type, with_clause = g_strdup_printf ("WITH %s", extra_with); } + + extra_with = " iterator_user_zone" + " AS (SELECT coalesce (NULLIF ((SELECT current_setting ('gvmd.tz_override'))," + " '')," + " (SELECT timezone FROM users" + " WHERE id = gvmd_user ()))" + " AS user_zone)," + " iterator_user_offset" + " AS (SELECT age (now () AT TIME ZONE (SELECT user_zone" + " FROM iterator_user_zone" + " LIMIT 1)," + " now () AT TIME ZONE 'UTC')" + " AS user_offset)"; + if (extra_with) + { + if (with_clause) + { + gchar *old_with; + + old_with = with_clause; + with_clause = g_strdup_printf ("%s, %s", old_with, extra_with); + g_free (old_with); + } + else + with_clause = g_strdup_printf ("WITH %s", extra_with); + } + g_free (owner_filter); array_free (permissions); diff --git a/src/manage_sql.h b/src/manage_sql.h index 6ea3ac24c..0a5a286ff 100644 --- a/src/manage_sql.h +++ b/src/manage_sql.h @@ -248,9 +248,12 @@ typedef struct * * @param[in] prefix Column prefix. */ +// FIX takes CPEs from 9s to 7s #define GET_ITERATOR_COLUMNS_STRING \ - "id, uuid, name, comment, iso_time (creation_time)," \ - " iso_time (modification_time), creation_time AS created," \ + "id, uuid, name, comment," \ + " iso_time (creation_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))," \ + " iso_time (modification_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))," \ + " creation_time AS created," \ " modification_time AS modified" /** @@ -263,8 +266,8 @@ typedef struct { prefix "uuid", NULL, KEYWORD_TYPE_STRING }, \ { prefix "name", NULL, KEYWORD_TYPE_STRING }, \ { prefix "comment", NULL, KEYWORD_TYPE_STRING }, \ - { " iso_time (" prefix "creation_time)", NULL, KEYWORD_TYPE_STRING }, \ - { " iso_time (" prefix "modification_time)", NULL, KEYWORD_TYPE_STRING }, \ + { " iso_time (" prefix "creation_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))", NULL, KEYWORD_TYPE_STRING }, \ + { " iso_time (" prefix "modification_time, (SELECT user_zone FROM iterator_user_zone LIMIT 1), (SELECT user_offset FROM iterator_user_offset LIMIT 1))", NULL, KEYWORD_TYPE_STRING }, \ { prefix "creation_time", "created", KEYWORD_TYPE_INTEGER }, \ { prefix "modification_time", "modified", KEYWORD_TYPE_INTEGER }