-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
310 lines (238 loc) · 6.92 KB
/
sequence.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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"strings"
//"sync"
"time"
)
var skillMap = map[string]string{
"NMAAHC": "Main Menu",
"NMAAHC 10": "Advance Passes",
"NMAAHC 11": "Same Day/Walk Up Passes",
"NMAAHC 12": "Cancel/Exchange Passes",
"NMAAHC TICKET": "Obtain Passes Over Phone",
"NMAAHC XFER VALID": "Valid Transfer",
"NMAAHC SHOP": "Museum Shop",
"NMAAHC CAFE": "Sweet Home Cafe",
"NMAAHC 14": "New Group Reservation",
"NMAAHC BusyNoAnswer": "Network Error/Busy",
"NMAAHC NetInvalidNum": "Network Error/Busy",
"NMAAHC 18": "Cancel Group Reservation",
"NMAAHC MEM RENEW": "Donations, Gifts, New/Renew Membership",
"NMAAHC MEM QUESTION": "Questions About Membership",
"NMAAHC 17": "Donate Object or Collection",
"NMAAHC 2": "Timed Entry Passes",
"NMAAHC 3": "Group Reservations",
"NMAAHC 4": "Membership & Donations",
"NMAAHC 5": "Location & Hours",
"NMAAHC 6": "Transportation & Parking",
"NMAAHC 7": "Tours",
"NMAAHC 8": "Accessibility",
"NMAAHC 9": "Sweet Home Cafe and the Museum Shop",
}
var TICKET string = "Wednesday"
var iID, iMID, iTIME, iCAMP, iCONTACT, iSKILL, iDATE int
type entry struct {
assigned int
values []string
}
type sequence struct {
seqID int
entries map[int]entry
}
type sequenceList struct {
index int
sequences map[int]sequence
}
func (seqList *sequenceList) addSequence(seq *sequence) {
seqList.sequences[seqList.index] = *seq
seq.seqID = seqList.index
seqList.index++
}
func (seq *sequence) addEntry(id int, ent *entry) {
ent.assigned = seq.seqID
seq.entries[id] = *ent
}
func (seq *sequence) merge(seq2 *sequence) {
for id, ent := range seq2.entries {
if _, ok := seq.entries[id]; !ok {
seq.addEntry(id, &ent)
}
}
}
func (ent *entry) forTrace(entries map[int]*entry) (*sequence, error) {
// if it is the beginning of a sequence, then add start the sequence with ent and delete it from entries
if id, err := strconv.Atoi(ent.values[iID]); err != nil {
return &sequence{}, err
} else {
seq := &sequence{}
seq.entries = make(map[int]entry)
seq.addEntry(id, ent)
defer delete(entries, id)
for _, spawn := range entries {
if spawn.assigned == -1 {
if bytes.Equal([]byte(ent.values[iID]), []byte(spawn.values[iMID])) {
if seq2, err := spawn.forTrace(entries); err != nil {
return seq, err
} else {
seq.merge(seq2)
}
}
}
}
return seq, nil
}
}
func (seq *sequence) xferReason() string {
for _, ent := range seq.entries {
if ent.values[iSKILL] == "NMAAHC TICKET" {
return "TICKETING"
} else if ent.values[iSKILL] == "NMAAHC MEM RENEW" ||
ent.values[iSKILL] == "NMAAHC MEM QUESTION" {
return "MEMBERSHIP"
}
}
return ""
}
func isTDay(start_date time.Time) bool {
start_year, start_month, _ := start_date.Date()
month_start := time.Date(start_year, start_month, 1, 1, 1, 1, 1, time.UTC)
for {
if month_start.Weekday().String() == TICKET {
break
} else {
month_start = month_start.Add(time.Hour * 24)
}
}
return start_date.Day() == month_start.Day()
}
func main() {
sFile := "Data.csv"
fileStr, err := ioutil.ReadFile(sFile)
if err != nil {
fmt.Println(err)
}
str := string(fileStr)
entries := make(map[int]*entry)
seqList := sequenceList{0, make(map[int]sequence)}
r := csv.NewReader(strings.NewReader(str))
headers, err := r.Read()
headers = append(headers, "S_ID", "TIME_30", "TIME_15", "CALL_SUM", "AREA", "WEEKDAY", "T_DAY")
if err != nil {
fmt.Println(err)
}
for i, sHeader := range headers {
if sHeader == "contact_id" {
iID = i
} else if sHeader == "master_contact_id" {
iMID = i
} else if sHeader == "start_time" {
iTIME = i
} else if sHeader == "campaign_name" {
iCAMP = i
} else if sHeader == "skill_name" {
iSKILL = i
} else if sHeader == "contact_name" {
iCONTACT = i
} else if sHeader == "start_date" {
iDATE = i
}
}
for {
record, err := r.Read()
if err != nil {
break
}
// make each entry have an initial assigned = -1
if record[iCAMP] == "NMAAHC" {
if id, err := strconv.Atoi(record[iID]); err != nil {
fmt.Println(err)
} else {
entries[id] = &entry{-1, record}
}
}
}
//var waitGroup sync.WaitGroup
for _, ent := range entries {
if ent.assigned == -1 {
if bytes.Equal([]byte(ent.values[iID]), []byte(ent.values[iMID])) {
if seq, err := ent.forTrace(entries); err != nil {
fmt.Println(err)
} else {
seqList.addSequence(seq)
}
}
}
}
//waitGroup.Wait()
file, err := os.Create("callSequence.csv")
w := csv.NewWriter(file)
if err := w.Write(headers); err != nil {
log.Fatalln(err)
}
for sID, seq := range seqList.sequences {
var index int = 1
for _, ent := range seq.entries {
timeStamp, err := time.Parse("15:04:05", ent.values[iTIME])
if err != nil {
fmt.Println(err)
}
var intervals [2]string
var iMin [2]int = [2]int{int(math.Floor(float64(timeStamp.Minute())/30.0) * 30),
int(math.Floor(float64(timeStamp.Minute())/15.0) * 15)}
for i := range intervals {
if timeStamp.Hour() < 10 {
intervals[i] += "0" + strconv.Itoa(timeStamp.Hour())
} else {
intervals[i] += strconv.Itoa(timeStamp.Hour())
}
if iMin[i] < 10 {
intervals[i] += ":0" + strconv.Itoa(iMin[i])
} else {
intervals[i] += ":" + strconv.Itoa(iMin[i])
}
intervals[i] += ":00"
}
if ent.values[iCONTACT] == "Outbound" {
ent.values[iCONTACT] = func(s *sequence) string {
for _, e := range s.entries {
if e.values[iCONTACT] != "Outbound" {
return e.values[iCONTACT]
}
}
return ""
}(&seq)
}
startDate, err := time.Parse("01/02/2006", ent.values[iDATE])
if err != nil {
fmt.Println(err)
}
var AREA string
if ent.values[iSKILL] == "NMAAHC XFER VALID" ||
ent.values[iSKILL] == "NMAAHC BusyNoAnswer" ||
ent.values[iSKILL] == "NMAAHC NetInvalidNum" {
AREA = seq.xferReason() + " " + skillMap[ent.values[iSKILL]]
} else {
AREA = skillMap[ent.values[iSKILL]]
}
ent.values = append(ent.values, // Array being appended to
strconv.Itoa(sID), intervals[0], intervals[1], strconv.Itoa(index), // Sequence ID, TIME_30, TIME_15
AREA, startDate.Weekday().String(), strconv.FormatBool(isTDay(startDate))) // Call sum, AREA, WEEKDAY, T_DAY
if err := w.Write(ent.values); err != nil {
log.Fatalln(err)
}
index = 0
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}