Skip to content

[FSSDK-10941] event processor files and directories cleanup - part 2 #967

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

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import { getMockAsyncCache } from '../tests/mock/mock_cache';

import { EventWithId } from './batch_event_processor';
import { formatEvents } from './event_builder/build_event_v1';
import { buildLogEvent } from './event_builder/build_event_v1';

Check failure on line 53 in lib/event_processor/batch_event_processor.react_native.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

Check failure on line 53 in lib/event_processor/batch_event_processor.react_native.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

Check failure on line 53 in lib/event_processor/batch_event_processor.react_native.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.
import { createImpressionEvent } from '../tests/mock/create_event';
import { ProcessableEvent } from './event_processor';

Expand Down Expand Up @@ -138,7 +138,7 @@

await exhaustMicrotasks();

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledWith(formatEvents(events));
expect(eventDispatcher.dispatchEvent).toHaveBeenCalledWith(buildLogEvent(events));
});

it('should unsubscribe from netinfo listener when stopped', async () => {
Expand Down
58 changes: 29 additions & 29 deletions lib/event_processor/batch_event_processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { getMockSyncCache } from '../tests/mock/mock_cache';
import { createImpressionEvent } from '../tests/mock/create_event';
import { ProcessableEvent } from './event_processor';
import { formatEvents } from './event_builder/build_event_v1';
import { buildLogEvent } from './event_builder/build_event_v1';

Check failure on line 22 in lib/event_processor/batch_event_processor.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

Check failure on line 22 in lib/event_processor/batch_event_processor.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

Check failure on line 22 in lib/event_processor/batch_event_processor.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.
import { resolvablePromise } from '../utils/promise/resolvablePromise';
import { advanceTimersByTime } from '../../tests/testUtils';
import { getMockLogger } from '../tests/mock/mock_logger';
Expand Down Expand Up @@ -139,9 +139,9 @@
await exhaustMicrotasks();

expect(mockDispatch).toHaveBeenCalledTimes(3);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents([events[0], events[1]]));
expect(mockDispatch.mock.calls[1][0]).toEqual(formatEvents([events[2], events[3]]));
expect(mockDispatch.mock.calls[2][0]).toEqual(formatEvents([events[4]]));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent([events[0], events[1]]));
expect(mockDispatch.mock.calls[1][0]).toEqual(buildLogEvent([events[2], events[3]]));
expect(mockDispatch.mock.calls[2][0]).toEqual(buildLogEvent([events[4]]));
});
});

Expand Down Expand Up @@ -202,7 +202,7 @@
await processor.process(event);

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(1);
expect(eventDispatcher.dispatchEvent.mock.calls[0][0]).toEqual(formatEvents(events));
expect(eventDispatcher.dispatchEvent.mock.calls[0][0]).toEqual(buildLogEvent(events));

events = [event];
for(let i = 101; i < 200; i++) {
Expand All @@ -217,7 +217,7 @@
await processor.process(event);

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(2);
expect(eventDispatcher.dispatchEvent.mock.calls[1][0]).toEqual(formatEvents(events));
expect(eventDispatcher.dispatchEvent.mock.calls[1][0]).toEqual(buildLogEvent(events));
});

it('should flush queue is context of the new event is different and enqueue the new event', async () => {
Expand Down Expand Up @@ -250,11 +250,11 @@
await processor.process(newEvent);

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(1);
expect(eventDispatcher.dispatchEvent.mock.calls[0][0]).toEqual(formatEvents(events));
expect(eventDispatcher.dispatchEvent.mock.calls[0][0]).toEqual(buildLogEvent(events));

await dispatchRepeater.execute(0);
expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(2);
expect(eventDispatcher.dispatchEvent.mock.calls[1][0]).toEqual(formatEvents([newEvent]));
expect(eventDispatcher.dispatchEvent.mock.calls[1][0]).toEqual(buildLogEvent([newEvent]));
});

