summaryrefslogtreecommitdiff
path: root/guile/scripts/histogram.scm
blob: b845f28119673e8e92e51897ac422c78c950506d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh
exec guile -e main -s $0 $@
!#
;; Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;;
;; This program is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation; either version 3, or (at your option) any
;; later version.

;; INFO: Histogram of the number of messages per time-unit
;; INFO: Options:
;; INFO:   --query=<query>:   limit to messages matching query
;; INFO:   --muhome=<muhome>: path to mu home dir
;; INFO:   --time-unit:       hour|day|month|year|month-year
;; INFO:   --output:          the output format, such as "png", "wxt"
;; INFO:                      (depending on the environment)

(use-modules (mu) (mu stats) (mu plot)
	     (ice-9 getopt-long) (ice-9 format))

(define (per-hour expr output)
  "Count the total number of messages per hour that match EXPR.
OUTPUT corresponds to the output format, as per gnuplot's 'set terminal'."
  (mu:plot-histogram
    (sort
      (mu:tabulate
	(lambda (msg)
	  (tm:hour (localtime (mu:date msg)))) expr)
      (lambda (x y) (< (car x) (car y))))
    (format #f "Messages per hour matching ~a" expr)
    "Hour" "Messages" output))


(define (per-day expr output)
  "Count the total number of messages for each weekday (0-6 for
Sun..Sat) that match EXPR. OUTPUT corresponds to the output format, as
per gnuplot's 'set terminal'."
  (mu:plot-histogram
    (mu:weekday-numbers->names
      (sort (mu:tabulate
	      (lambda (msg)
		(tm:wday (localtime (mu:date msg)))) expr)
	(lambda (x y) (< (car x) (car y)))))
    (format #f "Messages per weekday matching ~a" expr)
    "Day" "Messages" output))

(define (per-month expr output)
  "Count the total number of messages per month that match EXPR.
OUTPUT corresponds to the output format, as per gnuplot's 'set terminal'."
  (mu:plot-histogram
    (mu:month-numbers->names
      (sort
	(mu:tabulate
	  (lambda (msg)
	    (tm:mon (localtime (mu:date msg)))) expr)
	(lambda (x y) (< (car x) (car y)))))
    (format #f "Messages per month matching ~a" expr)
    "Month" "Messages" output))

(define (per-year expr output)
  "Count the total number of messages per year that match EXPR. OUTPUT corresponds
to the output format, as per gnuplot's 'set terminal'."
  (mu:plot-histogram
   (sort (mu:tabulate
	  (lambda (msg)
	    (+ 1900 (tm:year (localtime (mu:date msg))))) expr)
	 (lambda (x y) (< (car x) (car y))))
   (format #f "Messages per year matching ~a" expr)
   "Year" "Messages"  output))


(define (per-year-month expr output)
  "Count the total number of messages for each year and month that match EXPR.
OUTPUT corresponds to the output format, as per gnuplot's 'set terminal'."
  (mu:plot-histogram
    (sort (mu:tabulate
	    (lambda (msg)
	      (string->number
		(format #f "~d~2'0d"
		  (+ 1900 (tm:year (localtime (mu:date msg))))
		  (tm:mon (localtime (mu:date msg))))))
	    expr)
      (lambda (x y) (< (car x) (car y))))
    (format #f "Messages per year/month matching ~a" expr)
    "Year/Month" "Messages" output))

(define (main args)
  (let* ((optionspec
	  '((time-unit (value #t))
	    (query     (value #t))
	    (muhome    (value #t))
	    (output    (value #t))
	    (help      (single-char #\h) (value #f))))
	 (options (getopt-long args optionspec))
	 (help (option-ref options 'help #f))
	 (time-unit (option-ref options 'time-unit "year"))
	 (muhome (option-ref options 'muhome #f))
	 (query (option-ref options 'query ""))
	 (output (option-ref options 'output "dumb"))
	 (rest (option-ref options '() #f))
	 (func
	  (cond
	   ((equal? time-unit "hour") per-hour)
	   ((equal? time-unit "day")  per-day)
	   ((equal? time-unit "month") per-month)
	   ((equal? time-unit "year") per-year)
	   ((equal? time-unit "year-month") per-year-month)
	   (else #f))))
    (setlocale LC_ALL "")
    (unless func
      (display "error: unknown time-unit\n")
      (set! help #t))
    (if help
	(begin
	  (display
	   (string-append "parameters: [--help] [--output=dumb|png|wxt] "
			  "[--muhome=<muhome>] [--query=<query>]"
			  "[--time-unit=hour|day|month|year|year-month]"))
	  (newline))
	(begin
	  (mu:initialize muhome)
	  (func query output)))))

;; Local Variables:
;; mode: scheme
;; End: