Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 13ac5e4

Browse files
committed
add verbose flag to invoke command
1 parent ca27034 commit 13ac5e4

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ $ serverless invoke --function hello --data '{"name": "OpenWhisk"}'
111111
}
112112
```
113113

114+
*Add the `-v` or `--verbose` flag to show more [invocation details](https://github.com/apache/incubator-openwhisk/blob/master/docs/annotations.md#annotations-specific-to-activations), e.g. activation id and duration details.*
115+
116+
```shell
117+
$ serverless invoke --function hello -v
118+
=> action (<ACTION_NAME>) activation (<ID>) duration: 96ms (init: 83ms, wait: 35ms)
119+
{
120+
"payload": "Hello, OpenWhisk!"
121+
}
122+
```
123+
114124
## Writing Functions - Node.js
115125

116126
Here's an `index.js` file containing an example handler function.

invoke/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ class OpenWhiskInvoke {
140140
}
141141

142142
log(invocationReply) {
143+
if (this.options.verbose || this.options.v) {
144+
this.logDetails(invocationReply)
145+
}
146+
143147
let color = 'white';
144148

145149
// error response indicated in-blocking call boolean parameter, success.
@@ -158,6 +162,23 @@ class OpenWhiskInvoke {
158162
return BbPromise.resolve();
159163
}
160164

165+
logDetails(invocationReply) {
166+
const id = `/${invocationReply.namespace}/${invocationReply.name}`
167+
const actv = invocationReply.activationId
168+
169+
const find_time = (annotations, key) => (annotations.find(el => el.key === key) || {value: 0}).value
170+
const annotations = invocationReply.annotations || []
171+
const waitTime = find_time(annotations, 'waitTime')
172+
const initTime = find_time(annotations, 'initTime')
173+
174+
const field = (name, label) => `${chalk.blue(name)} (${chalk.yellow(label)})`
175+
const time = (name, value, color = 'blue') => `${chalk[color](name)}: ${chalk.green(value + 'ms')}`
176+
177+
const duration = (duration, init = 0, wait) => `${time('duration', duration)} (${time('init', init, 'magenta')}, ${time('wait', wait, 'magenta')})`
178+
179+
this.consoleLog(`${chalk.green('=>')} ${field('action', id)} ${field('activation', actv)} ${duration(invocationReply.duration, initTime, waitTime)}`)
180+
}
181+
161182
consoleLog(msg) {
162183
console.log(msg); // eslint-disable-line no-console
163184
}

invoke/tests/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const chalk = require('chalk');
34
const expect = require('chai').expect;
45
const chaiAsPromised = require('chai-as-promised');
56
const sinon = require('sinon');
@@ -217,4 +218,84 @@ describe('OpenWhiskInvoke', () => {
217218
return expect(openwhiskInvoke.invoke()).to.be.eventually.rejected;
218219
});
219220
});
221+
222+
describe('#log()', () => {
223+
it('should log activation response result', () => {
224+
const log = sandbox.stub(openwhiskInvoke, 'consoleLog');
225+
openwhiskInvoke.options.log = 'result'
226+
openwhiskInvoke.options.type = 'blocking'
227+
228+
const result = {success: true, result: { hello: "world"} };
229+
return openwhiskInvoke.log({response: result}).then(() => {
230+
expect(log.calledOnce).to.be.equal(true);
231+
const msg = chalk.white(JSON.stringify(result.result, null, 4));
232+
console.log(msg)
233+
expect(log.args[0][0]).to.be.equal(msg);
234+
});
235+
});
236+
237+
it('should log verbose activation response result', () => {
238+
const log = sandbox.stub(openwhiskInvoke, 'consoleLog');
239+
openwhiskInvoke.options.log = 'result'
240+
openwhiskInvoke.options.type = 'blocking'
241+
openwhiskInvoke.options.v = true
242+
243+
const input = {
244+
activationId: 12345,
245+
name: 'blah',
246+
namespace: 'workspace',
247+
duration: 100,
248+
annotations: [ { key: "waitTime", value: 33 } ],
249+
response: { success: true, result: { hello: "world"} }
250+
};
251+
return openwhiskInvoke.log(input).then(() => {
252+
expect(log.calledTwice).to.be.equal(true);
253+
const msg = chalk.white(JSON.stringify(input.response.result, null, 4));
254+
255+
const field = (name, label) => `${chalk.blue(name)} (${chalk.yellow(label)})`
256+
const time = (name, value, color = 'blue') => `${chalk[color](name)}: ${chalk.green(value + 'ms')}`
257+
const duration = (duration, init = 0, wait) => `${time('duration', duration)} (${time('init', init, 'magenta')}, ${time('wait', wait, 'magenta')})`
258+
259+
const output = `${chalk.green('=>')} ${field('action', '/workspace/blah')} ${field('activation', 12345)} ${duration(100, undefined, 33)}`
260+
261+
expect(log.args[0][0]).to.be.equal(output);
262+
expect(log.args[1][0]).to.be.equal(msg);
263+
});
264+
});
265+
266+
it('should log verbose activation coldstart response result', () => {
267+
const log = sandbox.stub(openwhiskInvoke, 'consoleLog');
268+
openwhiskInvoke.options.log = 'result'
269+
openwhiskInvoke.options.type = 'blocking'
270+
openwhiskInvoke.options.v = true
271+
272+
const input = {
273+
activationId: 12345,
274+
name: 'blah',
275+
namespace: 'workspace',
276+
duration: 100,
277+
annotations: [
278+
{ key: "waitTime", value: 33 },
279+
{ key: "initTime", value: 63 }
280+
],
281+
response: { success: true, result: { hello: "world"} }
282+
};
283+
return openwhiskInvoke.log(input).then(() => {
284+
expect(log.calledTwice).to.be.equal(true);
285+
const msg = chalk.white(JSON.stringify(input.response.result, null, 4));
286+
287+
const field = (name, label) => `${chalk.blue(name)} (${chalk.yellow(label)})`
288+
const time = (name, value, color = 'blue') => `${chalk[color](name)}: ${chalk.green(value + 'ms')}`
289+
const duration = (duration, init = 0, wait) => `${time('duration', duration)} (${time('init', init, 'magenta')}, ${time('wait', wait, 'magenta')})`
290+
291+
const output = `${chalk.green('=>')} ${field('action', '/workspace/blah')} ${field('activation', 12345)} ${duration(100, 63, 33)}`
292+
293+
expect(log.args[0][0]).to.be.equal(output);
294+
expect(log.args[1][0]).to.be.equal(msg);
295+
});
296+
});
297+
298+
299+
300+
});
220301
});

0 commit comments

Comments
 (0)