it('should store the event in the eventStore with increasing ids', async () => {
Expand Down Expand Up @@ -313,7 +313,7 @@
await dispatchRepeater.execute(0);

expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(1);
expect(eventDispatcher.dispatchEvent.mock.calls[0][0]).toEqual(formatEvents(events));
expect(eventDispatcher.dispatchEvent.mock.calls[0][0]).toEqual(buildLogEvent(events));

events = [];
for(let i = 1; i < 15; i++) {
Expand All @@ -324,7 +324,7 @@

await dispatchRepeater.execute(0);
expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(2);
expect(eventDispatcher.dispatchEvent.mock.calls[1][0]).toEqual(formatEvents(events));
expect(eventDispatcher.dispatchEvent.mock.calls[1][0]).toEqual(buildLogEvent(events));
});

it('should not retry failed dispatch if retryConfig is not provided', async () => {
Expand Down Expand Up @@ -397,7 +397,7 @@
expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(4);
expect(backoffController.backoff).toHaveBeenCalledTimes(3);

const request = formatEvents(events);
const request = buildLogEvent(events);
for(let i = 0; i < 4; i++) {
expect(eventDispatcher.dispatchEvent.mock.calls[i][0]).toEqual(request);
}
Expand Down Expand Up @@ -444,7 +444,7 @@
expect(eventDispatcher.dispatchEvent).toHaveBeenCalledTimes(201);
expect(backoffController.backoff).toHaveBeenCalledTimes(200);

const request = formatEvents(events);
const request = buildLogEvent(events);
for(let i = 0; i < 201; i++) {
expect(eventDispatcher.dispatchEvent.mock.calls[i][0]).toEqual(request);
}
Expand Down Expand Up @@ -723,7 +723,7 @@
await exhaustMicrotasks();

expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents(failedEvents));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent(failedEvents));

const eventsInStore = [...cache.getAll().values()].sort((a, b) => a.id < b.id ? -1 : 1).map(e => e.event);
expect(eventsInStore).toEqual(expect.arrayContaining([
Expand Down Expand Up @@ -761,7 +761,7 @@
dispatchRepeater.execute(0);
await exhaustMicrotasks();
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents([eventA, eventB]));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent([eventA, eventB]));

const failedEvents: ProcessableEvent[] = [];

Expand All @@ -776,7 +776,7 @@
await exhaustMicrotasks();

expect(mockDispatch).toHaveBeenCalledTimes(2);
expect(mockDispatch.mock.calls[1][0]).toEqual(formatEvents(failedEvents));
expect(mockDispatch.mock.calls[1][0]).toEqual(buildLogEvent(failedEvents));

mockResult2.resolve({});
await exhaustMicrotasks();
Expand Down Expand Up @@ -826,10 +826,10 @@
// events 0 1 4 5 6 7 have one context, and 2 3 have different context
// batches should be [0, 1], [2, 3], [4, 5, 6], [7]
expect(mockDispatch).toHaveBeenCalledTimes(4);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents([failedEvents[0], failedEvents[1]]));
expect(mockDispatch.mock.calls[1][0]).toEqual(formatEvents([failedEvents[2], failedEvents[3]]));
expect(mockDispatch.mock.calls[2][0]).toEqual(formatEvents([failedEvents[4], failedEvents[5], failedEvents[6]]));
expect(mockDispatch.mock.calls[3][0]).toEqual(formatEvents([failedEvents[7]]));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent([failedEvents[0], failedEvents[1]]));
expect(mockDispatch.mock.calls[1][0]).toEqual(buildLogEvent([failedEvents[2], failedEvents[3]]));
expect(mockDispatch.mock.calls[2][0]).toEqual(buildLogEvent([failedEvents[4], failedEvents[5], failedEvents[6]]));
expect(mockDispatch.mock.calls[3][0]).toEqual(buildLogEvent([failedEvents[7]]));
});
});

Expand Down Expand Up @@ -873,7 +873,7 @@
await exhaustMicrotasks();

expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents(failedEvents));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent(failedEvents));

