This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgogen.go
215 lines (176 loc) · 6.73 KB
/
gogen.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
package main
import (
"encoding/json"
"errors"
"fmt"
"gogen/data"
"gogen/exporter"
"gogen/test_fixtures"
"gogen/utilities"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/jessevdk/go-flags"
)
const VERSION = "0.2.13"
var defaultOpts struct{}
type runOpts struct {
OutputFolder string `long:"outputs" description:"The folder in which to place result files"`
DOJFiles string `long:"input-doj" description:"The files containing criminal histories from CA DOJ"`
County string `long:"county" short:"c" description:"The county for which eligibility will be computed"`
ComputeAt string `long:"compute-at" description:"The date for which eligibility will be evaluated, ex: 2020-10-31"`
EligibilityOptions string `long:"eligibility-options" description:"File containing options for which eligibility logic to apply"`
FileNameSuffix string `long:"file-name-suffix" hidden:"true" description:"string to append to file names"`
}
type exportTestCSVOpts struct {
ExcelFixturePath string `long:"excel-fixture-path" short:"e" description:"Path to a county's excel fixture file to generate test CSVs"`
OutputFolder string `long:"outputs" short:"o" description:"The folder in which to place result files"`
}
type versionOpts struct{}
var opts struct {
Version versionOpts `command:"version" description:"Print the version"`
Run runOpts `command:"run" description:"Process an input DOJ file and produce an annotated DOJ data file"`
ExportCSV exportTestCSVOpts `command:"export-test-csv" description:"Export example data files from excel fixtures"`
}
func (r runOpts) Execute(args []string) error {
var processingStartTime time.Time
utilities.SetErrorFileName(utilities.GenerateFileName(r.OutputFolder, "gogen%s.err", r.FileNameSuffix))
if r.OutputFolder == "" || r.DOJFiles == "" || r.County == "" || r.EligibilityOptions == "" {
utilities.ExitWithError(errors.New("missing required field: Run gogen --help for more info"))
}
inputFiles := strings.Split(r.DOJFiles, ",")
computeAtDate := time.Now()
if r.ComputeAt != "" {
computeAtOption, err := time.Parse("2006-01-02", r.ComputeAt)
if err != nil {
utilities.ExitWithError(errors.New("invalid --compute-at date: Must be a valid date in the format YYYY-MM-DD"))
} else {
computeAtDate = computeAtOption
}
}
var configurableEligibilityFlow data.ConfigurableEligibilityFlow
var options data.EligibilityOptions
optionsFile, err := os.Open(r.EligibilityOptions)
if err != nil {
utilities.ExitWithError(err)
}
defer optionsFile.Close()
optionsBytes, err := ioutil.ReadAll(optionsFile)
if err != nil {
utilities.ExitWithError(err)
}
err = json.Unmarshal(optionsBytes, &options)
if err != nil {
utilities.ExitWithError(err)
}
configurableEligibilityFlow, err = data.NewConfigurableEligibilityFlow(options, r.County)
if err != nil {
utilities.ExitWithError(err)
}
runErrors := make(map[string]utilities.GogenError)
var runSummary exporter.Summary
outputJsonFilePath := utilities.GenerateFileName(r.OutputFolder, "gogen%s.json", r.FileNameSuffix)
err = os.MkdirAll(r.OutputFolder, os.ModePerm)
if err != nil {
utilities.ExitWithError(err)
}
for fileIndex, inputFile := range inputFiles {
processingStartTime = time.Now()
fileIndex = fileIndex + 1
dojInformation, gogenErr := data.NewDOJInformation(inputFile, computeAtDate, configurableEligibilityFlow)
if gogenErr.ErrorType != "" {
runErrors[inputFile] = gogenErr
continue
}
countyEligibilities := dojInformation.DetermineEligibility(r.County, configurableEligibilityFlow)
dismissAllProp64Eligibilities := dojInformation.DetermineEligibility(r.County, data.EligibilityFlows["DISMISS ALL PROP 64"])
dismissAllProp64AndRelatedEligibilities := dojInformation.DetermineEligibility(r.County, data.EligibilityFlows["DISMISS ALL PROP 64 AND RELATED"])
dojFilePath := utilities.GenerateIndexedFileName(r.OutputFolder, "All_Results%s.csv", fileIndex, len(inputFiles), r.FileNameSuffix)
condensedFilePath := utilities.GenerateIndexedFileName(r.OutputFolder, "All_Results_Condensed%s.csv", fileIndex, len(inputFiles), r.FileNameSuffix)
prop64ConvictionsFilePath := utilities.GenerateIndexedFileName(r.OutputFolder, "Prop64_Results%s.csv", fileIndex, len(inputFiles), r.FileNameSuffix)
dojWriter, err := exporter.NewDOJWriter(dojFilePath)
if err != nil {
runErrors[inputFile] = utilities.GogenError{ErrorType: "OTHER", ErrorMessage: err.Error()}
continue
}
condensedDojWriter, err := exporter.NewCondensedDOJWriter(condensedFilePath)
if err != nil {
runErrors[inputFile] = utilities.GogenError{ErrorType: "OTHER", ErrorMessage: err.Error()}
continue
}
prop64ConvictionsDojWriter, err := exporter.NewDOJWriter(prop64ConvictionsFilePath)
if err != nil {
runErrors[inputFile] = utilities.GogenError{ErrorType: "OTHER", ErrorMessage: err.Error()}
continue
}
dataExporter := exporter.NewDataExporter(
dojInformation,
countyEligibilities,
dismissAllProp64Eligibilities,
dismissAllProp64AndRelatedEligibilities,
dojWriter,
condensedDojWriter,
prop64ConvictionsDojWriter)
fileSummary := dataExporter.Export(r.County, configurableEligibilityFlow)
runSummary = dataExporter.AccumulateSummaryData(runSummary, fileSummary)
}
if encounteredErrors(runErrors) {
utilities.ExitWithErrors(runErrors)
}
ExportSummary(runSummary, processingStartTime, outputJsonFilePath)
return nil
}
func encounteredErrors(runErrors map[string]utilities.GogenError) bool {
for _, value := range runErrors {
if value.ErrorType != "" {
return true
}
}
return false
}
func ExportSummary(summary exporter.Summary, startTime time.Time, filePath string) {
summary.ProcessingTimeInSeconds = time.Since(startTime).Seconds()
s, err := json.Marshal(summary)
if err != nil {
utilities.ExitWithError(err)
}
err = ioutil.WriteFile(filePath, s, 0644)
if err != nil {
utilities.ExitWithError(err)
}
}
func (e exportTestCSVOpts) Execute(args []string) error {
if e.ExcelFixturePath != "" {
inputCSV, expectedResultsCSV, err := test_fixtures.ExportFullCSVFixtures(e.ExcelFixturePath, e.OutputFolder)
if err != nil {
fmt.Println("Extracting test CSVs failed")
os.Exit(1)
}
fmt.Println("Wrote input CSV at: " + inputCSV)
fmt.Println("Wrote expected results CSV at: " + expectedResultsCSV)
openCommand := exec.Command("open", e.OutputFolder)
err = openCommand.Run()
if err != nil {
panic(err)
}
} else {
return errors.New("something went wrong")
}
return nil
}
func (v versionOpts) Execute(args []string) error {
fmt.Println(VERSION)
return nil
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
}