-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
342 lines (281 loc) · 8.24 KB
/
main.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"time"
)
type arrayOfFilters []string
func (i *arrayOfFilters) String() string {
return "my string representation"
}
func (i *arrayOfFilters) Set(value string) error {
*i = append(*i, value)
return nil
}
// types
type visitors struct {
new int
old int
}
type uniqueVisitors map[string]struct{}
type recurringVisitors struct {
curDay map[string]struct{}
curMonth map[string]struct{}
curYear map[string]struct{}
}
func (v *recurringVisitors) AddVisitor(ip string) {
if v.curDay == nil {
v.curDay = make(map[string]struct{})
}
if v.curMonth == nil {
v.curMonth = make(map[string]struct{})
}
if v.curYear == nil {
v.curYear = make(map[string]struct{})
}
if _, exists := v.curDay[ip]; !exists {
v.curDay[ip] = struct{}{}
}
if _, exists := v.curMonth[ip]; !exists {
v.curMonth[ip] = struct{}{}
}
if _, exists := v.curYear[ip]; !exists {
v.curYear[ip] = struct{}{}
}
}
func (v visitors) String() string {
return fmt.Sprintf("New: %d / Old: %d", v.new, v.old)
}
type dailyVisitors struct {
visitors *visitors
}
type monthlyVisitors struct {
visitors *visitors
daily map[int]*dailyVisitors
}
type yearlyVisitors struct {
visitors *visitors
monthly map[int]*monthlyVisitors
}
type allVisitors struct {
visitors *visitors
yearly map[int]*yearlyVisitors
}
func (all *allVisitors) InitDate(t time.Time) {
all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()] = new(dailyVisitors)
all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()].visitors = new(visitors)
}
func (all *allVisitors) InitMonth(t time.Time) {
all.yearly[t.Year()].monthly[int(t.Month())] = new(monthlyVisitors)
all.yearly[t.Year()].monthly[int(t.Month())].daily = make(map[int]*dailyVisitors)
all.yearly[t.Year()].monthly[int(t.Month())].visitors = new(visitors)
}
func (all *allVisitors) InitYear(t time.Time) {
all.yearly[t.Year()] = new(yearlyVisitors)
all.yearly[t.Year()].monthly = make(map[int]*monthlyVisitors)
all.yearly[t.Year()].visitors = new(visitors)
}
func (all *allVisitors) AddUniqueVisitor(t time.Time) {
all.yearly[t.Year()].visitors.new++
all.yearly[t.Year()].monthly[int(t.Month())].visitors.new++
all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()].visitors.new++
all.visitors.new++
}
func (all allVisitors) GetVisitorsFrom(date string) (error, visitors) {
t, err := time.Parse("02/01/2006", date)
if err != nil {
t, err := time.Parse("01/2006", date)
if err != nil {
t, err := time.Parse("2006", date)
if err != nil {
return err, visitors{}
}
if _, exists := all.yearly[t.Year()]; !exists {
return nil, visitors{}
} else {
return nil, visitors{new: all.yearly[t.Year()].visitors.new, old: all.yearly[t.Year()].visitors.old}
}
}
if _, exists := all.yearly[t.Year()].monthly[int(t.Month())]; !exists {
return nil, visitors{}
} else {
return nil, visitors{new: all.yearly[t.Year()].monthly[int(t.Month())].visitors.new, old: all.yearly[t.Year()].monthly[int(t.Month())].visitors.old}
}
}
if _, exists := all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()]; !exists {
return nil, visitors{}
} else {
return nil, visitors{new: all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()].visitors.new, old: all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()].visitors.old}
}
}
func (all allVisitors) ShowMonthlyVisitors() {
months := make([]int, 0)
for year, yearly := range all.yearly {
if year < 1970 {
continue
}
fmt.Printf("Year: %d\n", year)
for month := range yearly.monthly {
months = append(months, month)
}
sort.Ints(months)
for _, k := range months {
fmt.Printf("Month: %d\n", k)
fmt.Printf("new: %d / old: %d\n", yearly.monthly[k].visitors.new, yearly.monthly[k].visitors.old)
}
months = nil
}
}
func (all allVisitors) ExportMonthlyVisitors() {
// export data to stdout in tsv format
months := make([]int, 0)
years := make([]int, 0)
f := os.Stdout
writer := bufio.NewWriter(f)
for year := range all.yearly {
years = append(years, year)
}
sort.Ints(years)
fmt.Fprintln(writer, "month new old")
for _, year := range years {
if year < 1970 {
continue
}
for month := range all.yearly[year].monthly {
months = append(months, month)
}
sort.Ints(months)
for _, k := range months {
fmt.Fprintf(writer, "%d/%d %d %d\n", year, k, all.yearly[year].monthly[k].visitors.new, all.yearly[year].monthly[k].visitors.old)
}
months = nil
}
writer.Flush()
}
func isCrawler(line string, bannedIps *[]string) bool {
crawlers := []string{"YandexBot", "SoftDev", "UptimeRobot", "Nessus", "GoogleBot"}
for _, s := range crawlers {
if strings.Contains(line, s) {
*bannedIps = append(*bannedIps, line)
return true
}
}
return false
}
func filterLogRow(row string, filters arrayOfFilters) bool {
if len(filters) == 0 {
return true
}
for _, filter := range filters {
if strings.Contains(row, filter) {
return true
}
}
return false
}
func readLogFile(logfile string, all *allVisitors, unique uniqueVisitors, recurring *recurringVisitors, filters arrayOfFilters) {
file, err := os.Open(logfile)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var line, ipAddr string
var splitLine []string
// save crawler ips if needed later
var bannedIps []string
// keep the day/month/year which are being parsed in memory
var lastDay, lastMonth, lastYear int
for scanner.Scan() {
line = scanner.Text()
if isCrawler(line, &bannedIps) {
continue
}
if !filterLogRow(line, filters) {
continue
}
splitLine = strings.Split(line, " ")
if len(splitLine) > 2 {
ipAddr = splitLine[0]
t, err := time.Parse(
"02/Jan/2006:15:04:05",
strings.TrimLeft(strings.Split(line, " ")[3], "["),
)
if err != nil {
//fmt.Println(err)
//fmt.Printf("%v\n", splitLine)
}
// add recurring users on daily/monthly/yearly basis
if _, exists := all.yearly[t.Year()]; !exists {
all.InitYear(t)
// save old users from last year
if lastYear != 0 {
if _, exists := all.yearly[lastYear]; exists {
all.yearly[lastYear].visitors.old = len(recurring.curYear)
}
}
recurring.curYear = nil
}
if _, exists := all.yearly[t.Year()].monthly[int(t.Month())]; !exists {
all.InitMonth(t)
// save old users from last month
if lastMonth != 0 && lastYear != 0 {
if _, exists := all.yearly[lastYear].monthly[lastMonth]; exists {
all.yearly[lastYear].monthly[lastMonth].visitors.old = len(recurring.curMonth)
}
}
recurring.curMonth = nil
}
if _, exists := all.yearly[t.Year()].monthly[int(t.Month())].daily[t.Day()]; !exists {
all.InitDate(t)
// save old users from last day
if lastMonth != 0 && lastYear != 0 && lastDay != 0 {
if _, exists := all.yearly[lastYear].monthly[lastMonth].daily[lastDay]; exists {
all.yearly[lastYear].monthly[lastMonth].daily[lastDay].visitors.old = len(recurring.curDay)
}
}
recurring.curDay = nil
}
if _, exists := unique[ipAddr]; !exists {
// increment unique visitors
unique[ipAddr] = struct{}{}
all.AddUniqueVisitor(t)
} else {
recurring.AddVisitor(ipAddr)
}
lastDay = t.Day()
lastMonth = int(t.Month())
lastYear = t.Year()
} else {
fmt.Printf("Log entry missing data: %v", splitLine)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func main() {
// Check if there are command-line arguments
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <log.file.1> <log.file.2> <log.file.n>\n\nNOTE! The program logic assumes that the log files are sorted by date in ascending order.")
return
}
var filters arrayOfFilters
flag.Var(&filters, "filter", "Filter all other API requests except the one in filter flag.")
flag.Parse()
// datastructure to hold visitor data
allVisitors := allVisitors{visitors: new(visitors), yearly: make(map[int]*yearlyVisitors)}
// unique ip addresses (with map its possible to make quick checks of existing ips)
uniqueVisitors := make(map[string]struct{})
// keep track of the old visitors on daily/monthly/yearly basis
var recurring recurringVisitors
for _, arg := range flag.Args() {
readLogFile(arg, &allVisitors, uniqueVisitors, &recurring, filters)
}
allVisitors.ExportMonthlyVisitors()
}