-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (197 loc) · 5.77 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
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
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"io"
"os"
s "strings"
log "github.com/sirupsen/logrus"
)
var (
rowFieldIndices = new(fieldIndices)
noEmailCount = 0
csvFileIndex = 0
recordsForImportCount = 0
outputDir = "csv-output"
studentMap = make(map[string]*student)
studentList []*student
srcVersion = "2017.10.01"
)
func main() {
logLevel := flag.String("l", "f", "Logging level: valid values: p (panic), f (fatal), e (error), w (warn), i (info), d (debug)")
help := flag.Bool("h", false, "Help: Show this message")
version := flag.Bool("v", false, "Version: Show the version of this program")
flag.Parse()
if *help {
usage(0)
}
if *version {
fmt.Fprintln(os.Stdout, "\n\tVersion", srcVersion)
os.Exit(0)
}
setup(*logLevel)
var inputFileName string
if len(flag.Args()) < 1 {
fmt.Println("\n\tYou must enter a file name to convert.")
fmt.Println("")
usage(1)
} else {
inputFileName = flag.Args()[0]
}
ingestFile(&inputFileName)
rooms := makeRoomMap()
writeRoomCSVFiles(rooms)
makePdf()
logFin()
fmt.Printf("\nFinished successfully processing %v out of %v rows for CSV import.\n", recordsForImportCount, (csvFileIndex - 1))
fmt.Printf("(%d rows did not have e-mail addresses.)\n\n", noEmailCount)
fmt.Println("Your file has been converted to multiple CSV files for import into my-pta.")
fmt.Printf("You will find all of the files in a folder named '%v' in this current folder.\n\n", outputDir)
fmt.Printf("Total number of students: %d\n", len(studentMap))
}
func setup(logLevel string) {
log.SetLevel(loggerMap[logLevel])
rowFieldIndices.studentName = 0
rowFieldIndices.teacher = 1
rowFieldIndices.room = 2
rowFieldIndices.primaryPhone = 4
rowFieldIndices.streetAddress = 5
rowFieldIndices.city = 6
rowFieldIndices.zip = 7
rowFieldIndices.grade = 8
rowFieldIndices.parentType = 11
rowFieldIndices.parentName = 12
rowFieldIndices.parentEmail = 16
rowFieldIndices.parentEmailAlt = 10
}
func ingestFile(inputFileName *string) {
dir := "./"
file, err := os.Open(dir + *inputFileName)
check(err)
reader := csv.NewReader(bufio.NewReader(file))
for {
row, errRead := reader.Read()
if errRead == io.EOF {
break
}
if csvFileIndex == 0 {
// header
csvFileIndex++
continue
}
processRow(&row)
csvFileIndex++
}
}
func processRow(row *[]string) {
parPtr := resolveParent(row)
stuPtr := resolveStudent(row)
stuPtr.parents = append(stuPtr.parents, parPtr)
logRow(row, stuPtr)
}
func makeRoomMap() *roomMap {
rooms := make(roomMap)
for _, student := range studentMap {
key := student.gradeVal() + "-" + student.room
for _, parent := range student.parents {
if parent.hasEmailError() {
discardParentFromImport(parent, student)
} else {
rooms.add(key, []string{parent.name.first, parent.name.last, parent.email, student.room, student.gradeVal(), student.name.first, student.name.last})
recordsForImportCount++
}
}
}
return &rooms
}
func writeRoomCSVFiles(rooms *roomMap) {
os.Mkdir(outputDir, 0755)
header := []string{"FirstName", "LastName", "email", "room", "grade", "StuFn", "StuLn"}
for gradeRoom, parents := range *rooms {
gradeRoomSplitIdx := s.Index(gradeRoom, "-")
// key is grade-room, split these out
grade := gradeRoom[0:gradeRoomSplitIdx]
room := gradeRoom[gradeRoomSplitIdx+1:]
fileName := "grade" + grade + "-room" + room + ".csv"
file, err := os.Create(outputDir + "/" + fileName)
check(err)
writer := csv.NewWriter(bufio.NewWriter(file))
if err := writer.Write(header); err != nil {
log.Fatalf("error writing header to csv file '%v': %v\n", fileName, err)
}
if err := writer.WriteAll(parents); err != nil {
log.Fatalf("error writing parents to csv file '%v': %v\n", fileName, err)
}
writer.Flush()
if err := writer.Error(); err != nil {
log.Fatal(err)
}
}
}
func logRow(row *[]string, stud *student) {
log.Debugf("---------- row #%v --------", csvFileIndex)
log.WithFields(log.Fields{
"count": len(*row),
}).Debug("# columns")
log.WithFields(log.Fields{
"row": *row,
}).Debug("RAW ROW:")
rowFields := make(log.Fields, len(*row))
i := 0
for value := range *row {
// this simply makes a field label with 2-digit, 0-padded name
rowFields["f"+fmt.Sprintf("%02d", i)] = (*row)[value]
i++
}
log.WithFields(rowFields).Debug("Row fields:")
log.WithFields(log.Fields{
"student": (*stud).String(),
}).Debug("STUDENT + PARENTS:")
}
func logFin() {
if log.GetLevel() == log.DebugLevel {
for k, stu := range studentMap {
fmt.Printf("STUDENT: %s-> %s\n", k, stu)
fmt.Printf("\t parent len: %d\n", len(stu.parents))
for _, par := range stu.parents {
fmt.Printf("\t PARENT: %s\n", par.String())
}
}
}
log.WithFields(log.Fields{
"csvRecords": csvFileIndex,
"processedRecordsForImport": recordsForImportCount,
}).Infof("Finished successfully processing %v out of %v rows.\n", recordsForImportCount, csvFileIndex)
log.WithFields(log.Fields{
"count": noEmailCount,
}).Info("Rows with no email")
}
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func discardParentFromImport(par *parent, stu *student) {
log.WithFields(log.Fields{
"parent": (*par).String(),
"student": (*stu).String(),
}).Errorf("DISCARDING PARENT FROM CSV IMPORT; NO EMAIL ADDRESS")
noEmailCount++
}
func msgFromImportError(err error) (int, string) {
if rie, ok := err.(*recordImportError); ok {
return rie.cause, rie.msg
}
return -1, ""
}
func usage(exitCode int) {
fmt.Fprintf(os.Stderr, "\nUsage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n\t%s filename.csv", os.Args[0])
fmt.Fprintln(os.Stderr, "\n\nwhere filename.csv is the input file")
fmt.Fprintln(os.Stderr, "\noptionally, you may specify these flags")
fmt.Println("")
flag.PrintDefaults()
os.Exit(exitCode)
}