Change output to a tab separated output to make it parseable

This commit is contained in:
Solene Rapenne 2019-07-09 09:05:40 +02:00
parent 17024e3560
commit e6d5e1bcf7
1 changed files with 16 additions and 21 deletions

View File

@ -8,14 +8,6 @@
(defparameter *states-dir* "~/.reed-alert/states/")
(ensure-directories-exist *states-dir*)
(defun color(num1 num2)
(format nil "~a[~a;~am" #\Escape num1 num2))
(defparameter *red* (color 1 31))
(defparameter *white* (color 0 70))
(defparameter *green* (color 1 32))
(defparameter *yellow* (color 0 33))
;; simple hash function (Fowler Noll Vo)
;; https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
(defun fnv-hash(string)
@ -98,11 +90,13 @@
(or ,@body)))
(defun =>(level fonction &rest params)
(format t "[~a~10a ~20A~a] ~45A" *yellow* level fonction *white* (getf params :desc params))
(let* ((hash (fnv-hash (format nil "~{~a~}" (remove-if #'symbolp params))))
(result (funcall fonction params))
(filename (format nil "~a-~a-~a" level fonction hash))
(filepath (format nil "~a/~a" *states-dir* filename)))
(filepath (format nil "~a/~a" *states-dir* filename))
(current-state 'failure) ;; default state is a failure
(previous-state 'success)
(trigger-state 'no))
;; we open the file to read the number of tries
;; if no fail then we have 0 try
@ -117,18 +111,17 @@
(if (not (listp result))
(progn
;; mark state as success
(setf current-state 'success)
;; we delete the file with previous states
(when (probe-file filepath)
(delete-file filepath))
;; it was a failure and then it's back to normal state
(if triggered-before?
(progn
(uiop:run-program (trigger-alert level fonction params t 'success) :output t)
(format t " => ~afailure => success~a~%" *green* *white*))
(progn
;; last time was a success
(format t " => ~asuccess~a~%" *green* *white*)))
(when triggered-before?
(uiop:run-program (trigger-alert level fonction params t 'success) :output t)
(setf previous-state 'failure))
;; in any case we return t because it's ok
t)
@ -143,16 +136,18 @@
(and (= 0 (mod (+ 1 tries) (getf params :reminder *reminder*)))
'REMINDER))))) ;; do we need to remind it's failing?
(format t " => ~aerror (~a failure(s) before)~a~a~%" *red* tries *white* (if trigger-now? " NOTIFIED" ""))
;; more error than limit, send alert once
(when trigger-now?
(uiop:run-program (trigger-alert level fonction params (cadr result) trigger-now?) :output t))
(setf trigger-state 'notified)
(uiop:run-program (trigger-alert level fonction params (cadr result) trigger-now?)))
;; increment the number of tries by 1
(with-open-file (stream-out filepath :direction :output
:if-exists :supersede)
(format stream-out "~a~%~a~%" (+ 1 tries) params))
nil)))))
nil))
(format t "~a ~A ~A ~A ~A ~A~%"
level fonction params previous-state current-state trigger-state))))
;; abort when using ctrl+c instead of dropping to debugger
#+ecl