summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2024-09-30 19:08:45 -0700
committerFranck Cuny <franck@fcuny.net>2024-09-30 19:08:45 -0700
commit4a17d979c608c5021dd8c57853da559f0bdcb232 (patch)
tree9e17fdb0483d48b31048e1eca06a5af0072bc70f
parentforgot to add the denote related code (diff)
downloademacs.d-4a17d979c608c5021dd8c57853da559f0bdcb232.tar.gz
add SLO calculator
-rw-r--r--init.el1
-rw-r--r--lisp/my-uptime.el63
2 files changed, 64 insertions, 0 deletions
diff --git a/init.el b/init.el
index cbf72e1..e58528e 100644
--- a/init.el
+++ b/init.el
@@ -49,6 +49,7 @@
 (require 'init-elfeed)
 
 (require 'my-denote)
+(require 'my-uptime)
 
 (report-time-since-load)
 
diff --git a/lisp/my-uptime.el b/lisp/my-uptime.el
new file mode 100644
index 0000000..7d1665c
--- /dev/null
+++ b/lisp/my-uptime.el
@@ -0,0 +1,63 @@
+;;; my-uptime.el --- calculates uptime for SLOs
+
+;;; Commentary:
+
+;; Calculate how much downtime is allowed for different periods of time
+;; based on a given SLO. Display results in an aligned org-mode table.
+
+;;; Code:
+
+(require 'org)
+
+(defconst my-uptime/buffer-name "*slo-calculator*")
+
+(defconst my-uptime/seconds-per-hour 3600
+  "Number of seconds in an hour.")
+(defconst my-uptime/seconds-per-day (* my-uptime/seconds-per-hour 24)
+  "Number of seconds in a day.")
+(defconst my-uptime/seconds-per-week (* my-uptime/seconds-per-day 7)
+  "Number of seconds in a week.")
+(defconst my-uptime/seconds-per-month (* my-uptime/seconds-per-day 30)
+  "Number of seconds in a month.")
+(defconst my-uptime/seconds-per-quarter (* my-uptime/seconds-per-month 3)
+  "Number of seconds in a quarter.")
+(defconst my-uptime/seconds-per-year (* my-uptime/seconds-per-month 12)
+  "Number of seconds in a year.")
+
+(defun my/uptime-is (slo)
+  "Return the amount of allowed downtime for a given SLO."
+  (interactive "nSLO:")
+  (let* ((slo (cond ((< slo 0) 0)
+                    ((> slo 100) 100)
+                    (t slo)))
+         (allowed (/ (- (* 100 100) (* slo 100.0)) (* 100 100))))
+    (my/uptime--message allowed slo)))
+
+(defun my/uptime--message (seconds slo)
+  "Insert buffer text with allowed downtime based on SECONDS (derived from SLO)."
+  (let ((inhibit-read-only t)
+        (buffer-undo-list t))
+    (pop-to-buffer my-uptime/buffer-name)
+    (erase-buffer)
+    (org-mode)
+    (insert (format "* Calculated allowed downtime for %.2f%% availability\n\n" slo))
+    (insert "| Time Period | Allowed Downtime |\n")
+    (insert "|-------------+-------------------|\n")
+    (dolist (period '(("Daily" . my-uptime/seconds-per-day)
+                      ("Weekly" . my-uptime/seconds-per-week)
+                      ("Monthly" . my-uptime/seconds-per-month)
+                      ("Quarterly" . my-uptime/seconds-per-quarter)
+                      ("Yearly" . my-uptime/seconds-per-year)))
+      (let* ((period-name (car period))
+             (period-seconds (symbol-value (cdr period)))
+             (downtime (seconds-to-time (* period-seconds seconds))))
+        (insert (format "| %s | %s |\n"
+                        period-name
+                        (format-seconds "%D %H:%M:%S" downtime)))))
+    (insert "|-------------+-------------------|\n")
+    (org-table-align)
+    (goto-char (point-min))))
+
+(provide 'my-uptime)
+
+;;; my-uptime.el ends here