const eventsInStore = [...cache.getAll().values()].sort((a, b) => a.id < b.id ? -1 : 1).map(e => e.event);
expect(eventsInStore).toEqual(expect.arrayContaining([
Expand Down Expand Up @@ -913,7 +913,7 @@
dispatchRepeater.execute(0);
await exhaustMicrotasks();
expect(mockDispatch).toHaveBeenCalledTimes(1);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents([eventA, eventB]));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent([eventA, eventB]));

const failedEvents: ProcessableEvent[] = [];

Expand All @@ -928,7 +928,7 @@
await exhaustMicrotasks();

expect(mockDispatch).toHaveBeenCalledTimes(2);
expect(mockDispatch.mock.calls[1][0]).toEqual(formatEvents(failedEvents));
expect(mockDispatch.mock.calls[1][0]).toEqual(buildLogEvent(failedEvents));

mockResult2.resolve({});
await exhaustMicrotasks();
Expand Down Expand Up @@ -980,10 +980,10 @@
// events 0 1 4 5 6 7 have one context, and 2 3 have different context
// batches should be [0, 1], [2, 3], [4, 5, 6], [7]
expect(mockDispatch).toHaveBeenCalledTimes(4);
expect(mockDispatch.mock.calls[0][0]).toEqual(formatEvents([failedEvents[0], failedEvents[1]]));
expect(mockDispatch.mock.calls[1][0]).toEqual(formatEvents([failedEvents[2], failedEvents[3]]));
expect(mockDispatch.mock.calls[2][0]).toEqual(formatEvents([failedEvents[4], failedEvents[5], failedEvents[6]]));
expect(mockDispatch.mock.calls[3][0]).toEqual(formatEvents([failedEvents[7]]));
expect(mockDispatch.mock.calls[0][0]).toEqual(buildLogEvent([failedEvents[0], failedEvents[1]]));
expect(mockDispatch.mock.calls[1][0]).toEqual(buildLogEvent([failedEvents[2], failedEvents[3]]));
expect(mockDispatch.mock.calls[2][0]).toEqual(buildLogEvent([failedEvents[4], failedEvents[5], failedEvents[6]]));
expect(mockDispatch.mock.calls[3][0]).toEqual(buildLogEvent([failedEvents[7]]));
});
});

Expand Down Expand Up @@ -1012,7 +1012,7 @@
await dispatchRepeater.execute(0);

expect(dispatchListener).toHaveBeenCalledTimes(1);
expect(dispatchListener.mock.calls[0][0]).toEqual(formatEvents([event, event2]));
expect(dispatchListener.mock.calls[0][0]).toEqual(buildLogEvent([event, event2]));
});

it('should remove event handler when function returned from onDispatch is called', async () => {
Expand Down Expand Up @@ -1041,7 +1041,7 @@
await dispatchRepeater.execute(0);

expect(dispatchListener).toHaveBeenCalledTimes(1);
expect(dispatchListener.mock.calls[0][0]).toEqual(formatEvents([event, event2]));
expect(dispatchListener.mock.calls[0][0]).toEqual(buildLogEvent([event, event2]));

unsub();

Expand Down Expand Up @@ -1119,7 +1119,7 @@

processor.stop();
expect(closingEventDispatcher.dispatchEvent).toHaveBeenCalledTimes(1);
expect(closingEventDispatcher.dispatchEvent).toHaveBeenCalledWith(formatEvents(events));
expect(closingEventDispatcher.dispatchEvent).toHaveBeenCalledWith(buildLogEvent(events));
});

