]> git.ekhem.eu.org Git - metrics.git/commitdiff
Add a script for tracking sleep schedule.
authorJakub Czajka <jakub@ekhem.eu.org>
Tue, 2 Jan 2024 12:00:11 +0000 (13:00 +0100)
committerJakub Czajka <jakub@ekhem.eu.org>
Tue, 2 Jan 2024 13:15:54 +0000 (14:15 +0100)
metrics.sh [new file with mode: 0644]
sleep.sh [new file with mode: 0644]

diff --git a/metrics.sh b/metrics.sh
new file mode 100644 (file)
index 0000000..ee1fc09
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/sh
+# Copyright (c) 2024 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+. /etc/environment
+
+PAGE="
+<!DOCTYPE html>
+<html>
+<meta name='viewport' content='width=device-width'>
+<head>
+  <title>Metrics</title>
+  <style>
+    body {
+      background-color: #7fa99b;
+    }
+    form {
+      width: min(400px, 100%);
+    }
+    fieldset {
+      background-color: #f1d18a;
+      display: flex;
+      flex-direction: column;
+      row-gap: 5px;
+    }
+    label {
+      column-gap: 5px;
+      display: flex;
+    }
+    input[type=date] {
+      width: 100%;
+    }
+  </style>
+</head>
+<body>
+  <form method='post' action='/sleep'>
+    <fieldset>
+      <legend>Sleep</legend>
+
+      <label>
+        &#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 />
+
+      <button type='submit'>Record</>
+    </fieldset>
+  </form>
+</body>
+</html>"
+
+echo "HTTP/1.1 200 OK"
+echo "Content-Type: text/html"
+echo "${PAGE}" | sed 's/^[ \t]*//g'
diff --git a/sleep.sh b/sleep.sh
new file mode 100644 (file)
index 0000000..46a3a29
--- /dev/null
+++ b/sleep.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+# Copyright (c) 2024 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+. /etc/environment
+
+request_body=$(echo "${REQUEST_BODY}" | /usr/bin/python3 -c "\
+import sys;
+from urllib.parse import unquote_plus;
+print(unquote_plus(sys.stdin.read()));")
+
+get_query_param() {
+  echo "${request_body}" | sed "s/^.*${1}=// ; s/\&.*$//"
+}
+
+query_db() {
+  /usr/bin/psql --user=metrics_tracker --dbname="${METRICS_DB}" \
+    --tuples-only --no-align --command="${1}"
+}
+
+sleep_date=$(get_query_param "date")
+start=$(get_query_param "start")
+finish=$(get_query_param "finish")
+sleep_id=$(query_db "\
+WITH new_sleep_id AS (
+  INSERT INTO sleep (
+    date,
+    start,
+    finish
+  )
+  VALUES (
+    '${sleep_date}',
+    '${start}',
+    '${finish}'
+  )
+  RETURNING id
+)
+SELECT *
+FROM new_sleep_id;")
+
+PAGE="
+<!DOCTYPE html>
+<html>
+<meta name='viewport' content='width=device-width'>
+<head>
+  <title>Purchase</title>
+  <style>
+    body {
+      white-space: pre-wrap;
+      font-size: 1.2em;
+    }
+  </style>
+</head>
+<body>
+Recorded new sleep (id=${sleep_id}) from ${start} to ${finish}.
+</body>
+</html>"
+
+echo "HTTP/1.1 200 OK"
+echo "Content-Type: text/html"
+echo "${PAGE}" | sed 's/^[ \t]*//g'