-
Notifications
You must be signed in to change notification settings - Fork 0
/
processData.go
187 lines (160 loc) · 4.43 KB
/
processData.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
package main
import (
"bufio"
"encoding/json"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
// sorting method for UserData to find the most recent time entry
func (data *UserData) findMostRecent() int64 {
mostRecent := data.TimeVisit[0][0]
for _, i := range data.TimeVisit {
for _, j := range i {
if j > mostRecent {
mostRecent = j
}
}
}
return int64(mostRecent)
}
// processByDay processes UserData.TimeVisit and populates DailyMetrics struct
func (data *UserData) processByDay(configs Params) {
var total, unique, repeat int = 0, 0, 0
mostRecentEntry := time.Unix(data.findMostRecent(), 0)
query, err := strconv.Atoi(configs.Days)
// initialize struct with header - variadic function to unpack headers
data.Head = append(data.Head, []string{"day", "total", "unique", "repeat"}...)
if err != nil {
panic(err)
}
count := 0
// go through each day
for n := 0; n < query; n++ {
// start at the top and subtract a day
timeObject := mostRecentEntry.AddDate(0, 0, (-1 * n))
_, _, d := timeObject.Date()
// traverse the 2d array
// each list in list is specific to a unique user
// this must be done for each day in span created above
for _, j := range data.TimeVisit {
for _, k := range j {
// check if item in the list matches wrapping day
if time.Unix(int64(k), 0).Day() == d {
total++
unique++
repeat = FindDupesInArray(j, d)
} else {
continue
}
}
}
// anything over 1 visit means it is a repeat visit
if unique > 1 {
unique = unique - repeat
}
// append the data into the struct
temp := []int{int(timeObject.Unix()), total, unique, repeat}
data.Data = append(data.Data, temp)
total, repeat, unique = 0, 0, 0
count++
}
}
// FindDupesInArray returns the number of duplicate entries
// takes a list of unix times and specified day
func FindDupesInArray(array []int, day int) int {
count := 0
for i := 0; i < len(array); i++ {
if time.Unix(int64(array[i]), 0).Day() == day {
count++
}
}
return count
}
// readLines reads a whole file into memory and returns a list of strings
// containing each line.
func readLines(path string) ([]string, error) {
var lines []string
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// cleanDupes maps a key value pair of users and unixTime specific to each user
func processByTime(list *[]string) map[string][]int {
mapOfUsers := map[string][]int{}
for _, line := range *list {
entry := strings.Split(line, ",")
// entry must have an IP and time in order to be added to map
if len(entry) < 2 {
continue
} else {
// remove whitespace
entry[1] = strings.Join(strings.Fields(entry[1]), "")
// convert unix string to int
unixTime, err := strconv.Atoi(entry[1])
if err != nil {
panic(err)
}
// append the map: entry[0] = IP
// duplicates are appended to the key value entry[0]
mapOfUsers[entry[0]] = append(mapOfUsers[entry[0]], unixTime)
}
}
return mapOfUsers
}
// convertMapToStruct takes in a map object as an argument and returns a
// structure of type UserData
func convertMapToStruct(unstructData *map[string][]int) UserData {
//data := make(map[string]UserData)
var data UserData
count := 0
// iterate over the map and throw in struct
for _, value := range *unstructData {
data.Users = append(data.Users, strconv.Itoa(count))
data.TimeVisit = append(data.TimeVisit, value)
// add the length of each slice containing a time stamp to the count
count = count + len(value)
}
data.UniqueVisitors = len(data.Users)
data.HitCount = count
return data
}
func writeToFile(json *[]uint8, location string) {
err := ioutil.WriteFile(location, *json, 0644)
if err != nil {
panic(err)
}
}
func processFile() string {
var jsonString []byte
// load up config parameters
configs := getConfig()
// read in data
listOfUsers, err := readLines(configs.File)
if err != nil {
panic(err)
}
// filter data and create a map object
filteredList := processByTime(&listOfUsers)
// convert map into data structure
siteVisits := convertMapToStruct(&filteredList)
// process data using methods
siteVisits.processByDay(configs)
// encode structure into json format
jsonString, err = json.Marshal(siteVisits)
if err != nil {
panic(err)
}
// write to file
writeToFile(&jsonString, configs.Output)
return string(jsonString)
}