Skip to content

Commit

Permalink
feat: Add StepHistory class
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Jun 15, 2021
1 parent 0345ffe commit 20b8df5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
User account related utilities that also interact with stored config file. Several methods depend on `sdk` parameter, which should be provided as an instance of `ServerlessSDK` from `@serverless/platform-client` library.

```javascript
const accountUtils = require('@serverless/utils/account);
const accountUtils = require('@serverless/utils/account');
```

Exposes following _async_ methods:
Expand Down
11 changes: 11 additions & 0 deletions docs/telemetry.md
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`.
23 changes: 23 additions & 0 deletions telemetry.js
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,
};
28 changes: 28 additions & 0 deletions test/telemetry.js
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']]));
});
});
});

0 comments on commit 20b8df5

Please sign in to comment.