]> git.ekhem.eu.org Git - metrics.git/commitdiff
Add timezones to timestamps. main
authorJakub Czajka <jakub@ekhem.eu.org>
Tue, 2 Jan 2024 12:57:32 +0000 (13:57 +0100)
committerJakub Czajka <jakub@ekhem.eu.org>
Tue, 2 Jan 2024 13:16:10 +0000 (14:16 +0100)
create.sql
drop.sql
metrics.sh
sleep.sh

index a9acd2affaa3069661c6be847ad6dedce021036e..c301f94f4ec86177060d61e5363b143de44d4b84 100644 (file)
@@ -1,11 +1,18 @@
 /* Copyright (c) 2024 Jakub Czajka <jakub@ekhem.eu.org>
    License: GPL-3.0 or later. */
 
+CREATE TABLE IF NOT EXISTS time_zones (
+    id SERIAL PRIMARY KEY,
+    name VARCHAR(128) NOT NULL UNIQUE
+);
+
 CREATE TABLE IF NOT EXISTS sleep (
     id SERIAL PRIMARY KEY,
     date DATE DEFAULT CURRENT_DATE,
     start TIME(0) WITH TIME ZONE,
-    finish TIME(0) WITH TIME ZONE
+    start_time_zone_id INTEGER REFERENCES time_zones (id),
+    finish TIME(0) WITH TIME ZONE,
+    finish_time_zone_id INTEGER REFERENCES time_zones (id)
 );
 
 DO $$
@@ -15,15 +22,15 @@ BEGIN
         CREATE ROLE metrics_tracker LOGIN;
 
         GRANT SELECT
-        ON TABLE sleep
+        ON TABLE sleep, time_zones
         TO metrics_tracker;
 
         GRANT INSERT
-        ON TABLE sleep
+        ON TABLE sleep, time_zones
         TO metrics_tracker;
 
         GRANT USAGE, SELECT
-        ON SEQUENCE sleep_id_seq
+        ON SEQUENCE sleep_id_seq, time_zones_id_seq
         TO metrics_tracker;
 
         /* Execute for the current database. */
index 9d655d5a166e28f920202478d500ad8190da7eb5..998d597cf7204c02be1d499f2167d9b9edb84a77 100644 (file)
--- a/drop.sql
+++ b/drop.sql
@@ -6,15 +6,15 @@ BEGIN
     IF EXISTS (SELECT * FROM pg_user WHERE usename = 'metrics_tracker')
     THEN
         REVOKE SELECT
-        ON TABLE sleep
+        ON TABLE sleep, time_zones
         FROM metrics_tracker;
 
         REVOKE INSERT
-        ON TABLE sleep
+        ON TABLE sleep, time_zones
         FROM metrics_tracker;
 
         REVOKE USAGE, SELECT
-        ON SEQUENCE sleep_id_seq
+        ON SEQUENCE sleep_id_seq, time_zones_id_seq
         FROM metrics_tracker;
 
         EXECUTE
@@ -27,3 +27,4 @@ BEGIN
 END$$;
 
 DROP TABLE IF EXISTS sleep;
+DROP TABLE IF EXISTS time_zones;
index ee1fc0935c599072de4cdbf4d9421e9b2767dfaa..cc9677d4ab6b2b911d22852e0ba5b69b88b95c22 100644 (file)
@@ -4,6 +4,17 @@
 
 . /etc/environment
 
+query_db() {
+  /usr/bin/psql --user=payments_tracker --dbname="${PAYMENTS_DB}" \
+    --tuples-only --no-align --command="${1}"
+}
+
+as_options() {
+  xargs --replace={} echo "<option value='{}'>{}</option>"
+}
+
+time_zones=$(query_db "SELECT name FROM time_zones")
+
 PAGE="
 <!DOCTYPE html>
 <html>
@@ -27,9 +38,12 @@ PAGE="
       column-gap: 5px;
       display: flex;
     }
-    input[type=date] {
+    input {
       width: 100%;
     }
+    input[list] {
+      width: 30%;
+    }
   </style>
 </head>
 <body>
@@ -41,8 +55,23 @@ PAGE="
         &#128197;<input type='date' id='date' name='date' required />
       </label>
 
-      <input type='time' id='start' name='start' required />
-      <input type='time' id='end' name='end' required />
+      <label>
+        <input type='time' id='start' name='start' required />
+        <input list='time_zones' id='start_time_zone' name='start_time_zone'
+          placeholder='Timezone' required />
+        <datalist id='time_zones'>
+          $(echo ${time_zones} | as_options)
+        </datalist>
+      </label>
+
+      <label>
+        <input type='time' id='finish' name='finish' required />
+        <input list='time_zones' id='finish_time_zone' name='finish_time_zone'
+          placeholder='Timezone' required />
+        <datalist id='time_zones'>
+          $(echo ${time_zones} | as_options)
+        </datalist>
+      </label>
 
       <button type='submit'>Record</>
     </fieldset>
index 46a3a291c4a98f59a2d78c6452338f859290b6e2..5221190264d6ce46c1bb04e905d88b0d84711c3a 100644 (file)
--- a/sleep.sh
+++ b/sleep.sh
@@ -18,20 +18,47 @@ query_db() {
     --tuples-only --no-align --command="${1}"
 }
 
+insert_into() {
+  query_db "\
+  WITH new_id AS (
+    INSERT INTO ${1} (name)
+    VALUES ('${2}')
+    ON CONFLICT DO NOTHING
+    RETURNING id
+  )
+  SELECT *
+  FROM new_id
+  UNION
+  SELECT id
+  FROM ${1}
+  WHERE name='${2}'"
+}
+
 sleep_date=$(get_query_param "date")
+
 start=$(get_query_param "start")
+start_time_zone=$(get_query_param "start_time_zone")
+start_time_zone_id=$(insert_into "time_zones" "${start_time_zone}")
+
 finish=$(get_query_param "finish")
+finish_time_zone=$(get_query_param "finish_time_zone")
+finish_time_zone_id=$(insert_into "time_zones" "${finish_time_zone}")
+
 sleep_id=$(query_db "\
 WITH new_sleep_id AS (
   INSERT INTO sleep (
     date,
     start,
-    finish
+    start_time_zone_id,
+    finish,
+    finish_time_zone_id
   )
   VALUES (
     '${sleep_date}',
     '${start}',
-    '${finish}'
+    ${start_time_zone_id},
+    '${finish}',
+    ${finish_time_zone_id}
   )
   RETURNING id
 )
@@ -52,7 +79,9 @@ PAGE="
   </style>
 </head>
 <body>
-Recorded new sleep (id=${sleep_id}) from ${start} to ${finish}.
+Recorded new sleep (id=${sleep_id}) from ${start} ${start_time_zone} \
+(id=${start_time_zone_id}) to ${finish} ${finish_time_zone} \
+(id=${finish_time_zone_id}).
 </body>
 </html>"