summaryrefslogtreecommitdiff
path: root/rt-liberation-report.el
blob: b2af361e9ef1865f582169df404ce35ba2fbce31 (plain) (blame)
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
;;; rt-liberation-report.el --- Free from RT

;; Copyright (C) 2015  Yoni Rabkin
;;
;; Authors: Yoni Rabkin <yrk@gnu.org>
;;
;; 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 2 of the
;; License, 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., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.

;;; History:
;;
;; I wrote rt-report.py sometime in 2013 because people asked for some
;; information as to how many tickets were being resolved, and by
;; whom. When in came time up update rt-report.py I came to my senses
;; and decided to re-write it in Emacs Lisp as a part of
;; rt-liberation.


;;; Code:

(require 'rt-liberation-rest)

(defvar rt-liber-report-csv-header
  '("date" "tickets resolved")
  "Headers for comma separated value output.")

(defun rt-liber-report-get-interval (rt-queue start-date end-date)
  "Return tickets resolved between START-DATE and END-DATE.

The tickets must have their current status be Resolved in order
to be returned by this function. If no tickets match the query,
return `nil'."
  (when (or (not (stringp rt-queue))
	    (not (stringp start-date))
	    (not (stringp end-date)))
    (error "bad argument/s"))
  (rt-liber-rest-run-show-base-query
   (rt-liber-rest-run-ls-query
    (rt-liber-compile-query
     (and (queue    rt-queue)
	  (resolved start-date end-date)
	  (status   "resolved"))))))

(defun rt-liber-report-scan-ticket (ticket-alist)
  (let ((date-resolved (cdr (assoc "Resolved" ticket-alist)))
	(owner         (cdr (assoc "Owner" ticket-alist))))
    `(,(format-time-string "%d-%m-%Y" (date-to-time date-resolved))
      . ,owner)))

(defun rt-liber-report-scan-interval (interval)
  "Convert the list of tickets into an ordered format."
  (when (not interval)
    (error "no tickets in interval"))
  (let ((l (copy-tree interval))
	(r nil))
    (while l
      (setq r (append r `(,(rt-liber-report-scan-ticket (car l)))))
      (setq l (cdr l)))
    (setq r (sort r
		  #'(lambda (a b)
		      (string-lessp (car a) (car b)))))
    r))

(defun rt-liber-report-count (f l)
  (let (out)
    (while l
      (let* ((head (car l))
	     (old-value (cdr (assoc (funcall f head) out))))
	(if old-value
	    (setcdr (assoc (funcall f head) out) (+ old-value 1))
	  (setq out (append out `((,(funcall f head) . 1))))))
      (setq l (cdr l)))
    out))

(defun rt-liber-report-count-by-date (l)
  (rt-liber-report-count #'car l))

(defun rt-liber-report-count-by-owner (l)
  (rt-liber-report-count #'cdr l))

(defun rt-liber-report-print-csv (header l)
  (let (out)
    (with-temp-buffer
      (insert (format "\n%s\n" header))
      (dolist (entry l)
	(insert
	 (format "%s, %s\n" (car entry) (cdr entry))))
      (setq out (buffer-string)))
    out))

(defun rt-liber-report (rt-queue start-date end-date)
  (let ((tickets (rt-liber-report-scan-interval
		  (rt-liber-report-get-interval
		   rt-queue end-date start-date)))
	by-date by-owner
	by-date-out
	by-owner-out)
    (when (not tickets)
      (error (concat "no tickets in interval between "
		     start-date " and " end-date)))
    ;; collate
    (setq by-date  (rt-liber-report-count-by-date tickets)
	  by-owner (rt-liber-report-count-by-owner tickets))
    ;; rank owners by resolved tickets
    (setq by-owner
	  (sort
	   by-owner
	   #'(lambda (a b)
	       (> (cdr a) (cdr b)))))
    ;; print
    (insert (rt-liber-report-print-csv "date, resolved" by-date))
    (insert (rt-liber-report-print-csv "owner, resolved" by-owner))))


(provide 'rt-report)

;;; rt-liberation-report.el ends here.