summaryrefslogtreecommitdiff
path: root/src/gizmo/wasp-debt.el
blob: 4548f3a4778fc0d9a324c4a3fc85ed081888dcc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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