-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## Utilities related to telemetry | ||
|
||
```javascript | ||
const telemetryUtils = require('@serverless/utils/telemetry'); | ||
``` | ||
|
||
Exposes following classes and methods: | ||
|
||
### `StepHistory` | ||
|
||
Helper class for recording step history for recording telemetry steps in Interactive CLI flow. It extends `Map` class by adding `timestamp` and wrapping value in an object with timestamp and offers `toJSON` method that formats map into JSON-serializable object, as well as `valuesMap` that returns `Map` object that maps original keys to provided `value`. It also offers `valuesMap` method, which just returns regular `Map` without `timestamp`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
class StepHistory extends Map { | ||
set(key, value) { | ||
const valueWithTimestamp = { | ||
value, | ||
timestamp: Date.now(), | ||
}; | ||
return super.set(key, valueWithTimestamp); | ||
} | ||
|
||
valuesMap() { | ||
return new Map(Array.from(this.entries()).map(([key, obj]) => [key, obj.value])); | ||
} | ||
|
||
toJSON() { | ||
return Array.from(this.entries()).map(([key, obj]) => ({ key, ...obj })); | ||
} | ||
} | ||
|
||
module.exports = { | ||
StepHistory, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
const telemetry = require('../telemetry'); | ||
const expect = require('chai').expect; | ||
|
||
describe('telemetry', () => { | ||
describe('StepHistory', () => { | ||
it('stores values within object with a timestamp', () => { | ||
const history = new telemetry.StepHistory(); | ||
history.set('key', 'val'); | ||
expect(history.get('key').value).to.equal('val'); | ||
expect(history.get('key')).to.have.property('timestamp'); | ||
}); | ||
|
||
it('supports `toJSON` method', () => { | ||
const history = new telemetry.StepHistory([['key', 'val']]); | ||
const result = history.toJSON(); | ||
expect(result).to.have.lengthOf(1); | ||
expect(result[0].key).to.equal('key'); | ||
expect(result[0].value).to.equal('val'); | ||
expect(result[0]).to.have.property('timestamp'); | ||
}); | ||
|
||
it('supports `valuesMap` method', () => { | ||
const history = new telemetry.StepHistory([['key', 'val']]); | ||
expect(history.valuesMap()).to.deep.equal(new Map([['key', 'val']])); | ||
}); | ||
}); | ||
}); |