@aws-amplify/analytics 7.0.1-console-preview.11cf989.0 → 7.0.1-console-preview.4ae78ed.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/errors/assertValidationError.d.ts +1 -1
- package/lib/errors/assertValidationError.js +7 -3
- package/lib/errors/validation.d.ts +3 -1
- package/lib/errors/validation.js +8 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -1
- package/lib/providers/kinesis/apis/flushEvents.d.ts +8 -0
- package/lib/providers/kinesis/apis/flushEvents.js +37 -0
- package/lib/providers/kinesis/apis/index.d.ts +1 -0
- package/lib/providers/kinesis/apis/index.js +3 -1
- package/lib/providers/kinesis/apis/record.d.ts +1 -1
- package/lib/providers/kinesis/apis/record.js +40 -2
- package/lib/providers/kinesis/index.d.ts +1 -1
- package/lib/providers/kinesis/index.js +2 -1
- package/lib/providers/kinesis/types/buffer.d.ts +15 -0
- package/lib/providers/kinesis/types/buffer.js +4 -0
- package/lib/providers/kinesis/types/index.d.ts +1 -0
- package/lib/providers/kinesis/types/inputs.d.ts +6 -1
- package/lib/providers/kinesis/utils/constants.d.ts +6 -0
- package/lib/providers/kinesis/utils/constants.js +11 -0
- package/lib/providers/kinesis/utils/getEventBuffer.d.ts +3 -0
- package/lib/providers/kinesis/utils/getEventBuffer.js +103 -0
- package/lib/providers/kinesis/utils/resolveConfig.d.ts +7 -0
- package/lib/providers/kinesis/utils/resolveConfig.js +24 -0
- package/lib/providers/kinesis-firehose/apis/flushEvents.d.ts +8 -0
- package/lib/providers/kinesis-firehose/apis/flushEvents.js +36 -0
- package/lib/providers/kinesis-firehose/apis/index.d.ts +1 -0
- package/lib/providers/kinesis-firehose/apis/index.js +3 -1
- package/lib/providers/kinesis-firehose/apis/record.d.ts +1 -1
- package/lib/providers/kinesis-firehose/apis/record.js +37 -2
- package/lib/providers/kinesis-firehose/index.d.ts +1 -1
- package/lib/providers/kinesis-firehose/index.js +2 -1
- package/lib/providers/kinesis-firehose/types/buffer.d.ts +15 -0
- package/lib/providers/kinesis-firehose/types/buffer.js +4 -0
- package/lib/providers/kinesis-firehose/types/index.d.ts +1 -0
- package/lib/providers/kinesis-firehose/types/inputs.d.ts +5 -1
- package/lib/providers/kinesis-firehose/utils/constants.d.ts +6 -0
- package/lib/providers/kinesis-firehose/utils/constants.js +11 -0
- package/lib/providers/kinesis-firehose/utils/getEventBuffer.d.ts +3 -0
- package/lib/providers/kinesis-firehose/utils/getEventBuffer.js +100 -0
- package/lib/providers/kinesis-firehose/utils/index.d.ts +2 -0
- package/lib/providers/kinesis-firehose/utils/index.js +9 -0
- package/lib/providers/kinesis-firehose/utils/resolveConfig.d.ts +7 -0
- package/lib/providers/kinesis-firehose/utils/resolveConfig.js +24 -0
- package/lib/providers/personalize/apis/flushEvents.d.ts +8 -0
- package/lib/providers/personalize/apis/flushEvents.js +35 -0
- package/lib/providers/personalize/apis/index.d.ts +1 -0
- package/lib/providers/personalize/apis/index.js +3 -1
- package/lib/providers/personalize/apis/record.d.ts +1 -1
- package/lib/providers/personalize/apis/record.js +65 -2
- package/lib/providers/personalize/index.d.ts +1 -1
- package/lib/providers/personalize/index.js +2 -1
- package/lib/providers/personalize/types/buffer.d.ts +16 -0
- package/lib/providers/personalize/types/buffer.js +4 -0
- package/lib/providers/personalize/types/index.d.ts +2 -1
- package/lib/providers/personalize/types/inputs.d.ts +7 -1
- package/lib/providers/personalize/utils/autoTrackMedia.d.ts +10 -0
- package/lib/providers/personalize/utils/autoTrackMedia.js +182 -0
- package/lib/providers/personalize/utils/cachedSession.d.ts +5 -0
- package/lib/providers/personalize/utils/cachedSession.js +52 -0
- package/lib/providers/personalize/utils/constants.d.ts +7 -0
- package/lib/providers/personalize/utils/constants.js +12 -0
- package/lib/providers/personalize/utils/getEventBuffer.d.ts +3 -0
- package/lib/providers/personalize/utils/getEventBuffer.js +100 -0
- package/lib/providers/personalize/utils/index.d.ts +5 -0
- package/lib/providers/personalize/utils/index.js +17 -0
- package/lib/providers/personalize/utils/resolveConfig.d.ts +7 -0
- package/lib/providers/personalize/utils/resolveConfig.js +25 -0
- package/lib/providers/pinpoint/apis/flushEvents.d.ts +8 -0
- package/lib/providers/pinpoint/apis/flushEvents.js +27 -0
- package/lib/providers/pinpoint/apis/identifyUser.d.ts +2 -2
- package/lib/providers/pinpoint/apis/identifyUser.js +3 -3
- package/lib/providers/pinpoint/apis/index.d.ts +1 -0
- package/lib/providers/pinpoint/apis/index.js +3 -1
- package/lib/providers/pinpoint/index.d.ts +1 -1
- package/lib/providers/pinpoint/index.js +2 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/kinesis.d.ts +8 -0
- package/lib/types/kinesis.js +4 -0
- package/lib/utils/eventBuffer/EventBuffer.d.ts +16 -0
- package/lib/utils/eventBuffer/EventBuffer.js +91 -0
- package/lib/utils/eventBuffer/index.d.ts +2 -0
- package/lib/utils/eventBuffer/index.js +7 -0
- package/lib/utils/eventBuffer/types.d.ts +8 -0
- package/lib/utils/eventBuffer/types.js +4 -0
- package/lib/utils/groupBy.d.ts +1 -0
- package/lib/utils/groupBy.js +15 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.js +7 -1
- package/lib/utils/resolveCredentials.d.ts +9 -0
- package/lib/utils/resolveCredentials.js +21 -0
- package/lib-esm/errors/assertValidationError.d.ts +1 -1
- package/lib-esm/errors/assertValidationError.js +7 -3
- package/lib-esm/errors/validation.d.ts +3 -1
- package/lib-esm/errors/validation.js +8 -0
- package/lib-esm/index.d.ts +1 -1
- package/lib-esm/index.js +1 -1
- package/lib-esm/providers/kinesis/apis/flushEvents.d.ts +8 -0
- package/lib-esm/providers/kinesis/apis/flushEvents.js +33 -0
- package/lib-esm/providers/kinesis/apis/index.d.ts +1 -0
- package/lib-esm/providers/kinesis/apis/index.js +1 -0
- package/lib-esm/providers/kinesis/apis/record.d.ts +1 -1
- package/lib-esm/providers/kinesis/apis/record.js +40 -2
- package/lib-esm/providers/kinesis/index.d.ts +1 -1
- package/lib-esm/providers/kinesis/index.js +1 -1
- package/lib-esm/providers/kinesis/types/buffer.d.ts +15 -0
- package/lib-esm/providers/kinesis/types/buffer.js +3 -0
- package/lib-esm/providers/kinesis/types/index.d.ts +1 -0
- package/lib-esm/providers/kinesis/types/inputs.d.ts +6 -1
- package/lib-esm/providers/kinesis/utils/constants.d.ts +6 -0
- package/lib-esm/providers/kinesis/utils/constants.js +8 -0
- package/lib-esm/providers/kinesis/utils/getEventBuffer.d.ts +3 -0
- package/lib-esm/providers/kinesis/utils/getEventBuffer.js +99 -0
- package/lib-esm/providers/kinesis/utils/resolveConfig.d.ts +7 -0
- package/lib-esm/providers/kinesis/utils/resolveConfig.js +20 -0
- package/lib-esm/providers/kinesis-firehose/apis/flushEvents.d.ts +8 -0
- package/lib-esm/providers/kinesis-firehose/apis/flushEvents.js +32 -0
- package/lib-esm/providers/kinesis-firehose/apis/index.d.ts +1 -0
- package/lib-esm/providers/kinesis-firehose/apis/index.js +1 -0
- package/lib-esm/providers/kinesis-firehose/apis/record.d.ts +1 -1
- package/lib-esm/providers/kinesis-firehose/apis/record.js +37 -2
- package/lib-esm/providers/kinesis-firehose/index.d.ts +1 -1
- package/lib-esm/providers/kinesis-firehose/index.js +1 -1
- package/lib-esm/providers/kinesis-firehose/types/buffer.d.ts +15 -0
- package/lib-esm/providers/kinesis-firehose/types/buffer.js +3 -0
- package/lib-esm/providers/kinesis-firehose/types/index.d.ts +1 -0
- package/lib-esm/providers/kinesis-firehose/types/inputs.d.ts +5 -1
- package/lib-esm/providers/kinesis-firehose/utils/constants.d.ts +6 -0
- package/lib-esm/providers/kinesis-firehose/utils/constants.js +8 -0
- package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.d.ts +3 -0
- package/lib-esm/providers/kinesis-firehose/utils/getEventBuffer.js +96 -0
- package/lib-esm/providers/kinesis-firehose/utils/index.d.ts +2 -0
- package/lib-esm/providers/kinesis-firehose/utils/index.js +4 -0
- package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.d.ts +7 -0
- package/lib-esm/providers/kinesis-firehose/utils/resolveConfig.js +20 -0
- package/lib-esm/providers/personalize/apis/flushEvents.d.ts +8 -0
- package/lib-esm/providers/personalize/apis/flushEvents.js +31 -0
- package/lib-esm/providers/personalize/apis/index.d.ts +1 -0
- package/lib-esm/providers/personalize/apis/index.js +1 -0
- package/lib-esm/providers/personalize/apis/record.d.ts +1 -1
- package/lib-esm/providers/personalize/apis/record.js +65 -2
- package/lib-esm/providers/personalize/index.d.ts +1 -1
- package/lib-esm/providers/personalize/index.js +1 -1
- package/lib-esm/providers/personalize/types/buffer.d.ts +16 -0
- package/lib-esm/providers/personalize/types/buffer.js +3 -0
- package/lib-esm/providers/personalize/types/index.d.ts +2 -1
- package/lib-esm/providers/personalize/types/inputs.d.ts +7 -1
- package/lib-esm/providers/personalize/utils/autoTrackMedia.d.ts +10 -0
- package/lib-esm/providers/personalize/utils/autoTrackMedia.js +178 -0
- package/lib-esm/providers/personalize/utils/cachedSession.d.ts +5 -0
- package/lib-esm/providers/personalize/utils/cachedSession.js +47 -0
- package/lib-esm/providers/personalize/utils/constants.d.ts +7 -0
- package/lib-esm/providers/personalize/utils/constants.js +9 -0
- package/lib-esm/providers/personalize/utils/getEventBuffer.d.ts +3 -0
- package/lib-esm/providers/personalize/utils/getEventBuffer.js +96 -0
- package/lib-esm/providers/personalize/utils/index.d.ts +5 -0
- package/lib-esm/providers/personalize/utils/index.js +7 -0
- package/lib-esm/providers/personalize/utils/resolveConfig.d.ts +7 -0
- package/lib-esm/providers/personalize/utils/resolveConfig.js +21 -0
- package/lib-esm/providers/pinpoint/apis/flushEvents.d.ts +8 -0
- package/lib-esm/providers/pinpoint/apis/flushEvents.js +23 -0
- package/lib-esm/providers/pinpoint/apis/identifyUser.d.ts +2 -2
- package/lib-esm/providers/pinpoint/apis/identifyUser.js +3 -3
- package/lib-esm/providers/pinpoint/apis/index.d.ts +1 -0
- package/lib-esm/providers/pinpoint/apis/index.js +1 -0
- package/lib-esm/providers/pinpoint/index.d.ts +1 -1
- package/lib-esm/providers/pinpoint/index.js +1 -1
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/index.d.ts +1 -0
- package/lib-esm/types/kinesis.d.ts +8 -0
- package/lib-esm/types/kinesis.js +3 -0
- package/lib-esm/utils/eventBuffer/EventBuffer.d.ts +16 -0
- package/lib-esm/utils/eventBuffer/EventBuffer.js +88 -0
- package/lib-esm/utils/eventBuffer/index.d.ts +2 -0
- package/lib-esm/utils/eventBuffer/index.js +3 -0
- package/lib-esm/utils/eventBuffer/types.d.ts +8 -0
- package/lib-esm/utils/eventBuffer/types.js +3 -0
- package/lib-esm/utils/groupBy.d.ts +1 -0
- package/lib-esm/utils/groupBy.js +11 -0
- package/lib-esm/utils/index.d.ts +3 -0
- package/lib-esm/utils/index.js +3 -0
- package/lib-esm/utils/resolveCredentials.d.ts +9 -0
- package/lib-esm/utils/resolveCredentials.js +17 -0
- package/package.json +5 -4
- package/src/errors/assertValidationError.ts +9 -3
- package/src/errors/validation.ts +8 -0
- package/src/index.ts +1 -0
- package/src/providers/kinesis/apis/flushEvents.ts +42 -0
- package/src/providers/kinesis/apis/index.ts +1 -0
- package/src/providers/kinesis/apis/record.ts +54 -2
- package/src/providers/kinesis/index.ts +1 -1
- package/src/providers/kinesis/types/buffer.ts +20 -0
- package/src/providers/kinesis/types/index.ts +1 -0
- package/src/providers/kinesis/types/inputs.ts +7 -1
- package/src/providers/kinesis/utils/constants.ts +9 -0
- package/src/providers/kinesis/utils/getEventBuffer.ts +110 -0
- package/src/providers/kinesis/utils/resolveConfig.ts +36 -0
- package/src/providers/kinesis-firehose/apis/flushEvents.ts +41 -0
- package/src/providers/kinesis-firehose/apis/index.ts +1 -0
- package/src/providers/kinesis-firehose/apis/record.ts +47 -2
- package/src/providers/kinesis-firehose/index.ts +1 -1
- package/src/providers/kinesis-firehose/types/buffer.ts +20 -0
- package/src/providers/kinesis-firehose/types/index.ts +5 -0
- package/src/providers/kinesis-firehose/types/inputs.ts +6 -1
- package/src/providers/kinesis-firehose/utils/constants.ts +9 -0
- package/src/providers/kinesis-firehose/utils/getEventBuffer.ts +118 -0
- package/src/providers/kinesis-firehose/utils/index.ts +5 -0
- package/src/providers/kinesis-firehose/utils/resolveConfig.ts +36 -0
- package/src/providers/personalize/apis/flushEvents.ts +39 -0
- package/src/providers/personalize/apis/index.ts +1 -0
- package/src/providers/personalize/apis/record.ts +98 -2
- package/src/providers/personalize/index.ts +1 -1
- package/src/providers/personalize/types/buffer.ts +21 -0
- package/src/providers/personalize/types/index.ts +2 -1
- package/src/providers/personalize/types/inputs.ts +8 -1
- package/src/providers/personalize/utils/autoTrackMedia.ts +231 -0
- package/src/providers/personalize/utils/cachedSession.ts +67 -0
- package/src/providers/personalize/utils/constants.ts +12 -0
- package/src/providers/personalize/utils/getEventBuffer.ts +111 -0
- package/src/providers/personalize/utils/index.ts +11 -0
- package/src/providers/personalize/utils/resolveConfig.ts +41 -0
- package/src/providers/pinpoint/apis/flushEvents.ts +34 -0
- package/src/providers/pinpoint/apis/identifyUser.ts +3 -3
- package/src/providers/pinpoint/apis/index.ts +1 -0
- package/src/providers/pinpoint/index.ts +1 -1
- package/src/types/index.ts +2 -0
- package/src/types/kinesis.ts +13 -0
- package/src/utils/eventBuffer/EventBuffer.ts +84 -0
- package/src/utils/eventBuffer/index.ts +5 -0
- package/src/utils/eventBuffer/types.ts +12 -0
- package/src/utils/groupBy.ts +12 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/resolveCredentials.ts +14 -0
package/lib-esm/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { AutoTrackAttributes, AutoTrackEventOpts, AutoTrackPageViewOpts, AutoTrackSessionOpts, EventAttributes, EventMetrics, EventTrackOpts, PageViewTrackOpts, SessionTrackOpts, } from './analytics';
|
|
2
2
|
export { AnalyticsServiceOptions } from './options';
|
|
3
3
|
export { AnalyticsIdentifyUserInput } from './inputs';
|
|
4
|
+
export { KinesisStream, KinesisShard, KinesisEventData } from './kinesis';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EventBufferConfig, IAnalyticsClient } from './';
|
|
2
|
+
export declare class EventBuffer<T> {
|
|
3
|
+
private list;
|
|
4
|
+
private readonly config;
|
|
5
|
+
private getAnalyticsClient;
|
|
6
|
+
private timer?;
|
|
7
|
+
constructor(config: EventBufferConfig, getAnalyticsClient: () => IAnalyticsClient<T>);
|
|
8
|
+
append(...events: T[]): void;
|
|
9
|
+
flushAll(): Promise<void>;
|
|
10
|
+
release(): void;
|
|
11
|
+
get length(): number;
|
|
12
|
+
private head;
|
|
13
|
+
private insertAtBeginning;
|
|
14
|
+
private startEventLoop;
|
|
15
|
+
private submitEvents;
|
|
16
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { __read, __spreadArray, __values } from "tslib";
|
|
4
|
+
import { ConsoleLogger } from '@aws-amplify/core/internals/utils';
|
|
5
|
+
var logger = new ConsoleLogger('EventBuffer');
|
|
6
|
+
var EventBuffer = /** @class */ (function () {
|
|
7
|
+
function EventBuffer(config, getAnalyticsClient) {
|
|
8
|
+
this.list = [];
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.getAnalyticsClient = getAnalyticsClient;
|
|
11
|
+
this.startEventLoop();
|
|
12
|
+
}
|
|
13
|
+
EventBuffer.prototype.append = function () {
|
|
14
|
+
var e_1, _a;
|
|
15
|
+
var events = [];
|
|
16
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
17
|
+
events[_i] = arguments[_i];
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
for (var events_1 = __values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
|
|
21
|
+
var event_1 = events_1_1.value;
|
|
22
|
+
if (this.list.length + 1 > this.config.bufferSize) {
|
|
23
|
+
logger.debug("Exceed ".concat(typeof event_1, " event buffer limits, event dropped"));
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
this.list.push(event_1);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
30
|
+
finally {
|
|
31
|
+
try {
|
|
32
|
+
if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
|
|
33
|
+
}
|
|
34
|
+
finally { if (e_1) throw e_1.error; }
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
EventBuffer.prototype.flushAll = function () {
|
|
38
|
+
return this.submitEvents(this.list.length);
|
|
39
|
+
};
|
|
40
|
+
EventBuffer.prototype.release = function () {
|
|
41
|
+
this.list = [];
|
|
42
|
+
if (this.timer) {
|
|
43
|
+
clearInterval(this.timer);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
Object.defineProperty(EventBuffer.prototype, "length", {
|
|
47
|
+
get: function () {
|
|
48
|
+
return this.list.length;
|
|
49
|
+
},
|
|
50
|
+
enumerable: false,
|
|
51
|
+
configurable: true
|
|
52
|
+
});
|
|
53
|
+
EventBuffer.prototype.head = function (count) {
|
|
54
|
+
return this.list.splice(0, count);
|
|
55
|
+
};
|
|
56
|
+
EventBuffer.prototype.insertAtBeginning = function () {
|
|
57
|
+
var _a;
|
|
58
|
+
var data = [];
|
|
59
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
60
|
+
data[_i] = arguments[_i];
|
|
61
|
+
}
|
|
62
|
+
(_a = this.list).unshift.apply(_a, __spreadArray([], __read(data), false));
|
|
63
|
+
};
|
|
64
|
+
EventBuffer.prototype.startEventLoop = function () {
|
|
65
|
+
var _this = this;
|
|
66
|
+
if (this.timer) {
|
|
67
|
+
clearInterval(this.timer);
|
|
68
|
+
}
|
|
69
|
+
var _a = this.config, flushSize = _a.flushSize, flushInterval = _a.flushInterval;
|
|
70
|
+
setInterval(function () {
|
|
71
|
+
_this.submitEvents(flushSize);
|
|
72
|
+
}, flushInterval);
|
|
73
|
+
};
|
|
74
|
+
EventBuffer.prototype.submitEvents = function (count) {
|
|
75
|
+
var _this = this;
|
|
76
|
+
var events = this.head(count);
|
|
77
|
+
if (events.length === 0) {
|
|
78
|
+
return Promise.resolve();
|
|
79
|
+
}
|
|
80
|
+
return this.getAnalyticsClient()(events).then(function (result) {
|
|
81
|
+
if (result.length > 0) {
|
|
82
|
+
_this.insertAtBeginning.apply(_this, __spreadArray([], __read(result), false));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
return EventBuffer;
|
|
87
|
+
}());
|
|
88
|
+
export { EventBuffer };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const groupBy: <T>(getGroupId: (x: T) => string, list: T[]) => Record<string, T[]>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { __assign, __read, __spreadArray } from "tslib";
|
|
4
|
+
export var groupBy = function (getGroupId, list) {
|
|
5
|
+
return list.reduce(function (result, current) {
|
|
6
|
+
var _a;
|
|
7
|
+
var _b;
|
|
8
|
+
var groupId = getGroupId(current);
|
|
9
|
+
return __assign(__assign({}, result), (_a = {}, _a[groupId] = __spreadArray(__spreadArray([], __read(((_b = result[groupId]) !== null && _b !== void 0 ? _b : [])), false), [current], false), _a));
|
|
10
|
+
}, {});
|
|
11
|
+
};
|
package/lib-esm/utils/index.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
+
export { resolveCredentials } from './resolveCredentials';
|
|
2
|
+
export { groupBy } from './groupBy';
|
|
3
|
+
export { EventBuffer, IAnalyticsClient, EventBufferConfig, } from './eventBuffer';
|
|
1
4
|
export { enableAnalytics, disableAnalytics, isAnalyticsEnabled, } from './statusHelpers';
|
|
2
5
|
export { getAnalyticsUserAgent, getAnalyticsUserAgentString, } from './userAgent';
|
package/lib-esm/utils/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export { resolveCredentials } from './resolveCredentials';
|
|
4
|
+
export { groupBy } from './groupBy';
|
|
5
|
+
export { EventBuffer, } from './eventBuffer';
|
|
3
6
|
export { enableAnalytics, disableAnalytics, isAnalyticsEnabled, } from './statusHelpers';
|
|
4
7
|
export { getAnalyticsUserAgent, getAnalyticsUserAgentString, } from './userAgent';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { __awaiter, __generator } from "tslib";
|
|
4
|
+
import { AnalyticsValidationErrorCode, assertValidationError } from '../errors';
|
|
5
|
+
import { fetchAuthSession } from '@aws-amplify/core';
|
|
6
|
+
export var resolveCredentials = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
7
|
+
var _a, credentials, identityId;
|
|
8
|
+
return __generator(this, function (_b) {
|
|
9
|
+
switch (_b.label) {
|
|
10
|
+
case 0: return [4 /*yield*/, fetchAuthSession()];
|
|
11
|
+
case 1:
|
|
12
|
+
_a = _b.sent(), credentials = _a.credentials, identityId = _a.identityId;
|
|
13
|
+
assertValidationError(!!credentials, AnalyticsValidationErrorCode.NoCredentials);
|
|
14
|
+
return [2 /*return*/, { credentials: credentials, identityId: identityId }];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}); };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/analytics",
|
|
3
|
-
"version": "7.0.1-console-preview.
|
|
3
|
+
"version": "7.0.1-console-preview.4ae78ed.0+4ae78ed",
|
|
4
4
|
"description": "Analytics category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -96,14 +96,15 @@
|
|
|
96
96
|
"uuid": "^9.0.0"
|
|
97
97
|
},
|
|
98
98
|
"peerDependencies": {
|
|
99
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
99
|
+
"@aws-amplify/core": "6.0.1-console-preview.4ae78ed.0+4ae78ed"
|
|
100
100
|
},
|
|
101
101
|
"devDependencies": {
|
|
102
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
102
|
+
"@aws-amplify/core": "6.0.1-console-preview.4ae78ed.0+4ae78ed",
|
|
103
103
|
"@aws-sdk/client-firehose": "3.398.0",
|
|
104
104
|
"@aws-sdk/client-kinesis": "3.398.0",
|
|
105
105
|
"@aws-sdk/client-personalize-events": "3.398.0",
|
|
106
106
|
"@aws-sdk/types": "3.398.0",
|
|
107
|
+
"@smithy/util-utf8": "2.0.0",
|
|
107
108
|
"@types/uuid": "^9.0.0",
|
|
108
109
|
"typescript": "5.0.2"
|
|
109
110
|
},
|
|
@@ -151,5 +152,5 @@
|
|
|
151
152
|
"<rootDir>/src/setupTests.ts"
|
|
152
153
|
]
|
|
153
154
|
},
|
|
154
|
-
"gitHead": "
|
|
155
|
+
"gitHead": "4ae78ed23fc03ae718488c98fc48782d2bd6f5f3"
|
|
155
156
|
}
|
|
@@ -9,11 +9,17 @@ import { AnalyticsValidationErrorCode, validationErrorMap } from './validation';
|
|
|
9
9
|
*/
|
|
10
10
|
export function assertValidationError(
|
|
11
11
|
assertion: boolean,
|
|
12
|
-
name: AnalyticsValidationErrorCode
|
|
12
|
+
name: AnalyticsValidationErrorCode,
|
|
13
|
+
message?: string
|
|
13
14
|
): asserts assertion {
|
|
14
|
-
const { message, recoverySuggestion } =
|
|
15
|
+
const { message: defaultMessage, recoverySuggestion } =
|
|
16
|
+
validationErrorMap[name];
|
|
15
17
|
|
|
16
18
|
if (!assertion) {
|
|
17
|
-
throw new AnalyticsError({
|
|
19
|
+
throw new AnalyticsError({
|
|
20
|
+
name,
|
|
21
|
+
message: message ?? defaultMessage,
|
|
22
|
+
recoverySuggestion,
|
|
23
|
+
});
|
|
18
24
|
}
|
|
19
25
|
}
|
package/src/errors/validation.ts
CHANGED
|
@@ -8,6 +8,8 @@ export enum AnalyticsValidationErrorCode {
|
|
|
8
8
|
NoCredentials = 'NoCredentials',
|
|
9
9
|
NoEventName = 'NoEventName',
|
|
10
10
|
NoRegion = 'NoRegion',
|
|
11
|
+
NoTrackingId = 'NoTrackingId',
|
|
12
|
+
InvalidFlushSize = 'InvalidFlushSize',
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export const validationErrorMap: AmplifyErrorMap<AnalyticsValidationErrorCode> =
|
|
@@ -24,4 +26,10 @@ export const validationErrorMap: AmplifyErrorMap<AnalyticsValidationErrorCode> =
|
|
|
24
26
|
[AnalyticsValidationErrorCode.NoRegion]: {
|
|
25
27
|
message: 'Missing region.',
|
|
26
28
|
},
|
|
29
|
+
[AnalyticsValidationErrorCode.InvalidFlushSize]: {
|
|
30
|
+
message: 'Invalid FlushSize, it should be smaller than BufferSize',
|
|
31
|
+
},
|
|
32
|
+
[AnalyticsValidationErrorCode.NoTrackingId]: {
|
|
33
|
+
message: 'A trackingId is required to use Amazon Personalize',
|
|
34
|
+
},
|
|
27
35
|
};
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { resolveConfig } from '../utils/resolveConfig';
|
|
5
|
+
import {
|
|
6
|
+
getAnalyticsUserAgentString,
|
|
7
|
+
resolveCredentials,
|
|
8
|
+
} from '../../../utils';
|
|
9
|
+
import { getEventBuffer } from '../utils/getEventBuffer';
|
|
10
|
+
import {
|
|
11
|
+
AnalyticsAction,
|
|
12
|
+
ConsoleLogger,
|
|
13
|
+
} from '@aws-amplify/core/internals/utils';
|
|
14
|
+
|
|
15
|
+
const logger = new ConsoleLogger('Kinesis');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Flushes all buffered Kinesis events to the service.
|
|
19
|
+
*
|
|
20
|
+
* @note
|
|
21
|
+
* This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
|
|
22
|
+
* this API may not be included in the flush.
|
|
23
|
+
*/
|
|
24
|
+
export const flushEvents = () => {
|
|
25
|
+
const { region, flushSize, flushInterval, bufferSize, resendLimit } =
|
|
26
|
+
resolveConfig();
|
|
27
|
+
resolveCredentials()
|
|
28
|
+
.then(({ credentials, identityId }) =>
|
|
29
|
+
getEventBuffer({
|
|
30
|
+
region,
|
|
31
|
+
flushSize,
|
|
32
|
+
flushInterval,
|
|
33
|
+
bufferSize,
|
|
34
|
+
credentials,
|
|
35
|
+
identityId,
|
|
36
|
+
resendLimit,
|
|
37
|
+
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
|
|
38
|
+
})
|
|
39
|
+
)
|
|
40
|
+
.then(eventBuffer => eventBuffer.flushAll())
|
|
41
|
+
.catch(e => logger.warn('Failed to flush events.', e));
|
|
42
|
+
};
|
|
@@ -2,7 +2,59 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { RecordInput } from '../types';
|
|
5
|
+
import { getEventBuffer } from '../utils/getEventBuffer';
|
|
6
|
+
import { resolveConfig } from '../utils/resolveConfig';
|
|
7
|
+
import {
|
|
8
|
+
getAnalyticsUserAgentString,
|
|
9
|
+
isAnalyticsEnabled,
|
|
10
|
+
resolveCredentials,
|
|
11
|
+
} from '../../../utils';
|
|
12
|
+
import { fromUtf8 } from '@smithy/util-utf8';
|
|
13
|
+
import {
|
|
14
|
+
AnalyticsAction,
|
|
15
|
+
ConsoleLogger,
|
|
16
|
+
} from '@aws-amplify/core/internals/utils';
|
|
5
17
|
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
const logger = new ConsoleLogger('Kinesis');
|
|
19
|
+
|
|
20
|
+
export const record = ({
|
|
21
|
+
streamName,
|
|
22
|
+
partitionKey,
|
|
23
|
+
data,
|
|
24
|
+
}: RecordInput): void => {
|
|
25
|
+
if (!isAnalyticsEnabled()) {
|
|
26
|
+
logger.debug('Analytics is disabled, event will not be recorded.');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const timestamp = Date.now();
|
|
31
|
+
const { region, bufferSize, flushSize, flushInterval, resendLimit } =
|
|
32
|
+
resolveConfig();
|
|
33
|
+
|
|
34
|
+
resolveCredentials()
|
|
35
|
+
.then(({ credentials, identityId }) => {
|
|
36
|
+
const buffer = getEventBuffer({
|
|
37
|
+
region,
|
|
38
|
+
bufferSize,
|
|
39
|
+
flushSize,
|
|
40
|
+
flushInterval,
|
|
41
|
+
credentials,
|
|
42
|
+
identityId,
|
|
43
|
+
resendLimit,
|
|
44
|
+
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
buffer.append({
|
|
48
|
+
region,
|
|
49
|
+
streamName,
|
|
50
|
+
partitionKey,
|
|
51
|
+
event: ArrayBuffer.isView(data) ? data : fromUtf8(JSON.stringify(data)),
|
|
52
|
+
timestamp,
|
|
53
|
+
retryCount: 0,
|
|
54
|
+
});
|
|
55
|
+
})
|
|
56
|
+
.catch(e => {
|
|
57
|
+
// An error occured while fetching credentials or persisting the event to the buffer
|
|
58
|
+
logger.warn('Failed to record event.', e);
|
|
59
|
+
});
|
|
8
60
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { EventBufferConfig } from '../../../utils';
|
|
5
|
+
import { Credentials } from '@aws-sdk/types';
|
|
6
|
+
import { KinesisShard } from '../../../types';
|
|
7
|
+
|
|
8
|
+
export type KinesisBufferEvent = KinesisShard & {
|
|
9
|
+
event: Uint8Array;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
retryCount: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type KinesisEventBufferConfig = EventBufferConfig & {
|
|
15
|
+
region: string;
|
|
16
|
+
credentials: Credentials;
|
|
17
|
+
identityId?: string;
|
|
18
|
+
resendLimit?: number;
|
|
19
|
+
userAgentValue?: string;
|
|
20
|
+
};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
import { KinesisEventData } from '../../../types';
|
|
5
|
+
|
|
6
|
+
export type RecordInput = {
|
|
7
|
+
streamName: string;
|
|
8
|
+
partitionKey: string;
|
|
9
|
+
data: KinesisEventData;
|
|
10
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { KinesisBufferEvent, KinesisEventBufferConfig } from '../types';
|
|
5
|
+
import { EventBuffer, groupBy, IAnalyticsClient } from '../../../utils';
|
|
6
|
+
import { KinesisClient, PutRecordsCommand } from '@aws-sdk/client-kinesis';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* These Records hold cached event buffers and AWS clients.
|
|
10
|
+
* The hash key is determined by the region and session,
|
|
11
|
+
* consisting of a combined value comprising [region, sessionToken, identityId].
|
|
12
|
+
*
|
|
13
|
+
* Only one active session should exist at any given moment.
|
|
14
|
+
* When a new session is initiated, the previous ones should be released.
|
|
15
|
+
* */
|
|
16
|
+
const eventBufferMap: Record<string, EventBuffer<KinesisBufferEvent>> = {};
|
|
17
|
+
const cachedClients: Record<string, KinesisClient> = {};
|
|
18
|
+
|
|
19
|
+
const createKinesisPutRecordsCommand = (
|
|
20
|
+
streamName: string,
|
|
21
|
+
events: KinesisBufferEvent[]
|
|
22
|
+
): PutRecordsCommand =>
|
|
23
|
+
new PutRecordsCommand({
|
|
24
|
+
StreamName: streamName,
|
|
25
|
+
Records: events.map(event => ({
|
|
26
|
+
PartitionKey: event.partitionKey,
|
|
27
|
+
Data: event.event,
|
|
28
|
+
})),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const submitEvents = async (
|
|
32
|
+
events: KinesisBufferEvent[],
|
|
33
|
+
client: KinesisClient,
|
|
34
|
+
resendLimit?: number
|
|
35
|
+
): Promise<KinesisBufferEvent[]> => {
|
|
36
|
+
const groupedByStreamName = Object.entries(
|
|
37
|
+
groupBy(event => event.streamName, events)
|
|
38
|
+
);
|
|
39
|
+
const requests = groupedByStreamName
|
|
40
|
+
.map(([streamName, events]) =>
|
|
41
|
+
createKinesisPutRecordsCommand(streamName, events)
|
|
42
|
+
)
|
|
43
|
+
.map(command => client.send(command));
|
|
44
|
+
|
|
45
|
+
const responses = await Promise.allSettled(requests);
|
|
46
|
+
const failedEvents = responses
|
|
47
|
+
.map((response, i) =>
|
|
48
|
+
response.status === 'rejected' ? groupedByStreamName[i][1] : []
|
|
49
|
+
)
|
|
50
|
+
.flat();
|
|
51
|
+
return resendLimit
|
|
52
|
+
? failedEvents
|
|
53
|
+
.filter(event => event.retryCount < resendLimit)
|
|
54
|
+
.map(event => ({ ...event, retryCount: event.retryCount + 1 }))
|
|
55
|
+
.sort((a, b) => a.timestamp - b.timestamp)
|
|
56
|
+
: [];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const getEventBuffer = ({
|
|
60
|
+
region,
|
|
61
|
+
flushInterval,
|
|
62
|
+
flushSize,
|
|
63
|
+
bufferSize,
|
|
64
|
+
credentials,
|
|
65
|
+
identityId,
|
|
66
|
+
resendLimit,
|
|
67
|
+
userAgentValue,
|
|
68
|
+
}: KinesisEventBufferConfig): EventBuffer<KinesisBufferEvent> => {
|
|
69
|
+
const { sessionToken } = credentials;
|
|
70
|
+
const sessionIdentityKey = [region, sessionToken, identityId]
|
|
71
|
+
.filter(x => !!x)
|
|
72
|
+
.join('-');
|
|
73
|
+
|
|
74
|
+
if (!eventBufferMap[sessionIdentityKey]) {
|
|
75
|
+
const getKinesisClient = (): IAnalyticsClient<KinesisBufferEvent> => {
|
|
76
|
+
if (!cachedClients[sessionIdentityKey]) {
|
|
77
|
+
cachedClients[sessionIdentityKey] = new KinesisClient({
|
|
78
|
+
credentials,
|
|
79
|
+
region,
|
|
80
|
+
customUserAgent: userAgentValue,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return events =>
|
|
85
|
+
submitEvents(events, cachedClients[sessionIdentityKey], resendLimit);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// create new session
|
|
89
|
+
eventBufferMap[sessionIdentityKey] = new EventBuffer<KinesisBufferEvent>(
|
|
90
|
+
{
|
|
91
|
+
flushInterval,
|
|
92
|
+
flushSize,
|
|
93
|
+
bufferSize,
|
|
94
|
+
},
|
|
95
|
+
getKinesisClient
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
// release other sessions
|
|
99
|
+
const releaseSessionKeys = Object.keys(eventBufferMap).filter(
|
|
100
|
+
x => x !== sessionIdentityKey
|
|
101
|
+
);
|
|
102
|
+
for (const releaseSessionKey of releaseSessionKeys) {
|
|
103
|
+
eventBufferMap[releaseSessionKey].release();
|
|
104
|
+
delete eventBufferMap[releaseSessionKey];
|
|
105
|
+
delete cachedClients[releaseSessionKey];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return eventBufferMap[sessionIdentityKey];
|
|
110
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Amplify } from '@aws-amplify/core';
|
|
5
|
+
import {
|
|
6
|
+
AnalyticsValidationErrorCode,
|
|
7
|
+
assertValidationError,
|
|
8
|
+
} from '../../../errors';
|
|
9
|
+
import { DEFAULT_KINESIS_CONFIG } from './constants';
|
|
10
|
+
|
|
11
|
+
export const resolveConfig = () => {
|
|
12
|
+
const config = Amplify.getConfig().Analytics?.Kinesis;
|
|
13
|
+
const {
|
|
14
|
+
region,
|
|
15
|
+
bufferSize = DEFAULT_KINESIS_CONFIG.bufferSize,
|
|
16
|
+
flushSize = DEFAULT_KINESIS_CONFIG.flushSize,
|
|
17
|
+
flushInterval = DEFAULT_KINESIS_CONFIG.flushInterval,
|
|
18
|
+
resendLimit,
|
|
19
|
+
} = {
|
|
20
|
+
...DEFAULT_KINESIS_CONFIG,
|
|
21
|
+
...config,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
assertValidationError(!!region, AnalyticsValidationErrorCode.NoRegion);
|
|
25
|
+
assertValidationError(
|
|
26
|
+
flushSize < bufferSize,
|
|
27
|
+
AnalyticsValidationErrorCode.InvalidFlushSize
|
|
28
|
+
);
|
|
29
|
+
return {
|
|
30
|
+
region,
|
|
31
|
+
bufferSize,
|
|
32
|
+
flushSize,
|
|
33
|
+
flushInterval,
|
|
34
|
+
resendLimit,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { getEventBuffer, resolveConfig } from '../utils';
|
|
5
|
+
import {
|
|
6
|
+
getAnalyticsUserAgentString,
|
|
7
|
+
resolveCredentials,
|
|
8
|
+
} from '../../../utils';
|
|
9
|
+
import {
|
|
10
|
+
AnalyticsAction,
|
|
11
|
+
ConsoleLogger,
|
|
12
|
+
} from '@aws-amplify/core/internals/utils';
|
|
13
|
+
|
|
14
|
+
const logger = new ConsoleLogger('KinesisFirehose');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Flushes all buffered Kinesis events to the service.
|
|
18
|
+
*
|
|
19
|
+
* @note
|
|
20
|
+
* This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
|
|
21
|
+
* this API may not be included in the flush.
|
|
22
|
+
*/
|
|
23
|
+
export const flushEvents = () => {
|
|
24
|
+
const { region, flushSize, flushInterval, bufferSize, resendLimit } =
|
|
25
|
+
resolveConfig();
|
|
26
|
+
resolveCredentials()
|
|
27
|
+
.then(({ credentials, identityId }) =>
|
|
28
|
+
getEventBuffer({
|
|
29
|
+
region,
|
|
30
|
+
flushSize,
|
|
31
|
+
flushInterval,
|
|
32
|
+
bufferSize,
|
|
33
|
+
credentials,
|
|
34
|
+
identityId,
|
|
35
|
+
resendLimit,
|
|
36
|
+
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.Record),
|
|
37
|
+
})
|
|
38
|
+
)
|
|
39
|
+
.then(eventBuffer => eventBuffer.flushAll())
|
|
40
|
+
.catch(e => logger.warn('Failed to flush events.', e));
|
|
41
|
+
};
|