-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implement variation functions (#298)
- Loading branch information
Showing
21 changed files
with
407 additions
and
134 deletions.
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
85 changes: 85 additions & 0 deletions
85
packages/shared/common/src/internal/evaluation/EventFactoryBase.ts
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,85 @@ | ||
import { LDEvaluationReason, LDFlagValue } from '../../api'; | ||
import Context from '../../Context'; | ||
import { InputCustomEvent, InputEvalEvent, InputIdentifyEvent } from '../events'; | ||
|
||
export type EvalEventArgs = { | ||
addExperimentData?: boolean; | ||
context: Context; | ||
debugEventsUntilDate?: number; | ||
defaultVal: any; | ||
excludeFromSummaries?: boolean; | ||
flagKey: string; | ||
prereqOfFlagKey?: string; | ||
reason?: LDEvaluationReason; | ||
samplingRatio?: number; | ||
trackEvents: boolean; | ||
value: LDFlagValue; | ||
variation?: number; | ||
version: number; | ||
}; | ||
|
||
export default class EventFactoryBase { | ||
constructor(private readonly withReasons: boolean) {} | ||
|
||
evalEvent(e: EvalEventArgs): InputEvalEvent { | ||
return new InputEvalEvent( | ||
this.withReasons, | ||
e.context, | ||
e.flagKey, | ||
e.value, | ||
e.defaultVal, | ||
e.version, | ||
// Exclude null as a possibility. | ||
e.variation ?? undefined, | ||
e.trackEvents || e.addExperimentData, | ||
e.prereqOfFlagKey, | ||
this.withReasons || e.addExperimentData ? e.reason : undefined, | ||
e.debugEventsUntilDate, | ||
e.excludeFromSummaries, | ||
e.samplingRatio, | ||
); | ||
} | ||
|
||
unknownFlagEvent(key: string, defVal: LDFlagValue, context: Context) { | ||
return new InputEvalEvent( | ||
this.withReasons, | ||
context, | ||
key, | ||
defVal, | ||
defVal, | ||
// This isn't ideal, but the purpose of the factory is to at least | ||
// handle this situation. | ||
undefined, // version | ||
undefined, // variation index | ||
undefined, // track events | ||
undefined, // prereqOf | ||
undefined, // reason | ||
undefined, // debugEventsUntilDate | ||
undefined, // exclude from summaries | ||
undefined, // sampling ratio | ||
); | ||
} | ||
|
||
/* eslint-disable-next-line class-methods-use-this */ | ||
identifyEvent(context: Context) { | ||
// Currently sampling for identify events is always 1. | ||
return new InputIdentifyEvent(context, 1); | ||
} | ||
|
||
/* eslint-disable-next-line class-methods-use-this */ | ||
customEvent( | ||
key: string, | ||
context: Context, | ||
data?: any, | ||
metricValue?: number, | ||
samplingRatio: number = 1, | ||
) { | ||
return new InputCustomEvent( | ||
context, | ||
key, | ||
data ?? undefined, | ||
metricValue ?? undefined, | ||
samplingRatio, | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/shared/common/src/internal/evaluation/evaluationDetail.ts
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,18 @@ | ||
import { LDEvaluationReason, LDFlagValue } from '../../api'; | ||
import ErrorKinds from './ErrorKinds'; | ||
|
||
export const createErrorEvaluationDetail = (errorKind: ErrorKinds, def?: LDFlagValue) => ({ | ||
value: def ?? null, | ||
variationIndex: null, | ||
reason: { kind: 'ERROR', errorKind }, | ||
}); | ||
|
||
export const createSuccessEvaluationDetail = ( | ||
value: LDFlagValue, | ||
variationIndex?: number, | ||
reason?: LDEvaluationReason, | ||
) => ({ | ||
value, | ||
variationIndex: variationIndex ?? null, | ||
reason: reason ?? null, | ||
}); |
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 @@ | ||
import ErrorKinds from './ErrorKinds'; | ||
import { createErrorEvaluationDetail, createSuccessEvaluationDetail } from './evaluationDetail'; | ||
import EventFactoryBase, { EvalEventArgs } from './EventFactoryBase'; | ||
|
||
export { | ||
createSuccessEvaluationDetail, | ||
createErrorEvaluationDetail, | ||
ErrorKinds, | ||
EvalEventArgs, | ||
EventFactoryBase, | ||
}; |
2 changes: 0 additions & 2 deletions
2
...s/shared/sdk-server/src/ClientMessages.ts → ...mon/src/internal/events/ClientMessages.ts
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './diagnostics'; | ||
export * from './evaluation'; | ||
export * from './events'; | ||
export * from './stream'; |
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,3 @@ | ||
export default function clone(obj: any) { | ||
return JSON.parse(JSON.stringify(obj)); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import clone from './clone'; | ||
import { secondsToMillis } from './date'; | ||
import { defaultHeaders, httpErrorMessage, LDHeaders } from './http'; | ||
import noop from './noop'; | ||
import sleep from './sleep'; | ||
import { VoidFunction } from './VoidFunction'; | ||
|
||
export { defaultHeaders, httpErrorMessage, noop, LDHeaders, secondsToMillis, sleep, VoidFunction }; | ||
export { | ||
clone, | ||
defaultHeaders, | ||
httpErrorMessage, | ||
noop, | ||
LDHeaders, | ||
secondsToMillis, | ||
sleep, | ||
VoidFunction, | ||
}; |
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,83 @@ | ||
import { LDContext } from '@launchdarkly/js-sdk-common'; | ||
import { basicPlatform } from '@launchdarkly/private-js-mocks'; | ||
|
||
import fetchFlags from './evaluation/fetchFlags'; | ||
import * as mockResponseJson from './evaluation/mockResponse.json'; | ||
import LDClientImpl from './LDClientImpl'; | ||
|
||
jest.mock('./evaluation/fetchFlags', () => { | ||
const actual = jest.requireActual('./evaluation/fetchFlags'); | ||
return { | ||
__esModule: true, | ||
...actual, | ||
default: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('sdk-client object', () => { | ||
const testSdkKey = 'test-sdk-key'; | ||
const context: LDContext = { kind: 'org', key: 'Testy Pizza' }; | ||
const mockFetchFlags = fetchFlags as jest.Mock; | ||
|
||
let ldc: LDClientImpl; | ||
|
||
beforeEach(async () => { | ||
mockFetchFlags.mockResolvedValue(mockResponseJson); | ||
|
||
ldc = new LDClientImpl(testSdkKey, context, basicPlatform, {}); | ||
await ldc.start(); | ||
}); | ||
|
||
test('instantiate with blank options', () => { | ||
expect(ldc.config).toMatchObject({ | ||
allAttributesPrivate: false, | ||
baseUri: 'https://sdk.launchdarkly.com', | ||
capacity: 100, | ||
diagnosticOptOut: false, | ||
diagnosticRecordingInterval: 900, | ||
eventsUri: 'https://events.launchdarkly.com', | ||
flushInterval: 2, | ||
inspectors: [], | ||
logger: { | ||
destination: expect.any(Function), | ||
formatter: expect.any(Function), | ||
logLevel: 1, | ||
name: 'LaunchDarkly', | ||
}, | ||
privateAttributes: [], | ||
sendEvents: true, | ||
sendLDHeaders: true, | ||
serviceEndpoints: { | ||
events: 'https://events.launchdarkly.com', | ||
polling: 'https://sdk.launchdarkly.com', | ||
streaming: 'https://clientstream.launchdarkly.com', | ||
}, | ||
streamInitialReconnectDelay: 1, | ||
streamUri: 'https://clientstream.launchdarkly.com', | ||
tags: {}, | ||
useReport: false, | ||
withReasons: false, | ||
}); | ||
}); | ||
|
||
test('all flags', async () => { | ||
const all = ldc.allFlags(); | ||
|
||
expect(all).toEqual({ | ||
'dev-test-flag': true, | ||
'easter-i-tunes-special': false, | ||
'easter-specials': 'no specials', | ||
fdsafdsafdsafdsa: true, | ||
'log-level': 'warn', | ||
'moonshot-demo': true, | ||
test1: 's1', | ||
'this-is-a-test': true, | ||
}); | ||
}); | ||
|
||
test('variation', async () => { | ||
const devTestFlag = ldc.variation('dev-test-flag'); | ||
|
||
expect(devTestFlag).toBe(true); | ||
}); | ||
}); |
Oops, something went wrong.