unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* auto-format neat tables (and web stats tool) [Elisp]
@ 2022-05-19 19:38 Emanuel Berg
  2022-05-19 21:05 ` Emanuel Berg
  0 siblings, 1 reply; 2+ messages in thread
From: Emanuel Berg @ 2022-05-19 19:38 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: auto-format neat tables (and web stats tool) [Elisp]
  2022-05-19 19:38 auto-format neat tables (and web stats tool) [Elisp] Emanuel Berg
@ 2022-05-19 21:05 ` Emanuel Berg
  0 siblings, 0 replies; 2+ messages in thread
From: Emanuel Berg @ 2022-05-19 21:05 UTC (permalink / raw)
  To: help-gnu-emacs

> However look at the code below to see the problem, the
> "neatness" is actually hard-coded into the format string [...]
>
> 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?

Doesn't LOOK like it but I'll just pipe it to column(1)
instead I realized ...

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-19 21:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-19 19:38 auto-format neat tables (and web stats tool) [Elisp] Emanuel Berg
2022-05-19 21:05 ` Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).