/
runLog.go
220 lines (182 loc) · 5.67 KB
/
runLog.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"bufio"
"io"
"os"
"strings"
"unicode"
"github.com/openmpp/go/ompp/db"
"github.com/openmpp/go/ompp/omppLog"
)
// get current run status and page of log file lines
func (rsc *RunCatalog) readModelRunLog(digest, runStamp string, start, count int) (*RunStateLogPage, error) {
// search for run status and log text by model digest and run stamp
lrp, isFound, isNewLog := rsc.getRunStateLogPage(digest, runStamp, start, count)
if !isFound {
return lrp, nil // model run not found in catalog
}
if !lrp.isHistory {
return lrp, nil // found model run
}
// else found model run in run history
// check run status: if not completed then read most recent status from database
isUpdated := false
rl, isOk := theCatalog.RunRowList(digest, runStamp)
if isOk && len(rl) > 0 {
// use most recent run with that run stamp
r := rl[len(rl)-1]
isUpdated = lrp.RunState.UpdateDateTime != r.UpdateDateTime || lrp.RunState.IsFinal != db.IsRunCompleted(r.Status)
if isUpdated {
lrp.RunState.UpdateDateTime = r.UpdateDateTime
lrp.RunState.IsFinal = db.IsRunCompleted(r.Status)
}
}
// if run updated or if run found in history
if isNewLog || isUpdated {
// read log file and select log page to return
lines := []string{}
if lrp.RunState.IsLog {
if lines, isOk = rsc.readLogFile(lrp.logPath); isOk && len(lines) > 0 {
lrp.Offset, lrp.Size, lrp.Lines = getLinesPage(start, count, lines) // make log page to retrun
lrp.TotalSize = len(lines)
}
}
// update model run catalog
rsc.postRunStateLog(digest, lrp.RunState, lines)
}
return lrp, nil
}
// update model run catalog with new run state and log file lines.
func (rsc *RunCatalog) postRunStateLog(digest string, runState RunState, logLines []string) {
if digest == "" || runState.RunStamp == "" {
return // model run undefined
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// find model run state by digest and run stamp
// update model run state and append log message
var rs *runStateLog
var ok bool
for re := rsc.runLst.Back(); re != nil; re = re.Prev() {
rs, ok = re.Value.(*runStateLog)
if !ok || rs == nil {
continue
}
ok = rs.ModelDigest == digest && rs.RunStamp == runState.RunStamp
if ok {
break
}
}
// update existing run status or append new element to run list
if ok && rs != nil {
rs.RunState = runState
if len(logLines) > len(rs.logLineLst) {
rs.logLineLst = logLines
}
} else {
rsc.runLst.PushBack(
&runStateLog{
RunState: runState,
logLineLst: logLines,
})
}
}
// get current run status and page of log lines and return true if run status found in catalog.
// if run found in log history it is neccessary to check run status and may be read log file content.
// return run state and two flags: first is true if model run found, second is true if model run found in log history.
func (rsc *RunCatalog) getRunStateLogPage(digest, runStamp string, start, count int) (*RunStateLogPage, bool, bool) {
lrp := &RunStateLogPage{
Lines: []string{},
}
if digest == "" || runStamp == "" {
return lrp, false, false // empty model digest: exit
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// find model run state by digest and run stamp
// return model run state and page from model log
var rsl *runStateLog
isFound := false
for re := rsc.runLst.Front(); !isFound && re != nil; re = re.Next() {
rsl, isFound = re.Value.(*runStateLog)
if !isFound || rsl == nil {
continue
}
isFound = rsl.ModelDigest == digest && rsl.RunStamp == runStamp
}
// if not found then try to search log history
isNewLog := false
if !isFound {
if ml, ok := rsc.modelLogs[digest]; ok {
if r, isLog := ml[runStamp]; isLog {
rsl = &runStateLog{RunState: r, logLineLst: []string{}}
isFound = true
}
}
isNewLog = isFound
}
if !isFound {
return lrp, false, false // not found: return empty result
}
// run state found
lrp.RunState = rsl.RunState
// if run log lines are empty then it is necceassry to read log file outside of lock
if isNewLog || len(rsl.logLineLst) <= 0 {
return lrp, true, isNewLog
}
// copy log lines
lrp.TotalSize = len(rsl.logLineLst)
lrp.Offset, lrp.Size, lrp.Lines = getLinesPage(start, count, rsl.logLineLst)
return lrp, true, isNewLog
}
// read all non-empty text lines from log file.
func (rsc *RunCatalog) readLogFile(logPath string) ([]string, bool) {
if logPath == "" {
return []string{}, false // empty log file path: exit
}
f, err := os.Open(logPath)
if err != nil {
return []string{}, false // cannot open log file for reading
}
defer f.Close()
rd := bufio.NewReader(f)
// read log file, trim lines and skip empty lines
lines := []string{}
for {
ln, e := rd.ReadString('\n')
if e != nil {
if e != io.EOF {
omppLog.Log("Error at reading log file: ", logPath)
}
break
}
ln = strings.TrimRightFunc(ln, func(r rune) bool { return unicode.IsSpace(r) || !unicode.IsPrint(r) })
if ln != "" {
lines = append(lines, ln)
}
}
return lines, true
}
// getLinesPage return count pages from logLines starting at offset.
// if count <= 0 then return all lines starting at offset
func getLinesPage(offset, count int, logLines []string) (int, int, []string) {
nTotal := len(logLines)
if nTotal <= 0 {
return 0, 0, []string{}
}
if offset < 0 {
offset = 0
}
if offset >= nTotal {
return offset, 0, []string{} // log offset (first line to read) past last log line
}
if count <= 0 || offset+count > nTotal {
count = nTotal - offset
}
// copy log lines into result
lines := make([]string, count)
copy(lines, logLines[offset:offset+count])
return offset, count, lines
}