help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

auto-format neat tables (and web stats tool) [Elisp]


From: Emanuel Berg
Subject: auto-format neat tables (and web stats tool) [Elisp]
Date: Thu, 19 May 2022 21:38:03 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Another Elisp program, this time it does a neat table from
incoming server traffic ...

As always when I write Elisp, in the process
I do a couple, in this case three, functions to make it work,
however these functions has really nothing to do with the task
at hand. All such functions should be in Emacs libraries.
And maybe they are, but I don't know where and don't know how
to find out. But I'm not gonna whine about that, instead let
me say that if they aren't, feel free to use mine and then
just drop me a line telling what library to include ...

The functions I refer to are shell-command-to, sum-table-row,
and percent.

Then follows the function piles-pts, this computes the hits
with shell tools and they are put in a table (a list of
lists).

Last, piles-pts-print which prints the result. You can see the
result here:

  https://dataswamp.org/~incal/ds-stats.txt

It looks like this:

However look at the code below to see the problem, the
"neatness" is actually hard-coded into the format string.

  (re-search-forward "TODO")
  (how-many "TODO") ; 3

I know how I computed that - well, I already did - and it's
very simple and mechanic. So is there a library to do
_that_ anywhere?

PS. Other comments welcome as well, as always ...

Keep it real time

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/piles.el
;;
;; example output:
;;
;;   -----------------------------------
;;      user         hits       %   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)

(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 percent (nom denom)
  (* 100 (/ nom denom 1.0)) )

(defun piles-pts ()
  (let ((usrs (shell-command-to '() "cut -d : -f 1 /etc/passwd") )
        (pts-dir "/var/www/logs")
        (pts-file)
        (pts)
        (fallout) )
    (cl-dolist (u usrs)
      (setq pts-file (format "%s/access-%s.log" pts-dir u))
      (when (file-exists-p pts-file)
        (setq pts (shell-command-to 1
                    (format "wc -l %s | awk '{print $1}'" pts-file) ))
        (cl-pushnew (list u pts) fallout)))
    (cl-sort (cl-copy-list fallout) #'> :key #'cl-second) ))
;; (piles-pts)

(defun piles-pts-print ()
  (interactive)
  (let*((fallout (piles-pts))
        (sum     (sum-table-row fallout 1))
        (best)
        ;; TODO: compute
        (line (make-string 35 ?\-) ))
    ;; TODO: compute
    (message "%s\n   user         hits    1st%%   all%%\n%s" line line)
    (cl-loop
     for (u pts) in fallout
     and pos from 1 to (length fallout)
     do (when (= pos 1)
          (setq best pts) )
        ;; TODO: compute
        (message "%d. %-9s% 8d% 8.2f% 7.2f" pos u pts
                                            (percent pts best)
                                            (percent pts sum) ))
    (message "%s\nhttps://dataswamp.org/~incal/emacs-init/piles.el"; line) ))

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]