Add unts() function creation to cmd.sh
authorPeter Gervai <grin@grin.hu>
Fri, 04 Mar 2022 10:54:52 +0100
changeset 8 b92f5cceab78
parent 7 6aea45bf033b
child 9 c636ea2e8ee1
Add unts() function creation to cmd.sh
synapse/cmd.sh
--- a/synapse/cmd.sh	Wed Mar 02 14:50:37 2022 +0100
+++ b/synapse/cmd.sh	Fri Mar 04 10:54:52 2022 +0100
@@ -1,5 +1,5 @@
 #!/bin/bash
-#$Id: cmd.sh,v 728762bf2e91 2022/03/02 13:38:25 grin $
+#$Id: cmd.sh,v efd1721dba68 2022/03/04 09:54:35 grin $
 #
 #
 # This program queries PostgreSQL directly for various user statistics.
@@ -7,6 +7,9 @@
 #  without password entry.
 # Start without arguments for help.
 #
+# You need to run 'unts' the first time with write access or copy the
+# output into psql.
+#
 # (c)Peter Gervai, 2020-22
 # @grin:grin.hu
 # Licensed under GPLv3+ and CC_By-Sa-4.0-int
@@ -31,7 +34,9 @@
 	echo "        active1u                     - last 1 day users (unique results)"
 	echo "        lastreg [<n>]	           - last <n> (25) registrations"
 	echo "        room <room_id without !>     - room info"
-	echo "        evt <event_id w/o $>         - event info"
+	echo "        evt <event w/o $             - event info"
+	echo "        unts                         - create unts() function for human-readable"
+	echo "                                       timestamps; requires Write access or copy displayed sql."
 	exit
 fi
 
@@ -47,6 +52,24 @@
 }
 
 
+### create unts() func
+if [ "$MODE" = "unts" ]; then
+	echo "Creating unts() function; needs write access or you can simply copy the following code into psql."
+	echo " "
+	SQL='CREATE OR REPLACE FUNCTION public.unts(bigint)
+ RETURNS timestamp with time zone
+ LANGUAGE sql
+ IMMUTABLE STRICT
+AS $function$
+  SELECT TIMESTAMP WITH TIME ZONE ''epoch'' + $1/1000 * INTERVAL ''1 second''
+$function$'
+	echo "${SQL}"
+	echo " "
+	$CMD "${SQL}"
+	exit
+fi
+
+
 
 if [ "$MODE" = "user" ]; then
 	egy_arg 'user'