summaryrefslogtreecommitdiff
path: root/src/gizmo/wasp-debt.el
diff options
context:
space:
mode:
Diffstat (limited to 'src/gizmo/wasp-debt.el')
-rw-r--r--src/gizmo/wasp-debt.el108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/gizmo/wasp-debt.el b/src/gizmo/wasp-debt.el
new file mode 100644
index 00000000..4548f3a4
--- /dev/null
+++ b/src/gizmo/wasp-debt.el
@@ -0,0 +1,108 @@
+;;; wasp-debt --- description -*- lexical-binding: t; -*-
+;;; Commentary:
+;;; Code:
+
+(require 'rx)
+(require 'ht)
+(require 'wasp-db)
+(require 'wasp-user)
+(require 'wasp-gcp)
+
+(defvar w/debt-seconds 0)
+(defvar w/debt-total-humans 8000000000)
+(defvar w/debt-total-gamers 0)
+(defvar w/debt-nix-store 0)
+(defvar w/debt-nix-store-glibc 0)
+
+(defun w/debt-data-fetch ()
+ "Fetch relevant APIs for debts."
+ (w/fetch-json "https://www.valvesoftware.com/about/stats"
+ (lambda (data)
+ (when (hash-table-p data)
+ (setf w/debt-total-gamers (string-to-number (s-replace "," "" (ht-get data "users_ingame")))))))
+ (w/process "nix_store_size"
+ (lambda (data)
+ (setf w/debt-nix-store (string-to-number data))))
+ (w/process "nix_count_glibc"
+ (lambda (data)
+ (setf w/debt-nix-store-glibc (string-to-number data)))))
+
+(defun w/debt-heraldry-section ()
+ "Return the heraldry section of the docket."
+ (-some->> (w/slurp "~/notes/docket.org")
+ (s-split (rx "** Heraldry\n"))
+ (cadr)
+ (s-split (rx "* Irons"))
+ (car)
+ (s-lines)))
+
+(defun w/debt-irons-section ()
+ "Return the heraldry section of the docket."
+ (-some->> (w/slurp "~/notes/docket.org")
+ (s-split (rx "* Irons\n"))
+ (cadr)
+ (s-split (rx "* Gold"))
+ (car)))
+
+(defun w/debt-heraldry-unredeemed (term heraldry)
+ "Determine the number of unredeemed TERMs from HERALDRY."
+ (-sum
+ (--map
+ (if (s-contains? term it)
+ (if-let* ((matches (s-match-strings-all (rx "x" (group (+ digit))) it)))
+ (string-to-number (cadar matches))
+ 1)
+ 0)
+ heraldry)))
+
+(defun w/debt-total-user-stat (f g)
+ "Run G on all user data and fold the results with F."
+ 0
+ ;; (-reduce f
+ ;; (-non-nil
+ ;; (ht-map
+ ;; (lambda (_ v)
+ ;; (when (listp v)
+ ;; (funcall g v)))
+ ;; w/user-cache)))
+ )
+
+(defun w/debt-tcp-connections ()
+ "Return the number of open TCP connections."
+ (length (s-match-strings-all (rx bol "tcp") (shell-command-to-string "netstat -an"))))
+
+(defun w/debt-upload ()
+ "Upload a record of our current debts to the database."
+ (let ((heraldry (w/debt-heraldry-section)))
+ (w/db-hset "debt"
+ "clones-unredeemed" (number-to-string (w/debt-heraldry-unredeemed "clone" heraldry))
+ "equity-unredeemed" (number-to-string (w/debt-heraldry-unredeemed "equity" heraldry))
+ "clones-ellg-unredeemed" (number-to-string (w/debt-heraldry-unredeemed "ellg clone" heraldry))
+ "clones-tyumici-unredeemed" (number-to-string (w/debt-heraldry-unredeemed "tyumici clone" heraldry))
+ "total-boosts" (number-to-string (w/debt-total-user-stat #'+ (lambda (u) (alist-get :boost u))))
+ "total-equity" (number-to-string (w/debt-total-user-stat #'+ (lambda (u) (alist-get :equity u))))
+ "seconds" (number-to-string w/debt-seconds)
+ "docket-entries" (number-to-string (length (s-lines (w/slurp "/home/llll/notes/docket.org"))))
+ "irons-in-fires" (number-to-string (length (s-match-strings-all (rx bol "***") (w/debt-irons-section))))
+ "planes-owned" "0"
+ "danger" (number-to-string w/gcp)
+ "tcp-connections" (number-to-string (w/debt-tcp-connections))
+ "total-gamers" (number-to-string w/debt-total-gamers)
+ "total-humans" (number-to-string w/debt-total-humans)
+ "gamer-ratio" (number-to-string (* 100.0 (/ (float w/debt-total-gamers) w/debt-total-humans)))
+ "nix-store" (number-to-string w/debt-nix-store)
+ "nix-store-glibc" (number-to-string w/debt-nix-store-glibc)
+ "disk-usage" (number-to-string (- 100 (w/disk-usage "/")))
+ "flatpaks-installed" "0"
+ "containers" "0"
+ "appimages-installed" "0"
+ "hours-wasted" "0"
+ "streams-spent-on-this" "2"
+ ))
+ (incf w/debt-seconds 10))
+(add-hook 'w/gizmo-update-hook #'w/debt-upload)
+
+(w/debt-data-fetch)
+
+(provide 'wasp-debt)
+;;; wasp-debt.el ends here