-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add exposure service #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhukaihan
wants to merge
8
commits into
main
Choose a base branch
from
add-exposure-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
32c3635
feat: exposure service
zhukaihan f6a9a3b
test: add client test
zhukaihan 2c46cca
fix: add check options
zhukaihan 05d321e
fix: lint
zhukaihan 6d11462
fix: unused imports, comments
zhukaihan 639cbdd
fix: lint
zhukaihan 64da6af
feat: require api key
zhukaihan e961dba
fix: lint
zhukaihan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| import { Cache } from 'src/util/cache'; | ||
|
|
||
| import { Exposure, ExposureFilter } from './exposure'; | ||
| import { DAY_MILLIS } from './exposure-service'; | ||
|
|
||
| export class InMemoryExposureFilter implements ExposureFilter { | ||
| public ttlMillis: number; | ||
| private readonly cache: Cache<number>; | ||
|
|
||
| constructor(size: number, ttlMillis: number = DAY_MILLIS) { | ||
| this.ttlMillis = ttlMillis; | ||
| this.cache = new Cache<number>(size, ttlMillis); | ||
| } | ||
|
|
||
| public shouldTrack(exposure: Exposure): boolean { | ||
| if (Object.keys(exposure.results).length === 0) { | ||
| // Don't track empty exposures. | ||
| return false; | ||
| } | ||
| const canonicalExposure = exposure.canonicalize(); | ||
| const track = this.cache.get(canonicalExposure) == undefined; | ||
| if (track) { | ||
| this.cache.put(canonicalExposure, 0); | ||
| } | ||
| return track; | ||
| } | ||
| } |
This file contains hidden or 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,98 @@ | ||
| import { BaseEvent, CoreClient } from '@amplitude/analytics-types'; | ||
| import { EvaluationVariant } from '@amplitude/experiment-core'; | ||
| import { ExperimentUser } from 'src/types/user'; | ||
zhukaihan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { hashCode } from 'src/util/hash'; | ||
|
|
||
| import { Exposure, ExposureFilter, ExposureService } from './exposure'; | ||
|
|
||
| export const DAY_MILLIS = 24 * 60 * 60 * 1000; | ||
| export const FLAG_TYPE_MUTUAL_EXCLUSION_GROUP = 'mutual-exclusion-group'; | ||
|
|
||
| export class AmplitudeExposureService implements ExposureService { | ||
| private readonly amplitude: CoreClient; | ||
| private readonly exposureFilter: ExposureFilter; | ||
|
|
||
| constructor(amplitude: CoreClient, exposureFilter: ExposureFilter) { | ||
| this.amplitude = amplitude; | ||
| this.exposureFilter = exposureFilter; | ||
| } | ||
|
|
||
| async track(exposure: Exposure): Promise<void> { | ||
| if (this.exposureFilter.shouldTrack(exposure)) { | ||
| toExposureEvents(exposure, this.exposureFilter.ttlMillis).forEach( | ||
| (event) => { | ||
| this.amplitude.logEvent(event); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const toExposureEvents = ( | ||
| exposure: Exposure, | ||
| ttlMillis: number, | ||
| ): BaseEvent[] => { | ||
| const events: BaseEvent[] = []; | ||
| const canonicalExposure = exposure.canonicalize(); | ||
| for (const flagKey in exposure.results) { | ||
| const variant = exposure.results[flagKey]; | ||
|
|
||
| const trackExposure = (variant?.metadata?.trackExposure as boolean) ?? true; | ||
| if (!trackExposure) { | ||
| continue; | ||
| } | ||
|
|
||
| const flagType = variant.metadata?.flagType; | ||
| const isDefault: boolean = variant.metadata?.default as boolean; | ||
| if (isDefault) { | ||
| continue; | ||
| } | ||
|
|
||
| // Determine user properties to set and unset. | ||
| const set = {}; | ||
| const unset = {}; | ||
| if (flagType != FLAG_TYPE_MUTUAL_EXCLUSION_GROUP) { | ||
| if (variant.key) { | ||
| set[`[Experiment] ${flagKey}`] = variant.key; | ||
| } else if (variant.value) { | ||
| set[`[Experiment] ${flagKey}`] = variant.value; | ||
| } | ||
| } | ||
|
|
||
| // Build event properties. | ||
| const eventProperties = {}; | ||
| eventProperties['[Experiment] Flag Key'] = flagKey; | ||
| if (variant.key) { | ||
| eventProperties['[Experiment] Variant'] = variant.key; | ||
| } else if (variant.value) { | ||
| eventProperties['[Experiment] Variant'] = variant.value; | ||
| } | ||
| if (variant.metadata) { | ||
| eventProperties['metadata'] = variant.metadata; | ||
| } | ||
|
|
||
| // Build event. | ||
| const event: BaseEvent = { | ||
| event_type: '[Experiment] Exposure', | ||
| user_id: exposure.user.user_id, | ||
| device_id: exposure.user.device_id, | ||
| event_properties: eventProperties, | ||
| user_properties: { | ||
| $set: set, | ||
| $unset: unset, | ||
| }, | ||
| insert_id: `${exposure.user.user_id} ${ | ||
| exposure.user.device_id | ||
| } ${hashCode(flagKey + ' ' + canonicalExposure)} ${Math.floor( | ||
| exposure.timestamp / ttlMillis, | ||
| )}`, | ||
| }; | ||
| if (exposure.user.groups) { | ||
| event.groups = exposure.user.groups; | ||
| } | ||
|
|
||
| events.push(event); | ||
| } | ||
|
|
||
| return events; | ||
| }; | ||
This file contains hidden or 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,40 @@ | ||
| import { EvaluationVariant } from '@amplitude/experiment-core'; | ||
| import { ExperimentUser } from 'src/types/user'; | ||
|
|
||
| export interface ExposureService { | ||
| track(exposure: Exposure): Promise<void>; | ||
| } | ||
|
|
||
| export interface ExposureFilter { | ||
| shouldTrack(exposure: Exposure): boolean; | ||
| ttlMillis: number; | ||
| } | ||
|
|
||
| /** | ||
| * Exposure is a class that represents a user's exposure to a set of flags. | ||
| * It implements the FilterItem interface so it can be used with the dedupe filter. | ||
zhukaihan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| export class Exposure { | ||
| public user: ExperimentUser; | ||
| public results: Record<string, EvaluationVariant>; | ||
| public timestamp: number = Date.now(); | ||
|
|
||
| public constructor( | ||
| user: ExperimentUser, | ||
| results: Record<string, EvaluationVariant>, | ||
| ) { | ||
| this.user = user; | ||
| this.results = results; | ||
| } | ||
|
|
||
| public canonicalize(): string { | ||
| let canonical = `${this.user.user_id?.trim()} ${this.user.device_id?.trim()} `; | ||
| for (const key of Object.keys(this.results).sort()) { | ||
| const variant = this.results[key]; | ||
| if (variant?.key) { | ||
| canonical += key.trim() + ' ' + variant?.key?.trim() + ' '; | ||
| } | ||
| } | ||
| return canonical; | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.