-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
142 lines (122 loc) · 2.69 KB
/
analyze.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
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"os"
"sort"
)
var cmdAnalyze = cobra.Command{
Use: "analyze",
Short: "Analyze dumps",
}
var analysisProgress bool
var cmdUserFrequency = cobra.Command{
Use: "user-frequency <file>",
Short: "Messages per user",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
runAnalysis(&analysisUserFrequency{
userById: make(map[string]miniUser),
msgCount: make(map[string]int64),
}, args[0])
},
}
var cmdBotRatio = cobra.Command{
Use: "bot-ratio <file>",
Short: "Human/bot ratio",
Args: cobra.ExactArgs(1),
Run: func(c *cobra.Command, args []string) {
runAnalysis(new(analyisBotRatio), args[0])
},
}
func init() {
pf := cmdAnalyze.PersistentFlags()
pf.BoolVar(&analysisProgress, "progress", false, "Print progress while scanning")
}
func runAnalysis(a analysis, fpath string) {
f, err := os.Open(fpath)
if err != nil {
panic(err)
}
defer f.Close()
scan := bufio.NewScanner(f)
for i := 1; scan.Scan(); i++ {
line := scan.Bytes()
var msg miniMsg
err = json.Unmarshal(line, &msg)
if err != nil {
fmt.Printf("Error: %s", err)
break
}
a.Add(&msg)
if analysisProgress && i%1000 == 0 {
short := a.Short()
if short != "" {
fmt.Printf("Scanning (%5d): %s\n", i, a.Short())
} else {
fmt.Printf("Scanning (%5d)\n", i)
}
}
}
fmt.Println(a.String())
}
type analysis interface {
Add(msg *miniMsg)
Short() string
String() string
}
type analyisBotRatio struct {
BotMsgs int64
HumanMsgs int64
}
func (a *analyisBotRatio) Add(msg *miniMsg) {
if msg.Author.Bot {
a.BotMsgs++
} else {
a.HumanMsgs++
}
}
func (a *analyisBotRatio) Short() string {
return a.String()
}
func (a *analyisBotRatio) String() string {
return fmt.Sprintf("%.4f (%6d By Bots : %6d By Humans)",
float64(a.BotMsgs)/(float64(a.BotMsgs)+float64(a.HumanMsgs)),
a.BotMsgs, a.HumanMsgs)
}
type analysisUserFrequency struct {
userById map[string]miniUser
msgCount map[string]int64
}
func (a *analysisUserFrequency) Add(msg *miniMsg) {
if _, ok := a.userById[msg.Author.ID]; !ok {
a.userById[msg.Author.ID] = msg.Author
}
a.msgCount[msg.Author.ID]++
}
func (a *analysisUserFrequency) Short() string {
return ""
}
func (a *analysisUserFrequency) String() string {
type row struct {
User miniUser `json:"user"`
Count int64 `json:"count"`
}
var rows []row
for id, count := range a.msgCount {
rows = append(rows, row{a.userById[id], count})
}
sort.Slice(rows, func(i, j int) bool {
return rows[i].Count > rows[j].Count
})
var buf bytes.Buffer
for _, r := range rows {
res, _ := json.Marshal(r)
buf.WriteByte('\n')
buf.Write(res)
}
return buf.String()
}