Skip to content

Commit 7885261

Browse files
authored
[FSSDK-11638] cleanups in event processing (#1073)
## Summary - remove redundant functions - small update in batch event processor eventStoreInCount maintenance logic
1 parent 41fed0d commit 7885261

File tree

4 files changed

+78
-108
lines changed

4 files changed

+78
-108
lines changed

lib/event_processor/batch_event_processor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
266266
}
267267

268268
private async readEventCountInStore(store: Store<EventWithId>): Promise<void> {
269+
if (this.eventCountInStore !== undefined) {
270+
return;
271+
}
272+
269273
try {
270274
const keys = await store.getKeys();
271275
this.eventCountInStore = keys.length;

lib/event_processor/event_builder/log_event.spec.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
import { describe, it, expect } from 'vitest';
1717

1818
import {
19-
buildConversionEventV1,
20-
buildImpressionEventV1,
2119
makeEventBatch,
2220
} from './log_event';
2321

2422
import { ImpressionEvent, ConversionEvent } from './user_event';
2523

26-
describe('buildImpressionEventV1', () => {
27-
it('should build an ImpressionEventV1 when experiment and variation are defined', () => {
24+
describe('makeEventBatch', () => {
25+
it('should build a batch with single impression event when experiment and variation are defined', () => {
2826
const impressionEvent: ImpressionEvent = {
2927
type: 'impression',
3028
timestamp: 69,
@@ -65,7 +63,7 @@ describe('buildImpressionEventV1', () => {
6563
enabled: true,
6664
}
6765

68-
const result = buildImpressionEventV1(impressionEvent)
66+
const result = makeEventBatch([impressionEvent])
6967
expect(result).toEqual({
7068
client_name: 'node-sdk',
7169
client_version: '3.0.0',
@@ -123,7 +121,7 @@ describe('buildImpressionEventV1', () => {
123121
})
124122
})
125123

126-
it('should build an ImpressionEventV1 when experiment and variation are not defined', () => {
124+
it('should build a batch with simlge impression event when experiment and variation are not defined', () => {
127125
const impressionEvent: ImpressionEvent = {
128126
type: 'impression',
129127
timestamp: 69,
@@ -164,7 +162,7 @@ describe('buildImpressionEventV1', () => {
164162
enabled: true,
165163
}
166164

167-
const result = buildImpressionEventV1(impressionEvent)
165+
const result = makeEventBatch([impressionEvent])
168166
expect(result).toEqual({
169167
client_name: 'node-sdk',
170168
client_version: '3.0.0',
@@ -220,11 +218,9 @@ describe('buildImpressionEventV1', () => {
220218
},
221219
],
222220
})
223-
})
224-
})
221+
});
225222

226-
describe('buildConversionEventV1', () => {
227-
it('should build a ConversionEventV1 when tags object is defined', () => {
223+
it('should build a batch with single conversion event when tags object is defined', () => {
228224
const conversionEvent: ConversionEvent = {
229225
type: 'conversion',
230226
timestamp: 69,
@@ -260,7 +256,7 @@ describe('buildConversionEventV1', () => {
260256
value: 123,
261257
}
262258

263-
const result = buildConversionEventV1(conversionEvent)
259+
const result = makeEventBatch([conversionEvent])
264260
expect(result).toEqual({
265261
client_name: 'node-sdk',
266262
client_version: '3.0.0',
@@ -311,7 +307,7 @@ describe('buildConversionEventV1', () => {
311307
})
312308
})
313309

314-
it('should build a ConversionEventV1 when tags object is undefined', () => {
310+
it('should build a batch with single conversion event when when tags object is undefined', () => {
315311
const conversionEvent: ConversionEvent = {
316312
type: 'conversion',
317313
timestamp: 69,
@@ -343,7 +339,7 @@ describe('buildConversionEventV1', () => {
343339
value: 123,
344340
}
345341

346-
const result = buildConversionEventV1(conversionEvent)
342+
const result = makeEventBatch([conversionEvent])
347343
expect(result).toEqual({
348344
client_name: 'node-sdk',
349345
client_version: '3.0.0',
@@ -390,7 +386,7 @@ describe('buildConversionEventV1', () => {
390386
})
391387
})
392388

393-
it('should build a ConversionEventV1 when event id is null', () => {
389+
it('should build a batch with single conversion event when event id is null', () => {
394390
const conversionEvent: ConversionEvent = {
395391
type: 'conversion',
396392
timestamp: 69,
@@ -422,7 +418,7 @@ describe('buildConversionEventV1', () => {
422418
value: 123,
423419
}
424420

425-
const result = buildConversionEventV1(conversionEvent)
421+
const result = makeEventBatch([conversionEvent])
426422
expect(result).toEqual({
427423
client_name: 'node-sdk',
428424
client_version: '3.0.0',
@@ -469,7 +465,7 @@ describe('buildConversionEventV1', () => {
469465
})
470466
})
471467

472-
it('should include revenue and value if they are 0', () => {
468+
it('should include revenue and value for conversion events if they are 0', () => {
473469
const conversionEvent: ConversionEvent = {
474470
type: 'conversion',
475471
timestamp: 69,
@@ -505,7 +501,7 @@ describe('buildConversionEventV1', () => {
505501
value: 0,
506502
}
507503

508-
const result = buildConversionEventV1(conversionEvent)
504+
const result = makeEventBatch([conversionEvent])
509505
expect(result).toEqual({
510506
client_name: 'node-sdk',
511507
client_version: '3.0.0',
@@ -591,7 +587,7 @@ describe('buildConversionEventV1', () => {
591587
value: 123,
592588
}
593589

594-
const result = buildConversionEventV1(conversionEvent)
590+
const result = makeEventBatch([conversionEvent])
595591
expect(result).toEqual({
596592
client_name: 'node-sdk',
597593
client_version: '3.0.0',
@@ -635,9 +631,7 @@ describe('buildConversionEventV1', () => {
635631
],
636632
})
637633
})
638-
})
639634

640-
describe('makeEventBatch', () => {
641635
it('should batch Conversion and Impression events together', () => {
642636
const conversionEvent: ConversionEvent = {
643637
type: 'conversion',

lib/event_processor/event_builder/log_event.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -214,48 +214,6 @@ function makeVisitor(data: ImpressionEvent | ConversionEvent): Visitor {
214214
return visitor
215215
}
216216

217-
/**
218-
* Event for usage with v1 logtier
219-
*
220-
* @export
221-
* @interface EventBuilderV1
222-
*/
223-
export function buildImpressionEventV1(data: ImpressionEvent): EventBatch {
224-
const visitor = makeVisitor(data)
225-
visitor.snapshots.push(makeDecisionSnapshot(data))
226-
227-
return {
228-
client_name: data.context.clientName,
229-
client_version: data.context.clientVersion,
230-
231-
account_id: data.context.accountId,
232-
project_id: data.context.projectId,
233-
revision: data.context.revision,
234-
anonymize_ip: data.context.anonymizeIP,
235-
enrich_decisions: true,
236-
237-
visitors: [visitor],
238-
}
239-
}
240-
241-
export function buildConversionEventV1(data: ConversionEvent): EventBatch {
242-
const visitor = makeVisitor(data)
243-
visitor.snapshots.push(makeConversionSnapshot(data))
244-
245-
return {
246-
client_name: data.context.clientName,
247-
client_version: data.context.clientVersion,
248-
249-
account_id: data.context.accountId,
250-
project_id: data.context.projectId,
251-
revision: data.context.revision,
252-
anonymize_ip: data.context.anonymizeIP,
253-
enrich_decisions: true,
254-
255-
visitors: [visitor],
256-
}
257-
}
258-
259217
export function buildLogEvent(events: UserEvent[]): LogEvent {
260218
return {
261219
url: 'https://logx.optimizely.com/v1/events',

lib/event_processor/event_builder/user_event.ts

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ type EventContext = {
4444
botFiltering?: boolean;
4545
}
4646

47-
export type BaseUserEvent = {
48-
type: 'impression' | 'conversion';
47+
type EventType = 'impression' | 'conversion';
48+
49+
50+
export type BaseUserEvent<T extends EventType> = {
51+
type: T;
4952
timestamp: number;
5053
uuid: string;
5154
context: EventContext;
@@ -55,9 +58,7 @@ export type BaseUserEvent = {
5558
};
5659
};
5760

58-
export type ImpressionEvent = BaseUserEvent & {
59-
type: 'impression';
60-
61+
export type ImpressionEvent = BaseUserEvent<'impression'> & {
6162
layer: {
6263
id: string | null;
6364
} | null;
@@ -79,9 +80,7 @@ export type ImpressionEvent = BaseUserEvent & {
7980
cmabUuid?: string;
8081
};
8182

82-
export type ConversionEvent = BaseUserEvent & {
83-
type: 'conversion';
84-
83+
export type ConversionEvent = BaseUserEvent<'conversion'> & {
8584
event: {
8685
id: string | null;
8786
key: string;
@@ -108,6 +107,42 @@ export const areEventContextsEqual = (eventA: UserEvent, eventB: UserEvent): boo
108107
)
109108
}
110109

110+
const buildBaseEvent = <T extends EventType>({
111+
configObj,
112+
userId,
113+
userAttributes,
114+
clientEngine,
115+
clientVersion,
116+
type,
117+
}: {
118+
configObj: ProjectConfig;
119+
userId: string;
120+
userAttributes?: UserAttributes;
121+
clientEngine: string;
122+
clientVersion: string;
123+
type: T;
124+
}): BaseUserEvent<T> => {
125+
return {
126+
type,
127+
timestamp: fns.currentTimestamp(),
128+
uuid: fns.uuid(),
129+
context: {
130+
accountId: configObj.accountId,
131+
projectId: configObj.projectId,
132+
revision: configObj.revision,
133+
clientName: clientEngine,
134+
clientVersion: clientVersion,
135+
anonymizeIP: configObj.anonymizeIP || false,
136+
botFiltering: configObj.botFiltering,
137+
},
138+
user: {
139+
id: userId,
140+
attributes: buildVisitorAttributes(configObj, userAttributes),
141+
},
142+
};
143+
144+
}
145+
111146
export type ImpressionConfig = {
112147
decisionObj: DecisionObj;
113148
userId: string;
@@ -119,7 +154,6 @@ export type ImpressionConfig = {
119154
configObj: ProjectConfig;
120155
}
121156

122-
123157
/**
124158
* Creates an ImpressionEvent object from decision data
125159
* @param {ImpressionConfig} config
@@ -146,24 +180,14 @@ export const buildImpressionEvent = function({
146180
const layerId = experimentId !== null ? getLayerId(configObj, experimentId) : null;
147181

148182
return {
149-
type: 'impression',
150-
timestamp: fns.currentTimestamp(),
151-
uuid: fns.uuid(),
152-
153-
user: {
154-
id: userId,
155-
attributes: buildVisitorAttributes(configObj, userAttributes),
156-
},
157-
158-
context: {
159-
accountId: configObj.accountId,
160-
projectId: configObj.projectId,
161-
revision: configObj.revision,
162-
clientName: clientEngine,
163-
clientVersion: clientVersion,
164-
anonymizeIP: configObj.anonymizeIP || false,
165-
botFiltering: configObj.botFiltering,
166-
},
183+
...buildBaseEvent({
184+
configObj,
185+
userId,
186+
userAttributes,
187+
clientEngine,
188+
clientVersion,
189+
type: 'impression',
190+
}),
167191

168192
layer: {
169193
id: layerId,
@@ -218,24 +242,14 @@ export const buildConversionEvent = function({
218242
const eventValue = eventTags ? eventTagUtils.getEventValue(eventTags, logger) : null;
219243

220244
return {
221-
type: 'conversion',
222-
timestamp: fns.currentTimestamp(),
223-
uuid: fns.uuid(),
224-
225-
user: {
226-
id: userId,
227-
attributes: buildVisitorAttributes(configObj, userAttributes),
228-
},
229-
230-
context: {
231-
accountId: configObj.accountId,
232-
projectId: configObj.projectId,
233-
revision: configObj.revision,
234-
clientName: clientEngine,
235-
clientVersion: clientVersion,
236-
anonymizeIP: configObj.anonymizeIP || false,
237-
botFiltering: configObj.botFiltering,
238-
},
245+
...buildBaseEvent({
246+
configObj,
247+
userId,
248+
userAttributes,
249+
clientEngine,
250+
clientVersion,
251+
type: 'conversion',
252+
}),
239253

240254
event: {
241255
id: eventId,

0 commit comments

Comments
 (0)