-
Notifications
You must be signed in to change notification settings - Fork 4
/
outputconfigs.go
343 lines (320 loc) · 9.89 KB
/
outputconfigs.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
335
336
337
338
339
340
341
342
343
package dbs
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"time"
"unsafe"
"github.com/dmwm/dbs2go/utils"
)
// OutputConfigs DBS API
func (a *API) OutputConfigs() error {
var args []interface{}
var conds []string
tmpl := make(Record)
tmpl["Owner"] = DBOWNER
bid := "0"
blockID := getValues(a.Params, "block_id")
if len(blockID) > 1 {
msg := "The outputconfigs API does not support list of block_id"
return Error(InvalidParamErr, ParametersErrorCode, msg, "dbs.outputconfigs.OutputConfigs")
} else if len(blockID) == 1 {
_, bid = OperatorValue(blockID[0])
}
if bid == "0" {
tmpl["Main"] = true
dataset := getValues(a.Params, "dataset")
if len(dataset) == 1 {
tmpl["Dataset"] = true
conds, args = AddParam("dataset", "DS.DATASET", a.Params, conds, args)
}
lfn := getValues(a.Params, "logical_file_name")
if len(lfn) == 1 {
tmpl["Lfn"] = true
conds, args = AddParam("logical_file_name", "FS.LOGICAL_FILE_NAME", a.Params, conds, args)
}
conds, args = AddParam("app_name", "A.APP_NAME", a.Params, conds, args)
conds, args = AddParam("release_version", "R.RELEASE_VERSION", a.Params, conds, args)
conds, args = AddParam("pset_hash", "P.PSET_HASH", a.Params, conds, args)
conds, args = AddParam("output_module_label", "O.OUTPUT_MODULE_LABEL", a.Params, conds, args)
conds, args = AddParam("global_tag", "O.GLOBAL_TAG", a.Params, conds, args)
} else {
tmpl["Main"] = false
}
stm, err := LoadTemplateSQL("outputconfigs", tmpl)
if err != nil {
return Error(err, LoadErrorCode, "", "dbs.outputconfigs.OutputConfigs")
}
stm = WhereClause(stm, conds)
// use generic query API to fetch the results from DB
err = executeAll(a.Writer, a.Separator, stm, args...)
if err != nil {
return Error(err, QueryErrorCode, "", "dbs.outputconfigs.OutputConfigs")
}
return nil
}
// OutputConfigs represents Output Configs DBS DB table
type OutputConfigs struct {
OUTPUT_MOD_CONFIG_ID int64 `json:"output_mod_config_id"`
APP_EXEC_ID int64 `json:"app_exec_id" validate:"required,number,gt=0"`
RELEASE_VERSION_ID int64 `json:"release_version_id" validate:"required,number,gt=0"`
PARAMETER_SET_HASH_ID int64 `json:"parameter_set_hash_id" validate:"required,number,gt=0"`
OUTPUT_MODULE_LABEL string `json:"output_module_label" validate:"required"`
GLOBAL_TAG string `json:"global_tag" validate:"required"`
SCENARIO string `json:"scenario"`
CREATION_DATE int64 `json:"creation_date" validate:"required,number,gt=0"`
CREATE_BY string `json:"create_by" validate:"required"`
}
// Insert implementation of OutputConfigs
func (r *OutputConfigs) Insert(tx *sql.Tx) error {
var tid int64
var err error
if r.OUTPUT_MOD_CONFIG_ID == 0 {
if DBOWNER == "sqlite" {
tid, err = LastInsertID(tx, "OUTPUT_MODULE_CONFIGS", "output_mod_config_id")
r.OUTPUT_MOD_CONFIG_ID = tid + 1
} else {
tid, err = IncrementSequence(tx, "SEQ_OMC")
r.OUTPUT_MOD_CONFIG_ID = tid
}
if err != nil {
return Error(err, LastInsertErrorCode, "", "dbs.outputconfigs.Insert")
}
}
// set defaults and validate the record
r.SetDefaults()
err = r.Validate()
if err != nil {
log.Println("unable to validate record", err)
return Error(err, ValidateErrorCode, "", "dbs.outputconfigs.Insert")
}
// check if our data already exist in DB
var vals []interface{}
vals = append(vals, r.APP_EXEC_ID)
vals = append(vals, r.PARAMETER_SET_HASH_ID)
vals = append(vals, r.RELEASE_VERSION_ID)
vals = append(vals, r.OUTPUT_MODULE_LABEL)
vals = append(vals, r.GLOBAL_TAG)
args := []string{
"app_exec_id",
"parameter_set_hash_id",
"release_version_id",
"output_module_label",
"global_tag"}
if IfExistMulti(tx, "OUTPUT_MODULE_CONFIGS", "output_mod_config_id", args, vals...) {
return nil
}
// get SQL statement from static area
stm := getSQL("insert_outputconfigs")
if utils.VERBOSE > 0 {
log.Printf("Insert OutputConfigs\n%s\n%+v", stm, r)
}
_, err = tx.Exec(
stm,
r.OUTPUT_MOD_CONFIG_ID,
r.APP_EXEC_ID,
r.RELEASE_VERSION_ID,
r.PARAMETER_SET_HASH_ID,
r.OUTPUT_MODULE_LABEL,
r.GLOBAL_TAG,
r.SCENARIO,
r.CREATION_DATE,
r.CREATE_BY)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to insert into OutputConfigs, error", err)
}
return Error(err, InsertErrorCode, "", "dbs.outputconfigs.Insert")
}
return nil
}
// Validate implementation of OutputConfigs
func (r *OutputConfigs) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATION_DATE)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.outputconfigs.Validate")
}
return nil
}
// SetDefaults implements set defaults for OutputConfigs
func (r *OutputConfigs) SetDefaults() {
if r.CREATION_DATE == 0 {
r.CREATION_DATE = Date()
}
}
// Decode implementation for OutputConfigs
func (r *OutputConfigs) Decode(reader io.Reader) error {
// init record with given data record
data, err := io.ReadAll(reader)
if err != nil {
log.Println("fail to read data", err)
return Error(err, ReaderErrorCode, "", "dbs.outputconfigs.Decode")
}
err = json.Unmarshal(data, &r)
// decoder := json.NewDecoder(r)
// err := decoder.Decode(&rec)
if err != nil {
log.Println("fail to decode data", err)
return Error(err, UnmarshalErrorCode, "", "dbs.outputconfigs.Decode")
}
return nil
}
// Size implementation for OutputConfigs
func (r *OutputConfigs) Size() int64 {
size := int64(unsafe.Sizeof(*r))
size += int64(len(r.OUTPUT_MODULE_LABEL))
size += int64(len(r.CREATE_BY))
return size
}
// OutputConfigRecord represents input to InsertOutputConfigs API
type OutputConfigRecord struct {
APP_NAME string `json:"app_name"`
RELEASE_VERSION string `json:"release_version"`
PSET_HASH string `json:"pset_hash"`
PSET_NAME string `json:"pset_name"`
GLOBAL_TAG string `json:"global_tag"`
OUTPUT_MODULE_LABEL string `json:"output_module_label"`
CREATION_DATE int64 `json:"creation_date"`
CREATE_BY string `json:"create_by"`
SCENARIO string `json:"scenario"`
}
// InsertOutputConfigsTx DBS API
//gocyclo:ignore
func (a *API) InsertOutputConfigsTx(tx *sql.Tx) error {
// read given input
data, err := io.ReadAll(a.Reader)
if err != nil {
log.Println("fail to read data", err)
return Error(err, ReaderErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
rec := OutputConfigRecord{CREATE_BY: a.CreateBy}
err = json.Unmarshal(data, &rec)
if err != nil {
log.Println("fail to decode data", err)
return Error(err, UnmarshalErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
// check if our data already exist in DB
var vals []interface{}
vals = append(vals, rec.APP_NAME)
vals = append(vals, rec.PSET_HASH)
vals = append(vals, rec.RELEASE_VERSION)
vals = append(vals, rec.OUTPUT_MODULE_LABEL)
vals = append(vals, rec.GLOBAL_TAG)
args := []string{
"app_name",
"pset_hash",
"release_version",
"output_module_label",
"global_tag"}
if IfExistMulti(tx, "OUTPUT_MODULE_CONFIGS", "output_mod_config_id", args, vals...) {
return nil
}
if rec.CREATION_DATE == 0 {
rec.CREATION_DATE = time.Now().Unix()
}
if rec.CREATE_BY == "" {
rec.CREATE_BY = a.CreateBy
}
arec := ApplicationExecutables{APP_NAME: rec.APP_NAME}
rrec := ReleaseVersions{RELEASE_VERSION: rec.RELEASE_VERSION}
prec := ParameterSetHashes{PSET_HASH: rec.PSET_HASH, PSET_NAME: rec.PSET_NAME}
orec := OutputConfigs{
GLOBAL_TAG: rec.GLOBAL_TAG,
OUTPUT_MODULE_LABEL: rec.OUTPUT_MODULE_LABEL,
CREATION_DATE: rec.CREATION_DATE,
CREATE_BY: rec.CREATE_BY,
SCENARIO: rec.SCENARIO}
// get and insert (if necessary) records IDs
var appID, psetID, relID int64
appID, err = GetRecID(
tx,
&arec,
"APPLICATION_EXECUTABLES",
"app_exec_id",
"app_name",
arec.APP_NAME)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to find app_exec_id", err, "will insert")
}
err = arec.Insert(tx)
if err != nil {
return Error(err, InsertErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
}
psetID, err = GetRecID(
tx,
&prec,
"PARAMETER_SET_HASHES",
"parameter_set_hash_id",
"pset_hash",
prec.PSET_HASH)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to find parameter_set_hash_id", err)
}
err = prec.Insert(tx)
if err != nil {
return Error(err, InsertErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
}
relID, err = GetRecID(
tx,
&rrec,
"RELEASE_VERSIONS",
"release_version_id",
"release_version",
rrec.RELEASE_VERSION)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to find release_version_id", err)
}
err = rrec.Insert(tx)
if err != nil {
return Error(err, InsertErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
}
// init all foreign Id's in output config record
orec.APP_EXEC_ID = appID
orec.RELEASE_VERSION_ID = relID
orec.PARAMETER_SET_HASH_ID = psetID
err = orec.Insert(tx)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to insert OutputConfigs record, error", err)
}
return Error(err, InsertErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
return nil
}
// InsertOutputConfigs DBS API
func (a *API) InsertOutputConfigs() error {
// start transaction
tx, err := DB.Begin()
if err != nil {
return Error(err, TransactionErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
defer tx.Rollback()
err = a.InsertOutputConfigsTx(tx)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to insert output configs", err)
}
return Error(err, InsertErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
// commit transaction
err = tx.Commit()
if err != nil {
log.Println("unable to commit transaction", err)
return Error(err, CommitErrorCode, "", "dbs.outputconfigs.InsertOutputConfigs")
}
if a.Writer != nil {
a.Writer.Write([]byte(`[]`))
}
return nil
}