-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvconduit.go
More file actions
320 lines (273 loc) · 7.26 KB
/
Copy pathcsvconduit.go
File metadata and controls
320 lines (273 loc) · 7.26 KB
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
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
)
// CsvLine adds a line number to each input row's fields,
// so that logged output lines can be correlated to input
type CsvLine struct {
LineNumber int
Fields []string
}
type lcLead struct {
Id string
}
type lcResponse struct {
Outcome string
Reason string
Lead lcLead
Price float64
}
var csvlogfile *os.File
var lcSubmissionUrlCheck = regexp.MustCompile("/flows/[a-z0-9]{24}/sources/[a-z0-9]{24}")
var flowIdColumn = -1
var sourceIdColumn = -1
func initLog() {
// only init once
if csvlogfile == nil {
now := time.Now()
var err error
csvlogfile, err = os.Create(fmt.Sprintf("log_%s.csv", now.Format("0102_1504")))
if err != nil {
panic(err)
}
_, err = fmt.Fprintf(csvlogfile, "import_line_num,import_outcome,import_lead_id,import_reason\n")
if err != nil {
panic(err)
}
}
}
func csvlog(lineNumber int, outcome string, leadId, reason string) {
_, err := fmt.Fprintf(csvlogfile, "%d,%s,%s,%s\n", lineNumber, outcome, leadId, reason)
if err != nil {
panic(err)
}
}
func getFieldnames(rawRow []string) []string {
fieldnames := make([]string, len(rawRow))
for i, field := range rawRow {
fieldnames[i] = strings.ToLower(strings.ReplaceAll(field, " ", "_"))
// cache these column IDs, if found
if fieldnames[i] == "flow_id" {
flowIdColumn = i
} else if fieldnames[i] == "source_id" {
sourceIdColumn = i
}
}
return fieldnames
}
func isFullLcUrl(url string) bool {
return lcSubmissionUrlCheck.MatchString(url)
}
func getUrl(url string, record []string) string {
if isFullLcUrl(url) {
return url
}
if flowIdColumn >= 0 && sourceIdColumn >= 0 {
return fmt.Sprintf("%s/flows/%s/sources/%s/submit", url, record[flowIdColumn], record[sourceIdColumn])
} else {
log.Fatal("error: bad URL and no flow or source ID columns set")
return ""
}
}
func showPreview(serverUrl string, fieldnames []string, record []string, rowNum int) (proceedFlag int) {
fmt.Printf("posting URL: %s\n", getUrl(serverUrl, record))
fmt.Printf("preview of row #%d (note: empty values will not be posted)\n", rowNum)
// determine the longest field name, so we can center-align the preview
longestNameLength := 0
for _, name := range fieldnames {
if len(name) > longestNameLength {
longestNameLength = len(name)
}
}
for i, field := range record {
fmt.Printf(" %*s: %s\n", longestNameLength, fieldnames[i], field)
}
fmt.Print("\n")
reader := bufio.NewReader(os.Stdin)
fmt.Printf("proceed with posting 0, 1, or All remaining rows? (enter 0, 1, or A): ")
text, _ := reader.ReadString('\n')
text = strings.ToLower(strings.TrimSpace(text))
if text == "1" {
proceedFlag = 1
} else if text == "a" {
proceedFlag = 2 // 2 means "all"
} else {
// default to 0 (halt) on any other input
proceedFlag = 0
}
return proceedFlag
}
func post(serverUrl string, fieldnames []string, line CsvLine, showResponse bool) (outcome string) {
var leadId, reason string
values := url.Values{}
for i, field := range line.Fields {
if field != "" {
values.Set(fieldnames[i], field)
}
}
resp, err := http.PostForm(getUrl(serverUrl, line.Fields), values)
var body []byte
if err != nil {
outcome = "error"
reason = err.Error()
} else {
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
outcome = "error"
reason = err.Error()
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var lcr lcResponse
err = json.Unmarshal(body, &lcr)
if err != nil {
outcome = "error"
reason = err.Error()
} else {
outcome = lcr.Outcome
leadId = lcr.Lead.Id
reason = lcr.Reason
}
} else {
outcome = "error"
reason = fmt.Sprintf("%d: %s", resp.StatusCode, body)
}
}
if showResponse {
message := string(body)
if reason != "" {
message = reason
}
fmt.Printf("\n%s - %s\n\n", outcome, message)
}
csvlog(line.LineNumber, outcome, leadId, reason)
return
}
func main() {
// initialize command-line flags
var showHelp = flag.Bool("help", false, "show help & exit")
var threadCount = flag.Int("thread-count", 1, "number of threads (i.e., simultaneous posts); maximum: 20")
flag.Parse()
if *showHelp || *threadCount > 20 {
ShowHelp()
}
filename := ""
if len(flag.Args()) > 0 {
filename = flag.Arg(0)
} else {
ShowHelp()
}
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var serverUrl string
if len(flag.Args()) > 1 {
serverUrl = flag.Arg(1)
// make sure URL is valid
parsed, parseErr := url.Parse(serverUrl)
if parseErr != nil || parsed.Scheme == "" || parsed.Host == "" {
log.Fatalf("invalid URL: %q", serverUrl)
}
} else {
log.Fatal("specify URL to post to")
}
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
log.Fatal("error reading file: ", err)
}
fmt.Printf("read %d data rows\n", len(records)-1) //
fieldnames := getFieldnames(records[0])
// make sure either the URL has flow & source ids, or the fieldnames do
if !isFullLcUrl(serverUrl) && (flowIdColumn < 0 || sourceIdColumn < 0) {
log.Fatal("required submission info 'flow_id' and 'source_id' not found in URL or CSV")
}
proceedFlag := 1 // default to make showPreview run the first time, at least
var successes, failures, errors int
csvLines := make(chan CsvLine, *threadCount)
outcomes := make(chan string, *threadCount)
// crank up the goroutines that will post leads
for i := 0; i < *threadCount; i++ {
go func() {
for csvLine := range csvLines {
outcome := post(serverUrl, fieldnames, csvLine, false)
outcomes <- outcome
}
}()
}
var outcome string
numProcessing := 0
// start a single goroutine to read outcomes as they come in
go func() {
for outcome := range outcomes {
// keep score & show progress to stdout
switch outcome {
case "success":
successes++
fmt.Print(".")
case "failure":
failures++
fmt.Print("f")
case "error":
errors++
fmt.Print("e")
}
numProcessing--
}
}()
for i, dataRow := range records[1:] {
// show preview 1st time and when user has selected to proceed with 1 row
if proceedFlag == 1 {
proceedFlag = showPreview(serverUrl, fieldnames, dataRow, i+1)
if proceedFlag == 0 {
break
}
// this only needs to happen the first time through, but
// we only want to create the log if something will be posted
// (calling it more than once doesn't hurt anything)
initLog()
thisLine := CsvLine{i + 1, dataRow}
outcome = post(serverUrl, fieldnames, thisLine, proceedFlag == 1)
switch outcome {
case "success":
successes++
fmt.Println("Submission succeeded")
case "failure":
failures++
fmt.Println("Submission failed")
case "error":
errors++
fmt.Println("Submission errored")
}
} else {
// proceed must be 2 ("all"), so feed all the rest into the channel
numProcessing++
csvLines <- CsvLine{i + 1, dataRow}
}
}
// wait until everything's done
for numProcessing > 0 {
time.Sleep(10 * time.Millisecond)
}
logfileMsg := ""
if csvlogfile != nil {
logfileMsg = fmt.Sprintf("(see %s)", csvlogfile.Name())
}
fmt.Printf("\nfinished: %d successes, %d failures, %d errors %s\n",
successes, failures, errors, logfileMsg)
csvlogfile.Close()
}