summaryrefslogtreecommitdiff
path: root/guile/examples/msg-graphs
blob: 654dd28cb65b234cc94c49f4cf60b6e1dc7434df (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
128
129
130
131
132
133
#!/bin/sh
exec guile -e main -s $0 $@
!#
;;
;; Copyright (C) 2011-2012 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.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;

;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
(setlocale LC_ALL "")

(use-modules (ice-9 getopt-long) (ice-9 optargs) (ice-9 popen) (ice-9 format))
(use-modules (mu) (mu stats) (mu plot))
;;(use-modules (mu) (mu message) (mu stats) (mu plot))

(define (per-hour 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
    (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
    (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 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
    (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-month 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
    (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 (per-year 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
    (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 (main args)
  (let* ((optionspec   '( (muhome     (value #t))
			  (what       (value #t))
			  (text       (value #f))
			  (help       (single-char #\h) (value #f))))
	  (options (getopt-long args optionspec))
	  (msg (string-append
		 "usage: mu-msg-stats [--help] [--text] "
		 "[--muhome=<muhome>] "
		 "--what=<per-hour|per-day|per-month|per-year-month|"
		 "per-year> [searchexpr]\n"))
	  (help (option-ref options 'help #f))
	  (what (option-ref options 'what #f))
	  (text (option-ref options 'text #f))
	  ;; if `text' is `#f', use a graphical window by setting output to "wxt",
	  ;; else use text-mode plotting ("dumb")
	  (output (if text "dumb" "wxt"))
	  (muhome (option-ref options 'muhome #f))
	  (restargs (option-ref options '() #f))
	  (expr (if restargs (string-join restargs) "")))
    (if (or help (not what))
      (begin
	(display msg)
	(exit (if help 0 1))))
    (mu:initialize muhome)
    (cond
      ((string= what "per-hour")  (per-hour expr output))
      ((string= what "per-day")   (per-day expr output))
      ((string= what "per-month") (per-month expr output))
      ((string= what "per-year-month") (per-year-month expr output))
      ((string= what "per-year")  (per-year expr output))
      (else (begin
	      (display msg)
	      (exit 1))))))

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