-
Notifications
You must be signed in to change notification settings - Fork 5
/
analyzer.go
114 lines (86 loc) · 1.75 KB
/
analyzer.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
package main
import (
"bufio"
"log"
"os"
"sync"
"time"
)
// IPRecord test
type IPRecord struct {
Hits int64
Elapsed []float64
}
// TopMutex is..
var TopMutex = &sync.Mutex{}
// TopIP is a counter of IP clients
var TopIP map[string]*IPRecord
// AnalyzerQueue Queue of files to analyce
var AnalyzerQueue chan *FileLog
// AnalyzerDone Signal on complete files download
var AnalyzerDone chan bool
var start time.Time
var end time.Time
var wg sync.WaitGroup
// AnalyzerDispatch starts the go routine to analyce each record
func AnalyzerDispatch(vStart time.Time, vEnd time.Time) {
start = vStart
end = vEnd
TopIP = make(map[string]*IPRecord)
AnalyzerQueue = make(chan *FileLog, 10)
wg.Add(1)
go analyzerReader()
}
// AnalyzerFinished control the end of the analyce process
func AnalyzerFinished() {
defer log.Printf("Finish the analycis")
log.Println("Waiting for finish")
wg.Wait()
}
func analyzerReader() {
defer wg.Done()
for {
select {
case f, more := <-AnalyzerQueue:
if !more {
return
}
wg.Add(1)
go analyzerFile(f)
}
}
}
func analyzerFile(f *FileLog) {
defer wg.Done()
log.Printf("Analyzing %s", f.Filename)
count := 0
file, err := os.Open(f.Filename)
if err != nil {
log.Fatal(err)
}
defer func() {
file.Close()
log.Printf("%s - %d lines where processed", f.Filename, count)
}()
f.Pointer = NewFilePointer(f.Filename)
reader := bufio.NewReader(file)
pos := int64(0)
for {
l, err := reader.ReadString(byte('\n'))
if err != nil {
return
}
line := NewLineLog(l, f, pos)
pos += int64(len(l))
if InTimeSpan(start, end, line.Time) {
RecordAdd(line)
if analyze {
RecordIP(line.IPClient, 1, line.Elapsed)
}
if err != nil {
log.Fatalf("Error: %v", err)
}
}
count++
}
}