-
Notifications
You must be signed in to change notification settings - Fork 598
/
CmdLog.groovy
316 lines (245 loc) · 9.11 KB
/
CmdLog.groovy
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
/*
* Copyright 2013-2024, Seqera Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.cli
import java.nio.file.Path
import ch.artecat.grengine.Grengine
import com.beust.jcommander.Parameter
import com.beust.jcommander.Parameters
import com.google.common.hash.HashCode
import groovy.text.Template
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.exception.AbortOperationException
import nextflow.file.FileHelper
import nextflow.plugin.Plugins
import nextflow.processor.TaskRun
import nextflow.processor.TaskTemplateEngine
import nextflow.trace.TraceRecord
import nextflow.ui.TableBuilder
import static nextflow.cli.CmdHelper.fixEqualsOp
/**
* Implements the `log` command to print tasks runtime information of an execute pipeline
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@Slf4j
@CompileStatic
@Parameters(commandDescription = "Print executions log and runtime info")
class CmdLog extends CmdBase implements CacheBase {
static private List<String> ALL_FIELDS
static private DEFAULT_FIELDS = 'workdir'
static {
ALL_FIELDS = []
ALL_FIELDS.addAll( TraceRecord.FIELDS.keySet().collect { it.startsWith('%') ? 'p'+it.substring(1) : it } )
ALL_FIELDS << 'stdout'
ALL_FIELDS << 'stderr'
ALL_FIELDS << 'log'
ALL_FIELDS.sort(true)
}
static final public NAME = 'log'
@Parameter(names = ['-s'], description='Character used to separate column values')
String sep = '\\t'
@Parameter(names=['-f','-fields'], description = 'Comma separated list of fields to include in the printed log -- Use the `-l` option to show the list of available fields')
String fields
@Parameter(names = ['-t','-template'], description = 'Text template used to each record in the log ')
String templateStr
@Parameter(names=['-l','-list-fields'], description = 'Show all available fields', arity = 0)
boolean listFields
@Parameter(names=['-F','-filter'], description = "Filter log entries by a custom expression e.g. process =~ /foo.*/ && status == 'COMPLETED'")
String filterStr
@Parameter(names='-after', description = 'Show log entries for runs executed after the specified one')
String after
@Parameter(names='-before', description = 'Show log entries for runs executed before the specified one')
String before
@Parameter(names='-but', description = 'Show log entries of all runs except the specified one')
String but
@Parameter(names=['-q','-quiet'], description = 'Show only run names', arity = 0)
boolean quiet
@Parameter(description = 'Run name or session id')
List<String> args
private Script filterScript
private boolean showHistory
private Template templateScript
private Map<HashCode,Boolean> printed = new HashMap<>()
@Override
final String getName() { NAME }
void init() {
CacheBase.super.init()
//
// validate input options
//
if( fields && templateStr )
throw new AbortOperationException("Options `-f` and `-t` cannot be used in the same command")
//
// when no CLI options have been specified, just show the history log
showHistory = !args && !before && !after && !but
//
// initialise filter engine
//
if( filterStr ) {
filterScript = new Grengine().create("{ it -> ${fixEqualsOp(filterStr)} }")
}
//
// initialize the template engine
//
if( !templateStr ) {
if( !fields ) fields = DEFAULT_FIELDS
templateStr = fields.tokenize(', \n').collect { '$'+it } .join(sep)
}
else if( new File(templateStr).exists() ) {
templateStr = new File(templateStr).text
}
templateScript = new TaskTemplateEngine().createTemplate(templateStr)
}
/**
* Implements the `log` command
*/
@Override
void run() {
Plugins.init()
init()
// -- show the list of expected fields and exit
if( listFields ) {
ALL_FIELDS.each { println " $it" }
return
}
// -- show the current history and exit
if( showHistory ) {
quiet ? printQuiet() : printHistory()
return
}
// -- main
listIds().each { entry ->
cacheFor(entry)
.openForRead()
.eachRecord(this.&printRecord)
.close()
}
}
/**
* Print a log {@link TraceRecord} the the standard output by using the specified {@link #templateStr}
*
* @param record A {@link TraceRecord} instance representing a task runtime information
*/
protected void printRecord(HashCode hash, TraceRecord record) {
if( printed.containsKey(hash) )
return
else
printed.put(hash,Boolean.TRUE)
final adaptor = new TraceAdaptor(record)
if( filterScript ) {
filterScript.setBinding(adaptor)
// dynamic execution of the filter statement
// the `run` method interprets the statement groovy closure
// then the `call` method invokes the closure which returns a bool value
// if `false` skip this record
if( !((Closure)filterScript.run()).call() ) {
return
}
}
println templateScript.make(adaptor).toString()
}
private void printHistory() {
def table = new TableBuilder(cellSeparator: '\t')
.head('TIMESTAMP')
.head('DURATION')
.head('RUN NAME')
.head('STATUS')
.head('REVISION ID')
.head('SESSION ID')
.head('COMMAND')
history.eachRow { List<String> row ->
row[4] = row[4].size()>10 ? row[4].substring(0,10) : row[4]
table.append(row)
}
println table.toString()
}
private void printQuiet() {
history.eachRow { List row -> println(row[2]) }
}
/**
* Wrap a {@link TraceRecord} instance as a {@link Map} or a {@link Binding} object
*/
private static class TraceAdaptor extends Binding {
static private int MAX_LINES = 100
private TraceRecord record
@Delegate
private Map<String,Object> delegate = [:]
TraceAdaptor(TraceRecord record) {
this.record = record
}
@Override
boolean containsKey(Object key) {
delegate.containsKey(key.toString()) || record.containsKey(key.toString())
}
@Override
Object get(Object key) {
if( delegate.containsKey(key) ) {
return delegate.get(key)
}
if( key == 'stdout' ) {
return fetch(getWorkDir().resolve(TaskRun.CMD_OUTFILE))
}
if( key == 'stderr' ) {
return fetch(getWorkDir().resolve(TaskRun.CMD_ERRFILE))
}
if( key == 'log' ) {
return fetch(getWorkDir().resolve(TaskRun.CMD_LOG))
}
if( key == 'pcpu' )
return record.getFmtStr('%cpu')
if( key == 'pmem' )
return record.getFmtStr('%mem')
return record.getFmtStr(normaliseKey(key))
}
String normaliseKey(key) {
key .toString() .toLowerCase()
}
Object getVariable(String name) {
if( name == 'pcpu' )
return record.store.get('%cpu')
if( name == 'pmem' )
return record.store.get('%mem')
if( record.containsKey(name) )
return record.store.get(name)
throw new MissingPropertyException(name)
}
Map getVariables() {
new HashMap(record.store)
}
private Path getWorkDir() {
def folder = (String)record.get('workdir')
folder ? FileHelper.asPath(folder) : null
}
private String fetch(Path path) {
try {
int c=0
def result = new StringBuilder()
path.withReader { reader ->
String line
while( (line=reader.readLine()) && c++<MAX_LINES ) {
result << line << '\n'
}
}
result.toString() ?: TraceRecord.NA
}
catch( IOError e ) {
log.debug "Failed to fetch content for file: $path -- Cause: ${e.message ?: e}"
return TraceRecord.NA
}
}
}
}