Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ module.exports = {
'prettier',
'prettier/@typescript-eslint',
'plugin:prettier/recommended'
]
],
rules: {
'@typescript-eslint/no-var-requires': 'off'
}
};
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ npm install @datadog/sketches-js
yarn add @datadog/sketches-js
```

When using Protobuf serialization, the [protobufjs](https://www.npmjs.com/package/protobufjs) module must also be installed manually:

```sh
# NPM
npm install protobufjs

# Yarn
yarn add protobufjs
```

## Usage

### Initialize a sketch
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
"typecheck": "tsc --noEmit",
"generate:proto": "pbjs -t static-module -w commonjs -o src/ddsketch/proto/compiled.js src/ddsketch/proto/DDSketch.proto && pbts -o src/ddsketch/proto/compiled.d.ts src/ddsketch/proto/compiled.js"
},
"dependencies": {
"protobufjs": "^6.11.3"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.14",
"@typescript-eslint/eslint-plugin": "^4.2.0",
Expand All @@ -43,6 +41,7 @@
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.4.2",
"prettier": "^2.1.2",
"protobufjs": "^6.11.3",
"ts-jest": "^26.4.0",
"typescript": "^4.0.3"
}
Expand Down
3 changes: 2 additions & 1 deletion src/ddsketch/DDSketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { DenseStore } from './store';
import { Mapping, KeyMapping, LogarithmicMapping } from './mapping';
import { DDSketch as ProtoDDSketch } from './proto/compiled';

const DEFAULT_RELATIVE_ACCURACY = 0.01;

Expand Down Expand Up @@ -186,6 +185,7 @@ class BaseDDSketch {

/** Serialize a DDSketch to protobuf format */
toProto(): Uint8Array {
const ProtoDDSketch = require('./proto/compiled').DDSketch;
const message = ProtoDDSketch.create({
mapping: this.mapping.toProto(),
positiveValues: this.store.toProto(),
Expand All @@ -204,6 +204,7 @@ class BaseDDSketch {
* @param buffer Byte array containing DDSketch in protobuf format (from DDSketch.toProto)
*/
static fromProto(buffer: Uint8Array): DDSketch {
const ProtoDDSketch = require('./proto/compiled').DDSketch;
const decoded = ProtoDDSketch.decode(buffer);
const mapping = KeyMapping.fromProto(decoded.mapping);
const store = DenseStore.fromProto(decoded.positiveValues);
Expand Down
3 changes: 2 additions & 1 deletion src/ddsketch/mapping/CubicallyInterpolatedMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class CubicallyInterpolatedMapping extends KeyMapping {
}

_protoInterpolation(): IndexMappingProto.Interpolation {
return IndexMappingProto.Interpolation.CUBIC;
const { Interpolation } = require('../proto/compiled').IndexMapping;
return Interpolation.CUBIC;
}
}
11 changes: 7 additions & 4 deletions src/ddsketch/mapping/KeyMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class KeyMapping implements Mapping {
}

toProto(): IIndexMapping {
const ProtoIndexMapping = require('../proto/compiled').IndexMapping;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still some static imports to /proto/compiled at the top of this file, does that matter? I would sort of expect that any one of those would cause the module missing error, regardless of if an end user is invoking toProto / fromProto.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the TypeScript compiler is able to understand that actual code isn't used and not add the require call.

return ProtoIndexMapping.create({
gamma: this.gamma,
indexOffset: this._offset,
Expand All @@ -96,17 +97,18 @@ export class KeyMapping implements Mapping {
throw Error('Failed to decode mapping from protobuf');
}

const { Interpolation } = require('../proto/compiled').IndexMapping;
const { interpolation, gamma, indexOffset } = protoMapping;

switch (interpolation) {
case ProtoIndexMapping.Interpolation.NONE:
case Interpolation.NONE:
return LogarithmicMapping.fromGammaOffset(gamma, indexOffset);
case ProtoIndexMapping.Interpolation.LINEAR:
case Interpolation.LINEAR:
return LinearlyInterpolatedMapping.fromGammaOffset(
gamma,
indexOffset
);
case ProtoIndexMapping.Interpolation.CUBIC:
case Interpolation.CUBIC:
return CubicallyInterpolatedMapping.fromGammaOffset(
gamma,
indexOffset
Expand All @@ -127,6 +129,7 @@ export class KeyMapping implements Mapping {
}

_protoInterpolation(): ProtoIndexMapping.Interpolation {
return ProtoIndexMapping.Interpolation.NONE;
const { Interpolation } = require('../proto/compiled').IndexMapping;
return Interpolation.NONE;
}
}
3 changes: 2 additions & 1 deletion src/ddsketch/mapping/LinearlyInterpolatedMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class LinearlyInterpolatedMapping extends KeyMapping {
}

_protoInterpolation(): IndexMappingProto.Interpolation {
return IndexMappingProto.Interpolation.LINEAR;
const { Interpolation } = require('../proto/compiled').IndexMapping;
return Interpolation.LINEAR;
}
}
3 changes: 2 additions & 1 deletion src/ddsketch/mapping/LogarithmicMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class LogarithmicMapping extends KeyMapping {
}

_protoInterpolation(): IndexMappingProto.Interpolation {
return IndexMappingProto.Interpolation.NONE;
const { Interpolation } = require('../proto/compiled').IndexMapping;
return Interpolation.NONE;
}
}
3 changes: 2 additions & 1 deletion src/ddsketch/store/DenseStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { sumOfRange } from './util';
import type { Store } from './types';
import { Store as ProtoStore, IStore } from '../proto/compiled';
import { IStore } from '../proto/compiled';

/** The default number of bins to grow when necessary */
const CHUNK_SIZE = 128;
Expand Down Expand Up @@ -232,6 +232,7 @@ export class DenseStore implements Store<DenseStore> {
}

toProto(): IStore {
const ProtoStore = require('../proto/compiled').Store;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, did you try to get these to work with dynamic import? Benefit would be not needing the lint override.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would change the API since it makes the call asynchronous and uses a promise.

return ProtoStore.create({
contiguousBinCounts: this.bins,
contiguousBinIndexOffset: this.offset
Expand Down
Loading