UNPKG

@aws-amplify/analytics

Version:

Analytics category of aws-amplify

87 lines (85 loc) 3.77 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.getEventBuffer = void 0; const utils_1 = require("@aws-amplify/core/internals/utils"); const client_personalize_events_1 = require("@aws-sdk/client-personalize-events"); const utils_2 = require("../../../utils"); /** * These Records hold cached event buffers and AWS clients. * The hash key is determined by the region and session, * consisting of a combined value comprising [region, sessionToken, identityId]. * * Only one active session should exist at any given moment. * When a new session is initiated, the previous ones should be released. * */ const eventBufferMap = {}; const cachedClients = {}; const DELIMITER = '#'; const createPutEventsCommand = (ids, events) => { const [trackingId, sessionId, userId] = ids.split(DELIMITER); return new client_personalize_events_1.PutEventsCommand({ trackingId, sessionId, userId, eventList: events.map(event => ({ eventId: event.event.eventId, eventType: event.event.eventType, properties: JSON.stringify(event.event.properties), sentAt: new Date(event.timestamp), })), }); }; const submitEvents = async (events, client) => { const groupedByIds = Object.entries((0, utils_2.groupBy)(event => [event.trackingId, event.sessionId, event.userId] .filter(id => !!id) .join(DELIMITER), events)); const requests = groupedByIds .map(([ids, groupedEvents]) => createPutEventsCommand(ids, groupedEvents)) .map(command => client.send(command)); await Promise.allSettled(requests); return Promise.resolve([]); }; const getEventBuffer = ({ region, flushSize, flushInterval, bufferSize, credentials, identityId, userAgentValue, }) => { const sessionIdentityKey = [region, identityId].filter(x => !!x).join('-'); const [cachedClient, cachedCredentials] = cachedClients[sessionIdentityKey] ?? []; let credentialsHaveChanged = false; // Check if credentials have changed for the cached client if (cachedClient) { credentialsHaveChanged = (0, utils_1.haveCredentialsChanged)(cachedCredentials, credentials); } if (!eventBufferMap[sessionIdentityKey] || credentialsHaveChanged) { const getClient = () => { if (!cachedClient || credentialsHaveChanged) { cachedClients[sessionIdentityKey] = [ new client_personalize_events_1.PersonalizeEventsClient({ region, credentials, customUserAgent: userAgentValue, }), credentials, ]; } const [personalizeClient] = cachedClients[sessionIdentityKey]; return events => submitEvents(events, personalizeClient); }; eventBufferMap[sessionIdentityKey] = new utils_2.EventBuffer({ bufferSize, flushSize, flushInterval, }, getClient); const releaseSessionKeys = Object.keys(eventBufferMap).filter(key => key !== sessionIdentityKey); for (const releaseSessionKey of releaseSessionKeys) { eventBufferMap[releaseSessionKey].flushAll().finally(() => { eventBufferMap[releaseSessionKey].release(); delete eventBufferMap[releaseSessionKey]; delete cachedClients[releaseSessionKey]; }); } } return eventBufferMap[sessionIdentityKey]; }; exports.getEventBuffer = getEventBuffer; //# sourceMappingURL=getEventBuffer.js.map