-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathimpressionsSubmitter.ts
More file actions
63 lines (54 loc) · 2.14 KB
/
Copy pathimpressionsSubmitter.ts
File metadata and controls
63 lines (54 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { groupBy, forOwn } from '../../utils/lang';
import SplitIO from '../../../types/splitio';
import { submitterFactory } from './submitter';
import { ImpressionsPayload } from './types';
import { SUBMITTERS_PUSH_FULL_QUEUE } from '../../logger/constants';
import { ISdkFactoryContextSync } from '../../sdkFactory/types';
/**
* Converts `impressions` data from cache into request payload.
*/
export function fromImpressionsCollector(sendLabels: boolean, data: SplitIO.ImpressionDTO[]): ImpressionsPayload {
let groupedByFeature = groupBy(data, 'feature');
let dto: ImpressionsPayload = [];
forOwn(groupedByFeature, (value, name) => {
dto.push({
f: name, // Test Name
i: value.map(entry => { // Key Impressions
const keyImpression = {
k: entry.keyName, // Key
t: entry.treatment, // Treatment
m: entry.time, // Timestamp
c: entry.changeNumber, // ChangeNumber
r: sendLabels ? entry.label : undefined, // Rule
b: entry.bucketingKey, // Bucketing Key
pt: entry.pt, // Previous time
properties: entry.properties // Properties
};
return keyImpression;
})
});
});
return dto;
}
/**
* Submitter that periodically posts impressions data
*/
export function impressionsSubmitterFactory(params: ISdkFactoryContextSync) {
const {
settings: { log, scheduler: { impressionsRefreshRate }, core: { labelsEnabled } },
splitApi: { postTestImpressionsBulk },
storage: { impressions }
} = params;
// retry impressions only once.
const syncTask = submitterFactory(log, postTestImpressionsBulk, impressions, impressionsRefreshRate, fromImpressionsCollector.bind(undefined, labelsEnabled), 1);
// register impressions submitter to be executed when impressions cache is full
impressions.setOnFullQueueCb(() => {
if (syncTask.isRunning()) {
log.info(SUBMITTERS_PUSH_FULL_QUEUE, [impressions.name]);
syncTask.execute();
}
// If submitter is stopped (e.g., user consent declined or unknown, or app state offline), we don't send the data.
// Data will be sent when submitter is resumed.
});
return syncTask;
}