-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (103 loc) · 2.21 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
package main
import (
"bufio"
"container/heap"
"flag"
"fmt"
"io"
"log"
"os"
"regexp"
)
type kv struct {
Key string
Value int
}
// KVHeap https://golang.org/pkg/container/heap/
type KVHeap []kv
func (h KVHeap) Less(i, j int) bool {
return h[i].Value > h[j].Value
}
func (h KVHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h KVHeap) Len() int {
return len(h)
}
// Push on the heap
func (h *KVHeap) Push(x interface{}) {
*h = append(*h, x.(kv))
}
// Pop off the heap
func (h *KVHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func getOrderedHeap(m map[string]int) *KVHeap {
h := &KVHeap{}
heap.Init(h)
for k, v := range m {
heap.Push(h, kv{k, v})
}
return h
}
func processLine(currentLine string) (ip string, uri string) {
logFormatRegexp, err := regexp.Compile("^([0-9/.]+) [^ ]+ [^ ]+ (.*) \"GET (http://example.net)?([^ ]+) .*\"")
if err != nil {
log.Fatal(err)
}
matches := logFormatRegexp.FindStringSubmatch(currentLine)
if len(matches) > 0 {
return matches[1], matches[4]
}
return
}
func incrementMapEntry(m map[string]int, entry string) {
mapEntry := m[entry]
if mapEntry == 0 {
m[entry] = 1
} else {
m[entry] = mapEntry + 1
}
}
func scanLogFile(handle io.Reader) (map[string]int, map[string]int) {
ipList := make(map[string]int)
urlList := make(map[string]int)
scanner := bufio.NewScanner(handle)
for scanner.Scan() {
ip, url := processLine(scanner.Text())
incrementMapEntry(ipList, ip)
incrementMapEntry(urlList, url)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return ipList, urlList
}
func main() {
fptr := flag.String("f", "programming-task-example-data.log", "File to read logs from")
flag.Parse()
fileName := *fptr
handle, err := os.Open(string(fileName))
if err != nil {
log.Fatal(err)
}
defer handle.Close()
ipList, urlList := scanLogFile(handle)
ipListHeap := getOrderedHeap(ipList)
urlListHeap := getOrderedHeap(urlList)
// Output results
fmt.Println("Number of IP's:")
fmt.Println(len(ipList))
fmt.Println("Top 3 IP's:")
for i := 1; i <= 3; i++ {
fmt.Println(heap.Pop(ipListHeap))
}
fmt.Println("Top 3 URL's")
for i := 1; i <= 3; i++ {
fmt.Println(heap.Pop(urlListHeap))
}
}