Skip to content

Commit

Permalink
hotfix: GrowthBook feature value evaluation (#121)
Browse files Browse the repository at this point in the history
* fix: update deprecated SDK references and revert platform source usage

* chore: re-instate header middleware and add growth analytics attributes

* chore: remove unnecessary headers
  • Loading branch information
wax911 authored May 12, 2024
1 parent 5618fb1 commit 4a2bec2
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/common/core/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import state from './setup.ts';
import timing from '../middleware/timing.ts';
import error from '../middleware/error.ts';
import growth from '../middleware/growth.ts';
import header from '../middleware/header.ts';
import targeting from '../middleware/targeting.ts';
import { logger } from './logger.ts';
import { between } from 'x/optic';
import _localSourceFactory from '../mongo/factory.ts';
Expand All @@ -19,7 +21,9 @@ export default (opts: FactoryOptions): Application => {

app.use(
timing,
header,
growth,
targeting,
error,
);

Expand Down
1 change: 0 additions & 1 deletion src/common/core/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const applicationState: State = {
authorization: null,
contentType: null,
acceptEncoding: '',
forwarded: '',
language: '',
},
local: await _localSourceFactory.connect(),
Expand Down
2 changes: 1 addition & 1 deletion src/common/experiment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const isNewsApiv2Enabled = (growth: Features): boolean =>
export const getPlatformSource = (
growth: Features,
): PlatformSource | undefined =>
invoke(() => growth.getFeatureValue('api-platform-source', undefined));
invoke(() => growth.getFeatureValue('platform-source', undefined));

export const isAnalyticsEnabled = (growth: Features): boolean =>
invoke(() => growth.isOn('enable-analytics'));
2 changes: 1 addition & 1 deletion src/common/experiment/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface PlatformSource {
}

export interface AppFeatures {
'api-platform-source'?: PlatformSource;
'platform-source'?: PlatformSource;
'news-refactor-api': boolean;
'enable-analytics': boolean;
}
3 changes: 1 addition & 2 deletions src/common/middleware/growth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export default async (
next: () => Promise<unknown>,
) => {
logger.mark('load-features-start');
await state.features.loadFeatures({
autoRefresh: false,
await state.features.init({
timeout: 2000,
})
.then(() => {
Expand Down
9 changes: 4 additions & 5 deletions src/common/middleware/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ const pass = async (ctx: AppContext, next: () => Promise<unknown>) => {
agent: userAgent,
contentType: headers.get('content-type'),
acceptEncoding: headers.get('accept-encoding')!,
forwarded: headers.get('x-forwarded-for'),
language: headers.get('accept-language')!,
application: {
locale: headers.get('x-app-name'),
locale: headers.get('x-app-locale'),
version: headers.get('x-app-version'),
source: headers.get('x-app-code'),
code: headers.get('x-app-source'),
label: headers.get('x-app-locale'),
source: headers.get('x-app-source'),
code: headers.get('x-app-code'),
label: headers.get('x-app-name'),
buildType: headers.get('x-app-build-type'),
},
};
Expand Down
4 changes: 0 additions & 4 deletions src/common/middleware/index.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/common/middleware/targeting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { between } from 'x/optic';
import { logger } from '../core/logger.ts';
import type { AppContext } from '../types/core.d.ts';

export default async (
{ state }: AppContext,
next: () => Promise<unknown>,
) => {
logger.mark('set-attributes-start');
const { application, agent } = state.contextHeader;
let attributes: Record<string, unknown> = {};
if (application) {
attributes = {
'app_version': application.version,
'app_source': application.source,
'app_code': application.code,
'app_build_type': application.buildType,
'app_label': application.label,
'app_locale': application.locale,
};
}
await state.features.setAttributes({
'user_agent': agent,
...attributes,
}).then(() => {
logger.mark('set-attributes-end');
logger.measure(between('set-attributes-start', 'set-attributes-end'));
}).catch((e) => {
logger.error('set-attributes encountered an error', e);
}).finally(async () => {
await next();
});
};
1 change: 0 additions & 1 deletion src/common/types/state.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type ContextHeader = {
authorization: string | null;
contentType: string | null;
acceptEncoding: string;
forwarded: string | null;
language: string;
application?: {
locale: string | null;
Expand Down
12 changes: 6 additions & 6 deletions src/config/transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export const transform: Transform<
},
genres: data.document?.genres,
image: {
banner: `${platformSource?.media}${image?.banner}`,
poster: `${platformSource?.media}${image?.poster}`,
loading: `${platformSource?.media}${image?.loading}`,
error: `${platformSource?.media}${image?.error}`,
info: `${platformSource?.media}${image?.info}`,
default: `${platformSource?.media}${image?.default}`,
banner: image?.banner ?? '',
poster: image?.poster ?? '',
loading: image?.loading ?? '',
error: image?.error ?? '',
info: image?.info ?? '',
default: image?.default ?? '',
},
navigation: data.document?.navigation,
};
Expand Down

0 comments on commit 4a2bec2

Please sign in to comment.