/
makeUpDown.go
673 lines (584 loc) · 21.4 KB
/
makeUpDown.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"bufio"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/openmpp/go/ompp/omppLog"
)
// UpDownStatusLog contains download, upload or delete status info and content of log file
type UpDownStatusLog struct {
Status string // if not empty then one of: progress ready error
Kind string // if not empty then one of: model, run, workset, delete, upload
ModelDigest string // content of "Model Digest:"
RunDigest string // content of "Run Digest:"
WorksetName string // content of "Scenario Name:"
IsFolder bool // if true then download (or upload) folder exist
Folder string // content of "Folder:"
FolderModTime int64 // folder modification time in milliseconds since epoch
IsZip bool // if true then download (or upload) zip exist
ZipFileName string // zip file name
ZipModTime int64 // zip modification time in milliseconds since epoch
ZipSize int64 // zip file size
LogFileName string // log file name
LogModTime int64 // log file modification time in milliseconds since epoch
Lines []string // file content
}
// PathItem contain basic file info after tree walk: relative path, size and modification time
type PathItem struct {
Path string // file path in / slash form
IsDir bool // if true then it is a directory
Size int64 // file size (may be zero for directories)
ModTime int64 // file modification time in milliseconds since epoch
}
// make dbcopy command to prepare full model download
func makeModelDownloadCommand(mb modelBasic, logPath string, isNoAcc bool, isNoMd bool, isCsvBom bool) (*exec.Cmd, string) {
// make dbcopy message for user log
cmdMsg := "dbcopy -m " + mb.model.Name + " -dbcopy.Zip -dbcopy.OutputDir " + theCfg.downloadDir
if isNoAcc {
cmdMsg += " -dbcopy.NoAccumulatorsCsv"
}
if isNoMd {
cmdMsg += " -dbcopy.NoMicrodata"
}
if isCsvBom {
cmdMsg += " -dbcopy.Utf8BomIntoCsv"
}
// make relative path arguments to dbcopy work directory: to a model bin directory
downDir, dbPathRel, dbcopyRel, err := makeRelDbCopyArgs(mb.binDir, theCfg.downloadDir, mb.dbPath)
if err != nil {
renameToDownloadErrorLog(logPath, "Error at starting "+cmdMsg, err)
return nil, cmdMsg
}
// make dbcopy command
cArgs := []string{"-m", mb.model.Name, "-dbcopy.Zip", "-dbcopy.OutputDir", downDir, "-dbcopy.FromSqlite", dbPathRel}
if isNoAcc {
cArgs = append(cArgs, "-dbcopy.NoAccumulatorsCsv")
}
if isNoMd {
cArgs = append(cArgs, "-dbcopy.NoMicrodata")
}
if isCsvBom {
cArgs = append(cArgs, "-dbcopy.Utf8BomIntoCsv")
}
cmd := exec.Command(dbcopyRel, cArgs...)
cmd.Dir = mb.binDir // dbcopy work directory is a model bin directory
return cmd, cmdMsg
}
// make dbcopy command to prepare model run download
func makeRunDownloadCommand(mb modelBasic, runId int, logPath string, isNoAcc bool, isNoMd bool, isCsvBom bool) (*exec.Cmd, string) {
// make dbcopy message for user log
cmdMsg := "dbcopy -m " + mb.model.Name +
" -dbcopy.IdOutputNames=false" +
" -dbcopy.RunId " + strconv.Itoa(runId) +
" -dbcopy.Zip" +
" -dbcopy.OutputDir " + theCfg.downloadDir
if isNoAcc {
cmdMsg += " -dbcopy.NoAccumulatorsCsv"
}
if isNoMd {
cmdMsg += " -dbcopy.NoMicrodata"
}
if isCsvBom {
cmdMsg += " -dbcopy.Utf8BomIntoCsv"
}
// make relative path arguments to dbcopy work directory: to a model bin directory
downDir, dbPathRel, dbcopyRel, err := makeRelDbCopyArgs(mb.binDir, theCfg.downloadDir, mb.dbPath)
if err != nil {
renameToDownloadErrorLog(logPath, "Error at starting "+cmdMsg, err)
return nil, cmdMsg
}
// make dbcopy command
cArgs := []string{
"-m", mb.model.Name,
"-dbcopy.IdOutputNames=false",
"-dbcopy.RunId", strconv.Itoa(runId),
"-dbcopy.Zip",
"-dbcopy.OutputDir", downDir,
"-dbcopy.FromSqlite", dbPathRel,
}
if isNoAcc {
cArgs = append(cArgs, "-dbcopy.NoAccumulatorsCsv")
}
if isNoMd {
cArgs = append(cArgs, "-dbcopy.NoMicrodata")
}
if isCsvBom {
cArgs = append(cArgs, "-dbcopy.Utf8BomIntoCsv")
}
cmd := exec.Command(dbcopyRel, cArgs...)
cmd.Dir = mb.binDir // dbcopy work directory is a model bin directory
return cmd, cmdMsg
}
// make dbcopy command to prepare model workset download
func makeWorksetDownloadCommand(mb modelBasic, setName string, logPath string, isCsvBom bool) (*exec.Cmd, string) {
// make dbcopy message for user log
cmdMsg := "dbcopy -m " + mb.model.Name +
" -dbcopy.IdOutputNames=false" +
" -dbcopy.SetName " + setName +
" -dbcopy.Zip" +
" -dbcopy.OutputDir " + theCfg.downloadDir
if isCsvBom {
cmdMsg += " -dbcopy.Utf8BomIntoCsv "
}
// make relative path arguments to dbcopy work directory: to a model bin directory
downDir, dbPathRel, dbcopyRel, err := makeRelDbCopyArgs(mb.binDir, theCfg.downloadDir, mb.dbPath)
if err != nil {
renameToDownloadErrorLog(logPath, "Error at starting "+cmdMsg, err)
return nil, cmdMsg
}
// make dbcopy command
cArgs := []string{
"-m", mb.model.Name,
"-dbcopy.IdOutputNames=false",
"-dbcopy.SetName", setName,
"-dbcopy.Zip",
"-dbcopy.OutputDir", downDir,
"-dbcopy.FromSqlite", dbPathRel,
}
if isCsvBom {
cArgs = append(cArgs, "-dbcopy.Utf8BomIntoCsv")
}
cmd := exec.Command(dbcopyRel, cArgs...)
cmd.Dir = mb.binDir // dbcopy work directory is a model bin directory
return cmd, cmdMsg
}
// make dbcopy command to prepare model run import into database after upload
func makeRunUploadCommand(mb modelBasic, runName string, logPath string) (*exec.Cmd, string) {
// make dbcopy message for user log
cmdMsg := "dbcopy -m " + mb.model.Name +
" -dbcopy.IdOutputNames=false" +
" -dbcopy.RunName " + runName +
" -dbcopy.To db" +
" -dbcopy.Zip" +
" -dbcopy.InputDir " + theCfg.uploadDir
// make relative path arguments to dbcopy work directory: to a model bin directory
upDir, dbPathRel, dbcopyRel, err := makeRelDbCopyArgs(mb.binDir, theCfg.uploadDir, mb.dbPath)
if err != nil {
renameToUploadErrorLog(logPath, "Error at starting "+cmdMsg, err)
return nil, cmdMsg
}
// make dbcopy command
cArgs := []string{
"-m", mb.model.Name,
"-dbcopy.IdOutputNames=false",
"-dbcopy.RunName", runName,
"-dbcopy.To", "db",
"-dbcopy.Zip",
"-dbcopy.InputDir", upDir,
"-dbcopy.ToSqlite", dbPathRel,
}
cmd := exec.Command(dbcopyRel, cArgs...)
cmd.Dir = mb.binDir // dbcopy work directory is a model bin directory
return cmd, cmdMsg
}
// make dbcopy command to prepare model workset import into database after upload
func makeWorksetUploadCommand(mb modelBasic, setName string, logPath string, isNoDigestCheck bool) (*exec.Cmd, string) {
// make dbcopy message for user log
cmdMsg := "dbcopy -m " + mb.model.Name +
" -dbcopy.IdOutputNames=false" +
" -dbcopy.SetName " + setName +
" -dbcopy.To db" +
" -dbcopy.Zip" +
" -dbcopy.InputDir " + theCfg.uploadDir
if isNoDigestCheck {
cmdMsg += " -dbcopy.NoDigestCheck"
}
// make relative path arguments to dbcopy work directory: to a model bin directory
upDir, dbPathRel, dbcopyRel, err := makeRelDbCopyArgs(mb.binDir, theCfg.uploadDir, mb.dbPath)
if err != nil {
renameToUploadErrorLog(logPath, "Error at starting "+cmdMsg, err)
return nil, cmdMsg
}
// make dbcopy command
cArgs := []string{
"-m", mb.model.Name,
"-dbcopy.IdOutputNames=false",
"-dbcopy.SetName", setName,
"-dbcopy.To", "db",
"-dbcopy.Zip",
"-dbcopy.InputDir", upDir,
"-dbcopy.ToSqlite", dbPathRel,
}
if isNoDigestCheck {
cArgs = append(cArgs, "-dbcopy.NoDigestCheck")
}
cmd := exec.Command(dbcopyRel, cArgs...)
cmd.Dir = mb.binDir // dbcopy work directory is a model bin directory
return cmd, cmdMsg
}
// make relative to dbcopy work directory from targetPath, model db path and dbcopy path
func makeRelDbCopyArgs(workDir, targetPath, dbPath string) (string, string, string, error) {
wDir, err := filepath.Abs(workDir)
if err != nil {
return "", "", "", err
}
aTarget, err := filepath.Abs(targetPath)
if err != nil {
return "", "", "", err
}
relTarget, err := filepath.Rel(wDir, aTarget)
if err != nil {
return "", "", "", err
}
relDbPath, err := filepath.Rel(wDir, dbPath)
if err != nil {
return "", "", "", err
}
relDbcopy, err := filepath.Rel(wDir, theCfg.dbcopyPath)
if err != nil {
return "", "", "", err
}
return relTarget, relDbPath, relDbcopy, nil
}
// makeDownload invoke dbcopy to create model download directory and .zip file:
// 1. delete existing: previous download log file, model.xyz.zip, model.xyz directory.
// 2. start dbcopy to export model data into pack it into .zip file.
// 3. if dbcopy done OK then rename log file into model......ready.download.log else into model......error.download.log
func makeDownload(baseName string, cmd *exec.Cmd, cmdMsg string, logPath string) {
runUpDownDbcopy("download", theCfg.downloadDir, baseName, cmd, cmdMsg, logPath)
}
// makeUpload invoke dbcopy to create model upload directory and .zip file:
// 1. delete existing: previous upload log file and model.xyz directory.
// 2. start dbcopy to unzip uploaded file and import into it model database.
// 3. if dbcopy done OK then rename log file into model......ready.upload.log else into model......error.upload.log
func makeUpload(baseName string, cmd *exec.Cmd, cmdMsg string, logPath string) {
runUpDownDbcopy("upload", theCfg.uploadDir, baseName, cmd, cmdMsg, logPath)
}
// runUpDownDbcopy invoke dbcopy to export from dbd into download .zip or import from uploaded .zip into model database.
// 1. delete existing: previous log file and model.xyz directory.
// 2. if download then delete existing model.xyz.zip
// 3. if download: start dbcopy to export model data into .zip file.
// 3. if upload: stsrt dbopy to unzip uploaded file and import into it model database.
// 4. if dbcopy done OK then rename log file into model......ready.up-or-down.log else into model......error.up-or-down.log
func runUpDownDbcopy(upDown string, upDownDir string, baseName string, cmd *exec.Cmd, cmdMsg string, logPath string) {
// delete existing (previous copy) of download or upload data
basePath := filepath.Join(upDownDir, baseName)
if !removeUpDownFile(upDown, basePath+".ready."+upDown+".log", logPath, baseName+".ready."+upDown+".log") {
return
}
if !removeUpDownFile(upDown, basePath+".error."+upDown+".log", logPath, baseName+".error."+upDown+".log") {
return
}
if upDown == "download" && !removeUpDownFile(upDown, basePath+".zip", logPath, baseName+".zip") {
return
}
if !removeUpDownDir(upDown, basePath, logPath, baseName) {
return
}
writeToCmdLog(logPath, true, cmdMsg)
// connect console output to output log file
outPipe, err := cmd.StdoutPipe()
if err != nil {
renameToUpDownErrorLog(upDown, logPath, "Error at starting "+cmdMsg, err)
return
}
errPipe, err := cmd.StderrPipe()
if err != nil {
renameToUpDownErrorLog(upDown, logPath, "Error at starting "+cmdMsg, err)
return
}
outDoneC := make(chan bool, 1)
errDoneC := make(chan bool, 1)
logTck := time.NewTicker(logTickTimeout * time.Millisecond)
// append console output to log
isLogOk := true
doLog := func(path string, r io.Reader, done chan<- bool) {
sc := bufio.NewScanner(r)
for sc.Scan() {
if isLogOk {
isLogOk = writeToCmdLog(path, false, sc.Text())
}
}
done <- true
close(done)
}
// start console output listners
absLogPath, err := filepath.Abs(logPath)
if err != nil {
renameToUpDownErrorLog(upDown, logPath, "Error at starting "+cmdMsg, err)
return
}
go doLog(absLogPath, outPipe, outDoneC)
go doLog(absLogPath, errPipe, errDoneC)
// start dbcopy
omppLog.Log("Run dbcopy at: ", cmd.Dir)
omppLog.Log(strings.Join(cmd.Args, " "))
err = cmd.Start()
if err != nil {
renameToUpDownErrorLog(upDown, logPath, err.Error(), err)
return
}
// else dbcopy started: wait until completed
// wait until stdout and stderr closed
for outDoneC != nil || errDoneC != nil {
select {
case _, ok := <-outDoneC:
if !ok {
outDoneC = nil
}
case _, ok := <-errDoneC:
if !ok {
errDoneC = nil
}
case <-logTck.C:
}
}
// wait for dbcopy to be completed
e := cmd.Wait()
if e != nil {
omppLog.Log("Error at: ", cmd.Args)
writeToCmdLog(logPath, true, e.Error())
renameToUpDownErrorLog(upDown, logPath, "Error at: "+cmdMsg, e)
return
}
// else: completed OK
if !isLogOk {
omppLog.Log("Warning: dbcopy log output may be incomplete")
}
// all done, rename log file on success: model......progress.up-or-down.log into model......ready.up-or-down.log
os.Rename(logPath, strings.TrimSuffix(logPath, ".progress."+upDown+".log")+".ready."+upDown+".log")
}
// remove download file and append log message
// on error do rename log file into model......error.download.log and return false
func removeDownloadFile(path string, logPath string, fileName string) bool {
return removeUpDownFile("download", path, logPath, fileName)
}
// remove upload file and append log message
// on error do rename log file into model......error.upload.log and return false
func removeUploadFile(path string, logPath string, fileName string) bool {
return removeUpDownFile("upload", path, logPath, fileName)
}
// remove download or upload file and append log message
// on error do rename log file into model......error.up-or-down.log and return false
func removeUpDownFile(upDown string, path string, logPath string, fileName string) bool {
if !writeToCmdLog(logPath, true, "delete: "+fileName) {
renameToUpDownErrorLog(upDown, logPath, "", nil)
return false
}
if e := os.Remove(path); e != nil && !os.IsNotExist(e) {
renameToUpDownErrorLog(upDown, logPath, "Error at delete "+fileName, e)
return false
}
return true
}
// remove download directory and append log message
// on error do rename log file into model......error.download.log and return false
func removeDownloadDir(path string, logPath string, dirName string) bool {
return removeUpDownDir("download", path, logPath, dirName)
}
// remove upload directory and append log message
// on error do rename log file into model......error.upload.log and return false
func removeUploadDir(path string, logPath string, dirName string) bool {
return removeUpDownDir("upload", path, logPath, dirName)
}
// remove upload or download directory and append log message
// on error do rename log file into model......error.up-or-down.log and return false
func removeUpDownDir(upDown string, path string, logPath string, dirName string) bool {
if !writeToCmdLog(logPath, true, "delete: "+dirName) {
renameToUpDownErrorLog(upDown, logPath, "", nil)
return false
}
if e := os.RemoveAll(path); e != nil && !os.IsNotExist(e) {
renameToUpDownErrorLog(upDown, logPath, "Error at delete "+dirName, e)
return false
}
return true
}
// rename upload or download log file on error: model......progress.download.log into model......error.download.log
func renameToDownloadErrorLog(logPath string, logErrMsg string, err error) {
renameToUpDownErrorLog("download", logPath, logErrMsg, err)
}
// rename upload log file on error: model......progress.upload.log into model......error.upload.log
func renameToUploadErrorLog(logPath string, logErrMsg string, err error) {
renameToUpDownErrorLog("upload", logPath, logErrMsg, err)
}
// rename upload or download log file on error: model......progress.up-or-down.log into model......error.up-or-down.log
func renameToUpDownErrorLog(upDown string, logPath string, logErrMsg string, err error) {
if err != nil {
omppLog.Log(err)
}
if logErrMsg != "" {
writeToCmdLog(logPath, true, logErrMsg)
}
os.Rename(logPath, strings.TrimSuffix(logPath, ".progress."+upDown+".log")+".error."+upDown+".log")
}
// update file status of download files
func updateStatDownloadLog(logName string, uds *UpDownStatusLog) {
updateStatUpDownLog(logName, uds, theCfg.downloadDir)
}
// update file status of upload files
func updateStatUploadLog(logName string, uds *UpDownStatusLog) {
updateStatUpDownLog(logName, uds, theCfg.uploadDir)
}
// update file status of download or upload files:
// check if zip exist, if folder exist and retirve file modification time in milliseconds since epoch
func updateStatUpDownLog(logName string, uds *UpDownStatusLog, upDownDir string) {
// check if download zip or download folder exist
if uds.Folder != "" {
fi, e := dirStat(filepath.Join(upDownDir, uds.Folder))
uds.IsFolder = e == nil
if uds.IsFolder {
uds.FolderModTime = fi.ModTime().UnixNano() / int64(time.Millisecond)
}
if uds.Status == "ready" {
fi, e = fileStat(filepath.Join(upDownDir, uds.Folder+".zip"))
uds.IsZip = e == nil
if uds.IsZip {
uds.ZipFileName = uds.Folder + ".zip"
uds.ZipSize = fi.Size()
uds.ZipModTime = fi.ModTime().UnixNano() / int64(time.Millisecond)
}
}
}
// retrive log file modification time
if fi, err := os.Stat(filepath.Join(upDownDir, logName)); err == nil {
uds.LogModTime = fi.ModTime().UnixNano() / int64(time.Millisecond)
}
}
// parseDownloadLogFileList for each download directory entry check is it a .download.log file and parse content
func parseDownloadLogFileList(preffix string, dirEntryLst []fs.DirEntry) []UpDownStatusLog {
return parseUpDownLogFileList("download", preffix, dirEntryLst, theCfg.downloadDir)
}
// parseUploadLogFileList for each upload directory entry check is it a .upload.log file and parse content
func parseUploadLogFileList(preffix string, dirEntryLst []fs.DirEntry) []UpDownStatusLog {
return parseUpDownLogFileList("upload", preffix, dirEntryLst, theCfg.uploadDir)
}
// parseUpDownLogFileList for each download or upload directory entry check is it a .up-or-down.log file and parse content
func parseUpDownLogFileList(upDown string, preffix string, dirEntryLst []fs.DirEntry, upDownDir string) []UpDownStatusLog {
udsLst := []UpDownStatusLog{}
for _, f := range dirEntryLst {
// skip if this is not a .up-or-down.log file or does not have modelName. prefix
if f.IsDir() || !strings.HasSuffix(f.Name(), "."+upDown+".log") {
continue
}
if preffix != "" && !strings.HasPrefix(f.Name(), preffix) {
continue
}
// read log file, skip on error
bt, err := os.ReadFile(filepath.Join(upDownDir, f.Name()))
if err != nil {
omppLog.Log("Failed to read log file: ", f.Name())
continue // skip log file on read error
}
fc := string(bt)
// parse log file content to get folder name, log file kind and keys
uds := parseUpDownLog(upDown, f.Name(), fc)
updateStatUpDownLog(f.Name(), &uds, upDownDir)
udsLst = append(udsLst, uds)
}
return udsLst
}
// parse log file content to get folder name, log file kind and keys
// kind and keys are:
//
// model: model digest
// run: model digest, run digest
// workset: model digest, workset name
// delete: folder
// upload: zip file name
func parseDownloadLog(fileName, fileContent string) UpDownStatusLog {
return parseUpDownLog("download", fileName, fileContent)
}
// parse log file content to get folder name, log file kind and keys
// kind and keys are:
//
// model: model digest
// run: model digest, run digest
// workset: model digest, workset name
// delete: folder
// upload: zip file name
func parseUpDownLog(upDown, fileName, fileContent string) UpDownStatusLog {
uds := UpDownStatusLog{LogFileName: fileName}
// set status by .up-or-down.log file extension
if uds.Status == "" && strings.HasSuffix(fileName, ".ready."+upDown+".log") {
uds.Status = "ready"
}
if uds.Status == "" && strings.HasSuffix(fileName, ".progress."+upDown+".log") {
uds.Status = "progress"
}
if uds.Status == "" && strings.HasSuffix(fileName, ".error."+upDown+".log") {
uds.Status = "error"
}
// split log lines
uds.Lines = strings.Split(strings.ReplaceAll(fileContent, "\r", "\x20"), "\n")
if len(uds.Lines) <= 0 {
return uds // empty log file
}
// header is between -------- lines, at least 8 dashes expected
firstHdr := 0
endHdr := 0
for k := 0; k < len(uds.Lines); k++ {
if strings.HasPrefix(uds.Lines[k], "--------") {
if firstHdr <= 0 {
firstHdr = k + 1
} else {
endHdr = k
break
}
}
}
// header must have at least two lines: folder and model digest or delete
if firstHdr <= 1 || endHdr < firstHdr+2 || endHdr >= len(uds.Lines) {
return uds
}
// parse header lines to find keys and folder
for _, h := range uds.Lines[firstHdr:endHdr] {
if strings.HasPrefix(h, "Folder") {
if n := strings.IndexByte(h, ':'); n > 1 && n+1 < len(h) {
uds.Folder = strings.TrimSpace(h[n+1:])
}
continue
}
if strings.HasPrefix(h, "Model Digest") {
if n := strings.IndexByte(h, ':'); n > 1 && n+1 < len(h) {
uds.ModelDigest = strings.TrimSpace(h[n+1:])
}
continue
}
if strings.HasPrefix(h, "Run Digest") {
if n := strings.IndexByte(h, ':'); n > 1 && n+1 < len(h) {
uds.RunDigest = strings.TrimSpace(h[n+1:])
}
continue
}
if strings.HasPrefix(h, "Scenario Name") {
if n := strings.IndexByte(h, ':'); n > 1 && n+1 < len(h) {
uds.WorksetName = strings.TrimSpace(h[n+1:])
}
continue
}
if strings.HasPrefix(h, "Delete") {
if n := strings.IndexByte(h, ':'); n > 1 && n+1 < len(h) {
uds.Kind = "delete"
uds.Folder = strings.TrimSpace(h[n+1:])
}
continue
}
if strings.HasPrefix(h, "Upload") {
if n := strings.IndexByte(h, ':'); n > 1 && n+1 < len(h) {
uds.Kind = "upload"
uds.ZipFileName = strings.TrimSpace(h[n+1:])
}
continue
}
}
// check kind of the log: model, model run, workset or delete
if uds.Kind == "" && uds.ModelDigest != "" && uds.RunDigest != "" {
uds.Kind = "run"
}
if uds.Kind == "" && uds.ModelDigest != "" && uds.WorksetName != "" {
uds.Kind = "workset"
}
if uds.Kind == "" && uds.ModelDigest != "" {
uds.Kind = "model"
}
return uds
}