-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlist.js
249 lines (236 loc) · 7.63 KB
/
list.js
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
/*
Copyright 2019 Adobe Inc. All rights reserved.
This file is licensed to you 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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const moment = require('dayjs')
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
const { Flags, CliUx: cli } = require('@oclif/core')
const decorators = require('../../../decorators').decorators()
const statusStrings = ['success', 'app error', 'dev error', 'sys error']
class ActivationList extends RuntimeBaseCommand {
async run () {
const { args, flags } = await this.parse(ActivationList)
const id = args.action_name
try {
const options = {}
if (id) {
options.name = id
}
if (flags.limit) {
options.limit = flags.limit
}
if (flags.skip) {
options.skip = flags.skip
}
if (flags.since) {
options.since = flags.since
}
if (flags.upto) {
options.upto = flags.upto
}
if (flags.full) {
options.docs = flags.full
// implies --json
flags.json = true
}
if (flags.count) {
options.count = true
}
const ow = await this.wsk()
let listActivation
if (Object.entries(options).length === 0) {
listActivation = await ow.activations.list()
} else {
listActivation = await ow.activations.list(options)
}
// if only showing the count, show the result and return
if (flags.count) {
const result = listActivation
if (flags.json) {
this.logJSON('', result)
} else {
this.log(`You have ${result.activations} ${result.activations === 1 ? 'activation' : 'activations'} in this namespace.`)
}
return
}
if (flags.json) {
this.logJSON('', listActivation)
} else {
// determine the namespace to use in the activation table
const ns = (await ow.namespaces.list())[0]
const columns = {
Datetime: {
get: row => moment(row.start).format('MM/DD HH:mm:ss'),
minWidth: 16
},
Status: {
get: (row) => {
const code = statusStrings[row.statusCode || 0]
if (row.statusCode === 2) {
const timeout = row.annotations.find(_ => _.key === 'timeout')
if (timeout && timeout.value) {
return 'timeout'
}
}
return code
},
minWidth: 9
},
Kind: {
get: (row) => {
if (row.duration !== undefined) {
const { annotations } = row
let kind
if (annotations && annotations.length) {
kind = annotations.find(_ => _.key === 'kind').value
}
kind = kind || '??'
return `${kind.includes('lambda') ? kind.replace('-lambda', '') + ' (λ)' : kind}`
} else {
// this is a trigger
return 'trigger'
}
},
minWidth: 9
},
version: {
header: 'Version',
minWidth: 9,
get: row => row.version
},
Topmost: {
header: '',
maxWidth: 2,
get: row => {
if (row.annotations && row.annotations.length) {
const seq = row.annotations.find(_ => _.key === 'causedBy')
if (seq && seq.value === 'sequence') return decorators.dot
const top = row.annotations.find(_ => _.key === 'topmost')
if (top && top.value) return decorators.half_circle_lower
}
return ''
}
},
ActivationID: {
header: 'Activation ID',
get: row => `${row.activationId}`
},
Start: {
get: (row) => {
if (row.duration !== undefined) {
const { annotations } = row
if (!annotations || !annotations.length) return
const elem = annotations.find((elem) => {
return (elem.key === 'initTime')
})
return elem ? 'cold' : 'warm'
} else {
return '--'
}
}
},
Wait: {
get: (row) => {
if (row.duration !== undefined) {
const { annotations } = row
if (!annotations || !annotations.length) return
const elem = annotations.find((elem) => {
return (elem.key === 'waitTime')
})
return elem ? elem.value : '--'
} else {
return '--'
}
}
},
Init: {
get: (row) => {
if (row.duration !== undefined) {
const { annotations } = row
if (!annotations || !annotations.length) return
const elem = annotations.find((elem) => {
return (elem.key === 'initTime')
})
return elem ? elem.value : 0
} else {
return '--'
}
}
},
Duration: {
get: row => row.duration ? `${row.duration}ms` : '--'
},
Entity: {
get: (row) => {
const { annotations } = row
let path
if (annotations && annotations.length) {
path = annotations.find(_ => _.key === 'path').value
}
path = path || `${row.namespace}/${row.name}`
return path.replace(`${ns}/`, '')
}
}
}
if (listActivation) {
cli.ux.table(listActivation, columns, {
'no-truncate': true
})
}
}
} catch (err) {
await this.handleError('failed to list the activations', err)
}
}
}
ActivationList.args = [
{
name: 'action_name'
}
]
ActivationList.flags = {
...RuntimeBaseCommand.flags,
// example usage: aio runtime:activation:list --limit 10 --skip 2
limit: Flags.integer({
char: 'l',
description: 'only return LIMIT number of activations (min: 0, max: 50)'
}),
skip: Flags.integer({
char: 's',
description: 'exclude the first SKIP number of activations from the result'
}),
since: Flags.integer({
description: 'return activations with timestamps later than SINCE; measured in milliseconds since Th, 01, Jan 1970'
}),
upto: Flags.integer({
description: 'return activations with timestamps earlier than UPTO; measured in milliseconds since Th, 01, Jan 1970'
}),
count: Flags.boolean({
char: 'c',
description: 'show only the total number of activations'
}),
json: Flags.boolean({
description: 'output raw json'
}),
full: Flags.boolean({
char: 'f',
description: 'include full activation description'
})
}
ActivationList.description = 'Lists all the Activations'
ActivationList.aliases = [
'runtime:activations:list',
'runtime:activation:ls',
'runtime:activations:ls',
'rt:activation:list',
'rt:activation:ls',
'rt:activations:list',
'rt:activations:ls'
]
module.exports = ActivationList