it('should cancel retry of active dispatches', async () => {
Expand Down
20 changes: 10 additions & 10 deletions lib/event_processor/batch_event_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import { EventProcessor, ProcessableEvent } from "./event_processor";
import { Cache } from "../utils/cache/cache";
import { EventDispatcher, EventDispatcherResponse, EventV1Request } from "./event_dispatcher";
import { formatEvents } from "./event_builder/build_event_v1";
import { EventDispatcher, EventDispatcherResponse, LogEvent } from "./event_dispatcher";
import { buildLogEvent } from "./event_builder/log_event";
import { BackoffController, ExponentialBackoff, IntervalRepeater, Repeater } from "../utils/repeater/repeater";
import { LoggerFacade } from "../modules/logging";
import { BaseService, ServiceState, StartupLog } from "../service";
Expand All @@ -26,7 +26,7 @@ import { RunResult, runWithRetry } from "../utils/executor/backoff_retry_runner"
import { isSuccessStatusCode } from "../utils/http_request_handler/http_util";
import { EventEmitter } from "../utils/event_emitter/event_emitter";
import { IdGenerator } from "../utils/id_generator";
import { areEventContextsEqual } from "./events";
import { areEventContextsEqual } from "./event_builder/user_event";

export type EventWithId = {
id: string;
Expand All @@ -51,7 +51,7 @@ export type BatchEventProcessorConfig = {
};

type EventBatch = {
request: EventV1Request,
request: LogEvent,
ids: string[],
}

Expand All @@ -66,7 +66,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
private idGenerator: IdGenerator = new IdGenerator();
private runningTask: Map<string, RunResult<EventDispatcherResponse>> = new Map();
private dispatchingEventIds: Set<string> = new Set();
private eventEmitter: EventEmitter<{ dispatch: EventV1Request }> = new EventEmitter();
private eventEmitter: EventEmitter<{ dispatch: LogEvent }> = new EventEmitter();
private retryConfig?: RetryConfig;

constructor(config: BatchEventProcessorConfig) {
Expand All @@ -85,7 +85,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
this.failedEventRepeater?.setTask(() => this.retryFailedEvents());
}

onDispatch(handler: Consumer<EventV1Request>): Fn {
onDispatch(handler: Consumer<LogEvent>): Fn {
return this.eventEmitter.on('dispatch', handler);
}

Expand Down Expand Up @@ -119,7 +119,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
if (currentBatch.length === this.batchSize ||
(currentBatch.length > 0 && !areEventContextsEqual(currentBatch[0].event, event.event))) {
batches.push({
request: formatEvents(currentBatch.map((e) => e.event)),
request: buildLogEvent(currentBatch.map((e) => e.event)),
ids: currentBatch.map((e) => e.id),
});
currentBatch = [];
Expand All @@ -129,7 +129,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {

if (currentBatch.length > 0) {
batches.push({
request: formatEvents(currentBatch.map((e) => e.event)),
request: buildLogEvent(currentBatch.map((e) => e.event)),
ids: currentBatch.map((e) => e.id),
});
}
Expand All @@ -153,10 +153,10 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
});

this.eventQueue = [];
return { request: formatEvents(events), ids };
return { request: buildLogEvent(events), ids };
}

private async executeDispatch(request: EventV1Request, closing = false): Promise<EventDispatcherResponse> {
private async executeDispatch(request: LogEvent, closing = false): Promise<EventDispatcherResponse> {
const dispatcher = closing && this.closingEventDispatcher ? this.closingEventDispatcher : this.eventDispatcher;
return dispatcher.dispatchEvent(request).then((res) => {
if (res.statusCode && !isSuccessStatusCode(res.statusCode)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/event_processor/default_dispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
import { expect, vi, describe, it } from 'vitest';
import { DefaultEventDispatcher } from './default_dispatcher';
import { EventV1 } from './event_builder/build_event_v1';
import { EventBatch } from './event_builder/build_event_v1';

Check failure on line 18 in lib/event_processor/default_dispatcher.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

Check failure on line 18 in lib/event_processor/default_dispatcher.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

Check failure on line 18 in lib/event_processor/default_dispatcher.spec.ts

View workflow job for this annotation

GitHub Actions / unit_tests (22)

Cannot find module './event_builder/build_event_v1' or its corresponding type declarations.

const getEvent = (): EventV1 => {
const getEvent = (): EventBatch => {
return {
account_id: 'string',
project_id: 'string',
Expand Down
4 changes: 2 additions & 2 deletions lib/event_processor/default_dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { RequestHandler } from '../utils/http_request_handler/http';
import { EventDispatcher, EventDispatcherResponse, EventV1Request } from './event_dispatcher';
import { EventDispatcher, EventDispatcherResponse, LogEvent } from './event_dispatcher';

export class DefaultEventDispatcher implements EventDispatcher {
private requestHandler: RequestHandler;
Expand All @@ -24,7 +24,7 @@ export class DefaultEventDispatcher implements EventDispatcher {
}

async dispatchEvent(
eventObj: EventV1Request
eventObj: LogEvent
): Promise<EventDispatcherResponse> {
// Non-POST requests not supported
if (eventObj.httpVerb !== 'POST') {
Expand Down
Loading
Loading