generated from estuary/flow-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2_derive.flow.ts
81 lines (74 loc) · 3.32 KB
/
2_derive.flow.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { anchors, collections, interfaces, registers } from 'flow/modules';
// detail maps a segment event into a SegmentDetail.
function detail(event: collections.ExamplesSegmentEvents): anchors.SegmentDetail {
const rest = {
segment: event.segment,
last: event.timestamp,
};
if (event.remove) {
return { member: false, ...rest };
} else {
return { member: true, first: event.timestamp, value: event.value, ...rest };
}
}
// Implementation for derivation 2_derive.flow.yaml#/collections/examples~1segment~1memberships/derivation.
export class ExamplesSegmentMemberships implements interfaces.ExamplesSegmentMemberships {
fromSegmentationPublish(
source: collections.ExamplesSegmentEvents,
_register: registers.ExamplesSegmentMemberships,
_previous: registers.ExamplesSegmentMemberships,
): collections.ExamplesSegmentMemberships[] {
return [{ user: source.user, ...detail(source) }];
}
}
// Implementation for derivation 2_derive.flow.yaml#/collections/examples~1segment~1profiles/derivation.
export class ExamplesSegmentProfiles implements interfaces.ExamplesSegmentProfiles {
fromSegmentationPublish(
source: collections.ExamplesSegmentEvents,
_register: registers.ExamplesSegmentProfiles,
_previous: registers.ExamplesSegmentProfiles,
): collections.ExamplesSegmentProfiles[] {
// Each source is a segment set of O(1), which is combined with others.
return [{ user: source.user, segments: [detail(source)] }];
}
/*
// Uncomment me in tandem with trying out the "push" version of profiles in flow.yaml.
fromSegmentationUpdate(source: collections.ExamplesSegmentEvents): anchors.SegmentSet[] {
// Each source is a segment set of O(1), which is reduced into the register.
return [[detail(source)]];
}
fromSegmentationPublish(
source: collections.ExamplesSegmentEvents,
register: anchors.SegmentSet,
_previous: anchors.SegmentSet,
): collections.ExamplesSegmentProfiles[] {
// Join the user with their fully reduced segments from the register.
return [{ user: source.user, segments: register }];
}
*/
}
// Implementation for derivation 2_derive.flow.yaml#/collections/examples~1segment~1toggles/derivation.
export class ExamplesSegmentToggles implements interfaces.ExamplesSegmentToggles {
fromSegmentationUpdate(source: collections.ExamplesSegmentEvents): registers.ExamplesSegmentToggles[] {
if (source.remove) {
return [{ event: source }];
} else {
return [{ event: source, firstAdd: true }];
}
}
fromSegmentationPublish(
source: collections.ExamplesSegmentEvents,
_register: registers.ExamplesSegmentToggles,
previous: registers.ExamplesSegmentToggles,
): collections.ExamplesSegmentToggles[] {
const { event: last, firstAdd } = previous;
// Only publish a toggle if the user has been added to the segment at
// least once, and the |last| event add / remove status is different from
// the source event status. This is arbitrary but reduces volume to
// manageable levels.
if (firstAdd && last && last.remove != source.remove) {
return [{ previous: last, ...source }];
}
return [];
}
}