-
Notifications
You must be signed in to change notification settings - Fork 20
/
event_statistics.go
56 lines (43 loc) 路 1.11 KB
/
event_statistics.go
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
package html
import (
"io"
"sort"
"github.com/elliotchance/gedcom/v39"
"github.com/elliotchance/gedcom/v39/html/core"
)
type EventStatistics struct {
document *gedcom.Document
}
func NewEventStatistics(document *gedcom.Document) *EventStatistics {
return &EventStatistics{
document: document,
}
}
func (c *EventStatistics) WriteHTMLTo(w io.Writer) (int64, error) {
counts := map[string]int{}
for _, individual := range c.document.Individuals() {
for _, event := range individual.AllEvents() {
counts[event.Tag().String()] += 1
}
}
total := 0
for _, count := range counts {
total += count
}
rows := []core.Component{
core.NewKeyedTableRow("Total", core.NewNumber(total), true),
}
keys := []string{}
for name := range counts {
keys = append(keys, name)
}
sort.Strings(keys)
for _, name := range keys {
number := core.NewNumber(counts[name])
tableRow := core.NewKeyedTableRow(name, number, true)
rows = append(rows, tableRow)
}
table := core.NewTable("", core.NewComponents(rows...))
return core.NewCard(core.NewText("Events"), core.CardNoBadgeCount, table).
WriteHTMLTo(w)
}