Source file summary_table.ml
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module By_type = struct
module Type = struct
type t =
| Invalid
| CR
| XCR
| Soon
| Someday
[@@deriving compare, sexp_of]
let to_string t =
match sexp_of_t t with
| Atom a -> a
| List _ -> assert false
;;
let of_cr (cr : Cr_comment.t) =
match Cr_comment.header cr with
| Error _ -> Invalid
| Ok h ->
(match Cr_comment.Header.status h with
| XCR -> XCR
| CR ->
(match Cr_comment.Header.qualifier h with
| None -> CR
| Soon -> Soon
| Someday -> Someday))
;;
end
module Row = struct
type t =
{ type_ : Type.t
; count : int
}
end
type t = { rows : Row.t list }
let make (crs : Cr_comment.t list) =
let rows =
List.map crs ~f:Type.of_cr
|> List.sort_and_group ~compare:Type.compare
|> List.map ~f:(function
| [] -> assert false
| type_ :: _ as list -> { Row.type_; count = List.length list })
in
{ rows }
;;
let columns =
Text_table.O.
[ Column.make ~header:"CR Type" (fun (row : Row.t) ->
Cell.text (Type.to_string row.type_))
; Column.make ~header:"Count" ~align:Right (fun (row : Row.t) ->
Cell.text
~style:
(match row.type_ with
| Invalid -> Style.fg_red
| CR | XCR | Soon | Someday -> Style.default)
(Int.to_string_hum row.count))
]
;;
let to_text_table t =
if List.is_empty t.rows then None else Some (Text_table.make ~columns ~rows:t.rows)
;;
end
module Type = struct
type t =
| CR
| XCR
| Soon
| Someday
let h =
match Cr_comment.Header.status h with
| XCR -> XCR
| CR ->
(match Cr_comment.Header.qualifier h with
| None -> CR
| Soon -> Soon
| Someday -> Someday)
;;
end
module Key = struct
type t =
{ reporter : Vcs.User_handle.t
; recipient : Vcs.User_handle.t option
}
[@@deriving compare]
let (h : Cr_comment.Header.t) =
{ reporter = Cr_comment.Header.reporter h; recipient = Cr_comment.Header.recipient h }
;;
end
module Row = struct
type t =
{ reporter : Vcs.User_handle.t
; recipient : Vcs.User_handle.t option
; cr_count : int
; xcr_count : int
; soon_count : int
; someday_count : int
; total_count : int
}
end
type t = { rows : Row.t list }
let make (crs : Cr_comment.t list) =
let rows =
List.filter_map crs ~f:(fun cr ->
match Cr_comment.header cr with
| Error _ -> None
| Ok h ->
let key = Key.of_header h in
let type_ = Type.of_header h in
Some (key, type_))
|> List.sort_and_group ~compare:(fun (k1, _) (k2, _) -> Key.compare k1 k2)
|> List.map ~f:(function
| [] -> assert false
| ({ Key.reporter; recipient }, _) :: _ as list ->
let cr_count = ref 0 in
let xcr_count = ref 0 in
let soon_count = ref 0 in
let someday_count = ref 0 in
let total_count = ref 0 in
List.iter list ~f:(fun (_, type_) ->
Int.incr total_count;
match (type_ : Type.t) with
| CR -> Int.incr cr_count
| XCR -> Int.incr xcr_count
| Soon -> Int.incr soon_count
| Someday -> Int.incr someday_count);
{ Row.reporter
; recipient
; cr_count = !cr_count
; xcr_count = !xcr_count
; soon_count = !soon_count
; someday_count = !someday_count
; total_count = !total_count
})
in
{ rows }
;;
let columns =
let count ~ count =
Text_table.O.(
Column.make ~header ~align:Right (fun (row : Row.t) ->
let count = count row in
if count = 0 then Cell.empty else Cell.text (Int.to_string_hum count)))
in
Text_table.O.
[ Column.make ~header:"Reporter" (fun (row : Row.t) ->
Cell.text (Vcs.User_handle.to_string row.reporter))
; Column.make ~header:"For" (fun (row : Row.t) ->
match row.recipient with
| None -> Cell.empty
| Some user -> Cell.text (Vcs.User_handle.to_string user))
; count ~header:"CRs" (fun row -> row.cr_count)
; count ~header:"XCRs" (fun row -> row.xcr_count)
; count ~header:"Soon" (fun row -> row.soon_count)
; count ~header:"Someday" (fun row -> row.someday_count)
; count ~header:"Total" (fun row -> row.total_count)
]
;;
let to_text_table t =
if List.is_empty t.rows then None else Some (Text_table.make ~columns ~rows:t.rows)
;;