;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/piles.el ;; ;; also see: ;; https://dataswamp.org/~incal/hits/hits.gpi ;; https://dataswamp.org/~incal/hits/hits.png ;; ;; output with util-linux column(1) and GNU sed(1): ;; ;; ----------------------------------- ;; user hits 1st% all% ;; ----------------------------------- ;; 1. solene 7971388 100.00 85.56 ;; 2. incal 728391 9.14 7.82 ;; 3. lich 599255 7.52 6.43 ;; 4. josk 12056 0.15 0.13 ;; 5. katzeilla 3977 0.05 0.04 ;; 6. rjc 1706 0.02 0.02 ;; ----------------------------------- (require 'cl-lib) (require 'math-incal) (require 'search-regexp-in-files) (defun shell-command-to (eg cmd) (cond ((listp eg) (split-string (shell-command-to-string cmd))) ((numberp eg) (string-to-number (shell-command-to-string cmd))) (t (error "Argument (%s) type (%s) DNC" eg (type-of eg))) )) (defun sum-table-row (tbl r) (cl-loop with sum = 0 for row in tbl do (cl-incf sum (nth r row)) finally return sum) ) ;; (sum-table-row (piles-pts) 1) (defun piles-get-files (user) (let*((pts-dir "/var/www/logs") (pts-file (format "%s/access-%s.log" pts-dir user)) (gz-files (files-as-list (format "%s.[0-9]*.gz" pts-file))) (files `(,pts-file ,@gz-files) )) files) ) ;; (piles-get-files "incal") (defun piles-hits-from-files (user) (let*((files (piles-get-files user)) (pts-file (car files)) (gz-files (cdr files)) (pts 0) ) (when (file-exists-p pts-file) (cl-incf pts (shell-command-to 1 (format "wc -l %s | awk '{print $1}'" pts-file) ))) (dolist (gf gz-files) (when (file-exists-p gf) (cl-incf pts (shell-command-to 1 (format "zcat %s | wc -l" gf))) )) pts) ) ;; (piles-hits-from-files "incal") (defun piles-pts () (let ((usrs (sort (shell-command-to '() "cut -d : -f 1 /etc/passwd") #'string>) ) (pts) (fallout) ) (dolist (u usrs) (setq pts (piles-hits-from-files u)) (unless (zerop pts) (cl-pushnew (list u pts) fallout :test #'equal) )) fallout) ) ;; (piles-pts) (defun piles-all-vertical-header () (message (mapconcat 'identity (mapcar (lambda (h) (format "%s" (car h))) (piles-pts)) " "))) ;; (piles-all-vertical-header) (defun piles-all-vertical () (message (mapconcat 'identity (mapcar (lambda (h) (format "%s" (cadr h))) (piles-pts)) " "))) ;; (piles-all-vertical) (defun piles-pts-print () (interactive) (let*((fallout (piles-pts)) (sum (sum-table-row fallout 1)) (best (cadr (car fallout))) ) (message " :user:hits: 1st%%: all%%\n") (cl-loop for (u pts) in fallout and pos from 1 to (length fallout) do (message "%d.:%s:%d:%6.2f:%6.2f" pos u pts (percent pts best) (percent pts sum) )))) ;; (piles-pts-print) (provide 'piles)