aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahm@jadupc.com>2023-03-23 17:01:08 +0600
committerLibravatar Mubashshir <ahm@jadupc.com>2023-03-23 17:02:05 +0600
commit171af59c7c1589eb7f16118ddd28e3a55f9cf0d1 (patch)
tree974fc505ee2b351e5f39b244b7019d8be02b99cc
parentb3ccd6b08b908e60fd452bcfaf520e92e7b6f232 (diff)
downloadshopno-os-log-sync-171af59c7c1589eb7f16118ddd28e3a55f9cf0d1.tar.gz
shopno-os-log-sync-171af59c7c1589eb7f16118ddd28e3a55f9cf0d1.zip
Implement Log uploader
Signed-off-by: Mubashshir <ahm@jadupc.com>
-rw-r--r--data/.gitignore1
-rw-r--r--data/scripts/common.sh51
-rwxr-xr-xdata/scripts/log-sync36
-rw-r--r--debian/changelog6
-rw-r--r--debian/install2
-rw-r--r--debian/shopno-os-log-sync@.user.service6
-rw-r--r--debian/shopno-os-log-sync@.user.timer9
7 files changed, 111 insertions, 0 deletions
diff --git a/data/.gitignore b/data/.gitignore
new file mode 100644
index 0000000..6804e7f
--- /dev/null
+++ b/data/.gitignore
@@ -0,0 +1 @@
+!scripts/
diff --git a/data/scripts/common.sh b/data/scripts/common.sh
new file mode 100644
index 0000000..faa6b03
--- /dev/null
+++ b/data/scripts/common.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+
+SERVER_URL="https://frypan.jadupc.com" #"builder.jadupc.com"
+SERVER_URL="https://builder.jadupc.com/logs" #"builder.jadupc.com"
+
+if [[ $DEBUG ]]; then
+ SERVER_URL="http://log-server.local"
+fi
+
+function getiface
+{
+ printf '%s\n' "/sys/class/$1"*/ | head -n1
+}
+
+function getaddr
+{
+ if [[ -d $1 ]]; then
+ tr ':' '-' < "$1/address"
+ else
+ echo '00-00-00-00-00-00'
+ fi
+}
+
+function communicate
+{
+ local endpoint type wlmac enmac year week day month
+ endpoint="$1"
+ type="$2"
+ wlmac="$3"
+ enmac="$4"
+ year="$5"
+ shift 5
+ case "${type#*/}" in
+ weekly)
+ week="$1"
+ shift 1
+ set -- -d "week=$week" "$@"
+ ;;
+ daily)
+ month="$1"
+ day="$2"
+ shift 2
+ set -- -d "month=$month" -d "day=$day" "$@"
+ ;;
+ esac
+
+ curl \
+ -d "type=${type%%/*}" -d "wlmac=$wlmac" \
+ -d "enmac=$enmac" -d "year=$year" "$@" \
+ "$SERVER_URL$endpoint"
+}
diff --git a/data/scripts/log-sync b/data/scripts/log-sync
new file mode 100755
index 0000000..e810f6e
--- /dev/null
+++ b/data/scripts/log-sync
@@ -0,0 +1,36 @@
+#!/bin/bash
+source "${BASH_ARGV0%/*}"/common.sh
+RAW_DATA_PATH=""
+
+enmac="$(getaddr "$(getiface "net/e")")"
+wlmac="$(getaddr "$(getiface "net/w")")"
+
+get_logs()
+{
+ find "${XDG_CACHE_HOME:-$HOME/.cache}/Watcher/$1" -type f -exec basename -a '{}' + \
+ | grep -oE "$2"
+}
+
+# shellcheck disable=SC2031,SC2030
+if [[ $1 == "weekly" ]]; then
+ watcher -ws 2> /dev/null >&2
+
+ get_logs Analysis '[0-9]+-[0-9]+' \
+ | while IFS=- read -r week year; do
+ ! communicate "/whack/a/mole" user/weekly "$wlmac" "$enmac" "$year" "$week" --silent --fail || continue
+
+ echo "Uploading log: Week $week, $year" >&2
+ communicate "/puss/in/boots" user/weekly "$wlmac" "$enmac" "$year" "$week" \
+ -# --data-urlencode "data@$RAW_DATA_PATH/Analysis/W$week-$year.csv"
+ done
+else
+ get_logs daily_data '[0-9]{4}(-[0-9]{2}){2}' \
+ | while IFS=- read -r year month day; do
+ ! [[ $(date --rfc-3339=date) == "$year-$month-$day" ]] || continue
+ ! communicate "/whack/a/mole" user/daily "$wlmac" "$enmac" "$year" "$month" "$day" --silent --fail || continue
+
+ echo "Uploading log: $year-$month-$day" >&2
+ communicate "/puss/in/boots" user/daily "$wlmac" "$enmac" "$year" "$month" "$day" \
+ -# --data-urlencode "data@$RAW_DATA_PATH/daily_data/$year-$month-$day.csv"
+ done
+fi
diff --git a/debian/changelog b/debian/changelog
index 93e66fe..7e881aa 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+shopno-os-log-sync (1.99.2) shopno; urgency=medium
+
+ * Implement Log uploader
+
+ -- Mubashshir <ahm@jadupc.com> Thu, 23 Mar 2023 17:01:35 +0600
+
shopno-os-log-sync (1.99.1) shopno; urgency=medium
* Add Autostart entry
diff --git a/debian/install b/debian/install
index 0da326b..1e3ced0 100644
--- a/debian/install
+++ b/debian/install
@@ -1,3 +1,5 @@
src/Watcher/ usr/share/
src/bin/watcher usr/bin/
data/shopno-os-log.desktop etc/xdg/autostart/
+data/scripts/common.sh usr/share/Watcher/
+data/scripts/log-sync usr/share/Watcher/
diff --git a/debian/shopno-os-log-sync@.user.service b/debian/shopno-os-log-sync@.user.service
new file mode 100644
index 0000000..928dd23
--- /dev/null
+++ b/debian/shopno-os-log-sync@.user.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Sync %I logs
+
+[Service]
+Type=oneshot
+ExecStart=/usr/share/Watcher/log-sync %i
diff --git a/debian/shopno-os-log-sync@.user.timer b/debian/shopno-os-log-sync@.user.timer
new file mode 100644
index 0000000..ec7bbb3
--- /dev/null
+++ b/debian/shopno-os-log-sync@.user.timer
@@ -0,0 +1,9 @@
+[Unit]
+Description=Sync usage logs with server %i
+
+[Timer]
+OnCalendar=%i
+Persistent=true
+
+[Install]
+WantedBy=timers.target