Skip to content

Commit

Permalink
Export Layer and DynamicConfig (#227)
Browse files Browse the repository at this point in the history
* Export Layer and DynamicConfig

* Feedback - remove dependnecy on store from EvalDetails

* export as Statsig to remove _instance
  • Loading branch information
tore-statsig authored Jan 31, 2023
1 parent d4759b6 commit eb0e1a0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 101 deletions.
136 changes: 68 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "statsig-node",
"version": "5.2.0",
"version": "5.2.1",
"description": "Statsig Node.js SDK for usage in multi-user server environments.",
"main": "dist/index.js",
"scripts": {
Expand Down
26 changes: 11 additions & 15 deletions src/EvaluationDetails.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import SpecStore from './SpecStore';

export type EvaluationReason =
| 'Network'
| 'LocalOverride'
| 'Unrecognized'
| 'Uninitialized'
| 'Bootstrap'
| 'DataAdapter';
import { EvaluationReason } from './EvaluationReason';

export class EvaluationDetails {
readonly configSyncTime: number;
Expand All @@ -25,15 +17,19 @@ export class EvaluationDetails {
this.serverTime = Date.now();
}

static uninitialized(): EvaluationDetails {
return new EvaluationDetails(0, 0, 'Uninitialized');
static uninitialized() {
return new EvaluationDetails(
0,
0,
'Uninitialized',
);
}

static make(store: SpecStore, reason?: EvaluationReason) {
static make(lastUpdateTime: number, initialUpdateTime: number, reason: EvaluationReason) {
return new EvaluationDetails(
store.getLastUpdateTime(),
store.getInitialUpdateTime(),
reason ?? store.getInitReason(),
initialUpdateTime,
lastUpdateTime,
reason,
);
}
}
7 changes: 7 additions & 0 deletions src/EvaluationReason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type EvaluationReason =
| 'Network'
| 'LocalOverride'
| 'Unrecognized'
| 'Uninitialized'
| 'Bootstrap'
| 'DataAdapter';
24 changes: 15 additions & 9 deletions src/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class Evaluator {
const override = this.lookupGateOverride(user, gateName);
if (override) {
return override.withEvaluationDetails(
EvaluationDetails.make(this.store, 'LocalOverride'),
EvaluationDetails.make(this.store.getLastUpdateTime(), this.store.getInitialUpdateTime(), 'LocalOverride'),
);
}

Expand All @@ -112,7 +112,7 @@ export default class Evaluator {
const override = this.lookupConfigOverride(user, configName);
if (override) {
return override.withEvaluationDetails(
EvaluationDetails.make(this.store, 'LocalOverride'),
EvaluationDetails.make(this.store.getLastUpdateTime(), this.store.getInitialUpdateTime(), 'LocalOverride'),
);
}

Expand All @@ -129,7 +129,7 @@ export default class Evaluator {
const override = this.lookupLayerOverride(user, layerName);
if (override) {
return override.withEvaluationDetails(
EvaluationDetails.make(this.store, 'LocalOverride'),
EvaluationDetails.make(this.store.getLastUpdateTime(), this.store.getInitialUpdateTime(), 'LocalOverride'),
);
}

Expand Down Expand Up @@ -387,12 +387,18 @@ export default class Evaluator {
_evalConfig(user: StatsigUser, config: ConfigSpec | null): ConfigEvaluation {
if (!config) {
return new ConfigEvaluation(false).withEvaluationDetails(
EvaluationDetails.make(this.store, 'Unrecognized'),
EvaluationDetails.make(this.store.getLastUpdateTime(), this.store.getInitialUpdateTime(), 'Unrecognized'),
);
}

const evaulation = this._eval(user, config);
return evaulation.withEvaluationDetails(EvaluationDetails.make(this.store));
return evaulation.withEvaluationDetails(
EvaluationDetails.make(
this.store.getLastUpdateTime(),
this.store.getInitialUpdateTime(),
this.store.getInitReason(),
),
);
}

_eval(user: StatsigUser, config: ConfigSpec): ConfigEvaluation {
Expand Down Expand Up @@ -478,10 +484,10 @@ export default class Evaluator {
_evalPassPercent(user: StatsigUser, rule: ConfigRule, config: ConfigSpec) {
const hash = computeUserHash(
config.salt +
'.' +
(rule.salt ?? rule.id) +
'.' +
(this._getUnitID(user, rule.idType) ?? ''),
'.' +
(rule.salt ?? rule.id) +
'.' +
(this._getUnitID(user, rule.idType) ?? ''),
);
return (
Number(hash % BigInt(CONDITION_SEGMENT_COUNT)) < rule.passPercentage * 100
Expand Down
Loading

0 comments on commit eb0e1a0

Please sign in to comment.