/
dbToCsvWorkset.go
334 lines (289 loc) · 8.5 KB
/
dbToCsvWorkset.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"database/sql"
"errors"
"os"
"path/filepath"
"strconv"
"time"
"github.com/openmpp/go/ompp/db"
"github.com/openmpp/go/ompp/helper"
"github.com/openmpp/go/ompp/omppLog"
)
// write all readonly workset data into csv files: input parameters
func toWorksetListCsv(
dbConn *sql.DB,
modelDef *db.ModelMeta,
outDir string,
fileCreated map[string]bool,
isUseIdNames bool,
isAllInOne bool) error {
// get all readonly worksets
wl, err := db.GetWorksetFullList(dbConn, modelDef.Model.ModelId, true, "")
if err != nil {
return err
}
// read all workset parameters and dump it into csv files
for k := range wl {
err := toWorksetCsv(
dbConn, modelDef, &wl[k], outDir, fileCreated, isUseIdNames, isAllInOne)
if err != nil {
return err
}
}
// write workset rows into csv
row := make([]string, 6)
idx := 0
err = toCsvFile(
outDir,
"workset_lst.csv",
[]string{"set_id", "base_run_id", "model_id", "set_name", "is_readonly", "update_dt"},
func() (bool, []string, error) {
if 0 <= idx && idx < len(wl) {
row[0] = strconv.Itoa(wl[idx].Set.SetId)
if wl[idx].Set.BaseRunId <= 0 { // non-positive run id is NULL
row[1] = "NULL"
} else {
row[1] = strconv.Itoa(wl[idx].Set.BaseRunId)
}
row[2] = strconv.Itoa(wl[idx].Set.ModelId)
row[3] = wl[idx].Set.Name
row[4] = strconv.FormatBool(wl[idx].Set.IsReadonly)
row[5] = wl[idx].Set.UpdateDateTime
idx++
return false, row, nil
}
return true, row, nil // end of workset rows
})
if err != nil {
return errors.New("failed to write worksets into csv " + err.Error())
}
// write workset text rows into csv
row = make([]string, 4)
idx = 0
j := 0
err = toCsvFile(
outDir,
"workset_txt.csv",
[]string{"set_id", "lang_code", "descr", "note"},
func() (bool, []string, error) {
if idx < 0 || idx >= len(wl) { // end of workset rows
return true, row, nil
}
// if end of current workset texts then find next workset with text rows
if j < 0 || j >= len(wl[idx].Txt) {
j = 0
for {
idx++
if idx < 0 || idx >= len(wl) { // end of workset rows
return true, row, nil
}
if len(wl[idx].Txt) > 0 {
break
}
}
}
// make workset text []string row
row[0] = strconv.Itoa(wl[idx].Txt[j].SetId)
row[1] = wl[idx].Txt[j].LangCode
row[2] = wl[idx].Txt[j].Descr
if wl[idx].Txt[j].Note == "" { // empty "" string is NULL
row[3] = "NULL"
} else {
row[3] = wl[idx].Txt[j].Note
}
j++
return false, row, nil
})
if err != nil {
return errors.New("failed to write worksets text into csv " + err.Error())
}
// write workset parameter rows into csv
row = make([]string, 4)
idx = 0
j = 0
err = toCsvFile(
outDir,
"workset_parameter.csv",
[]string{"set_id", "parameter_hid", "sub_count", "default_sub_id"},
func() (bool, []string, error) {
if idx < 0 || idx >= len(wl) { // end of workset rows
return true, row, nil
}
// if end of current workset parameters then find next workset with parameter rows
if j < 0 || j >= len(wl[idx].Param) {
j = 0
for {
idx++
if idx < 0 || idx >= len(wl) { // end of workset rows
return true, row, nil
}
if len(wl[idx].Param) > 0 {
break
}
}
}
// make workset parameter []string row
row[0] = strconv.Itoa(wl[idx].Set.SetId)
row[1] = strconv.Itoa(wl[idx].Param[j].ParamHid)
row[2] = strconv.Itoa(wl[idx].Param[j].SubCount)
row[3] = strconv.Itoa(wl[idx].Param[j].DefaultSubId)
j++
return false, row, nil
})
if err != nil {
return errors.New("failed to write workset parameters into csv " + err.Error())
}
// write workset parameter text (parameter value notes) rows into csv
row = make([]string, 4)
idx = 0
pix := 0
j = 0
err = toCsvFile(
outDir,
"workset_parameter_txt.csv",
[]string{"set_id", "parameter_hid", "lang_code", "note"},
func() (bool, []string, error) {
if idx < 0 || idx >= len(wl) { // end of workset rows
return true, row, nil
}
// if end of current workset parameter text then find next workset with parameter text rows
if pix < 0 || pix >= len(wl[idx].Param) || j < 0 || j >= len(wl[idx].Param[pix].Txt) {
j = 0
for {
if 0 <= pix && pix < len(wl[idx].Param) {
pix++
}
if pix < 0 || pix >= len(wl[idx].Param) {
idx++
pix = 0
}
if idx < 0 || idx >= len(wl) { // end of workset rows
return true, row, nil
}
if pix >= len(wl[idx].Param) { // end of parameter rows for that workset
continue
}
if len(wl[idx].Param[pix].Txt) > 0 {
break
}
}
}
// make workset parameter text []string row
row[0] = strconv.Itoa(wl[idx].Param[pix].Txt[j].SetId)
row[1] = strconv.Itoa(wl[idx].Param[pix].Txt[j].ParamHid)
row[2] = wl[idx].Param[pix].Txt[j].LangCode
if wl[idx].Param[pix].Txt[j].Note == "" { // empty "" string is NULL
row[3] = "NULL"
} else {
row[3] = wl[idx].Param[pix].Txt[j].Note
}
j++
return false, row, nil
})
if err != nil {
return errors.New("failed to write workset parameter text into csv " + err.Error())
}
return nil
}
// toWorksetCsv write workset paarameters into csv files, in separate subdirectory
func toWorksetCsv(
dbConn *sql.DB,
modelDef *db.ModelMeta,
meta *db.WorksetMeta,
outDir string,
fileCreated map[string]bool,
isUseIdNames bool,
isAllInOne bool) error {
// create workset subdir under output dir
setId := meta.Set.SetId
omppLog.Log("Workset ", setId, " ", meta.Set.Name)
// make output directory as one of:
// all_input_sets, set.Name_Of_the_Set, as set.NN.Name_Of_the_Set
var csvDir string
if isAllInOne {
csvDir = filepath.Join(outDir, "all_input_sets")
} else {
if !isUseIdNames {
csvDir = filepath.Join(outDir, "set."+helper.CleanPath(meta.Set.Name))
} else {
csvDir = filepath.Join(outDir, "set."+strconv.Itoa(setId)+"."+helper.CleanPath(meta.Set.Name))
}
}
if !isAllInOne && !theCfg.isKeepOutputDir {
if ok := dirDeleteAndLog(csvDir); !ok {
return errors.New("Error: unable to delete: " + csvDir)
}
}
if err := os.MkdirAll(csvDir, 0750); err != nil {
return err
}
// if this is "all-in-one" output then first column is set id or set name
var firstCol, firstVal string
if isAllInOne {
if theCfg.isIdCsv {
firstCol = "set_id"
firstVal = strconv.Itoa(setId)
} else {
firstCol = "set_name"
firstVal = meta.Set.Name
}
}
// write parameter into csv file
nP := len(meta.Param)
omppLog.Log(" Parameters: ", nP)
logT := time.Now().Unix()
for j := 0; j < nP; j++ {
idx, ok := modelDef.ParamByHid(meta.Param[j].ParamHid)
if !ok {
return errors.New("missing workset parameter Hid: " + strconv.Itoa(meta.Param[j].ParamHid) + " workset: " + strconv.Itoa(setId) + " " + meta.Set.Name)
}
cvtParam := &db.CellParamConverter{
ModelDef: modelDef,
Name: modelDef.Param[idx].Name,
IsIdCsv: theCfg.isIdCsv,
DoubleFmt: theCfg.doubleFmt,
}
paramLt := db.ReadParamLayout{
ReadLayout: db.ReadLayout{
Name: modelDef.Param[idx].Name,
FromId: setId,
},
IsFromSet: true,
}
logT = omppLog.LogIfTime(logT, logPeriod, " ", j, " of ", nP, ": ", paramLt.Name)
err := toCellCsvFile(dbConn, modelDef, paramLt, cvtParam, fileCreated, csvDir, firstCol, firstVal)
if err != nil {
return err
}
}
// write each workset parameter value notes into parameterName.LANG.md file
if !isAllInOne {
for j := range meta.Param {
paramName := ""
for i := range meta.Param[j].Txt {
if meta.Param[j].Txt[i].LangCode != "" && meta.Param[j].Txt[i].Note != "" {
// find parameter by name if this is a first note for that parameter
if paramName == "" {
k, ok := modelDef.ParamByHid(meta.Param[j].ParamHid)
if !ok {
return errors.New("parameter not found by Hid: " + strconv.Itoa(meta.Param[j].ParamHid))
}
paramName = modelDef.Param[k].Name
}
// write notes into parameterName.LANG.md file
err := toDotMdFile(
csvDir,
paramName+"."+meta.Param[j].Txt[i].LangCode,
meta.Param[j].Txt[i].Note)
if err != nil {
return err
}
}
}
}
}
return nil
}