Skip to content

Commit

Permalink
Change how events are indexed (#17)
Browse files Browse the repository at this point in the history
* update how events are stored

* test

* switched to index by type, then by id
  • Loading branch information
jasonwaters committed May 2, 2023
1 parent b15f6b0 commit c496e9e
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/aep-rules-engine",
"version": "1.0.3",
"version": "2.0.1",
"description": "Adobe Experience Platform rules engine.",
"repository": {
"type": "git",
Expand Down
72 changes: 40 additions & 32 deletions src/historical.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ describe("test helper functions", () => {
];
const context = {
events: {
abc: {
event: {
type: "display",
id: "abc",
display: {
abc: {
event: {
type: "display",
id: "abc",
},
timestamp: 1609086720000,
count: 2,
},
timestamp: 1609086720000,
count: 2,
},
def: {
event: {
type: "display",
id: "def",
def: {
event: {
type: "display",
id: "def",
},
timestamp: 1609086720000,
count: 6,
},
timestamp: 1609086720000,
count: 6,
},
},
};
Expand All @@ -67,21 +69,23 @@ describe("test helper functions", () => {
];
const context = {
events: {
abc: {
event: {
type: "display",
id: "abc",
display: {
abc: {
event: {
type: "display",
id: "abc",
},
timestamp: 1609086720000,
count: 2,
},
timestamp: 1609086720000,
count: 2,
},
def: {
event: {
type: "display",
id: "def",
def: {
event: {
type: "display",
id: "def",
},
timestamp: 1609086720000,
count: 6,
},
timestamp: 1609086720000,
count: 6,
},
},
};
Expand All @@ -100,9 +104,11 @@ describe("test helper functions", () => {
];
const context = {
events: {
A: { count: 1, timestamp: 1 },
B: { count: 1, timestamp: 2 },
C: { count: 1, timestamp: 3 },
display: {
A: { count: 1, timestamp: 1 },
B: { count: 1, timestamp: 2 },
C: { count: 1, timestamp: 3 },
},
},
};
const from = 0;
Expand All @@ -120,9 +126,11 @@ describe("test helper functions", () => {
];
const context = {
events: {
A: { count: 1, timestamp: 1 },
B: { count: 1, timestamp: 2 },
C: { count: 1, timestamp: 3 },
display: {
A: { count: 1, timestamp: 1 },
B: { count: 1, timestamp: 2 },
C: { count: 1, timestamp: 3 },
},
},
};
const from = 0;
Expand Down
22 changes: 15 additions & 7 deletions src/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ export function queryAndCountAnyEvent(
to?: any
) {
return events.reduce((countTotal, event) => {
const { id } = event;
const contextEvent = context.events[id];
const eventsOfType = context.events[event.type];
if (!eventsOfType) {
return countTotal;
}

const contextEvent = eventsOfType[event.id];
if (!contextEvent) {
return countTotal;
}
Expand Down Expand Up @@ -90,8 +94,12 @@ export function queryAndCountOrderedEvent(
) {
let previousEventTimestamp = from;
const sameSequence = events.every((event) => {
const { id } = event;
const contextEvent = context.events[id];
const eventsOfType = context.events[event.type];
if (!eventsOfType) {
return false;
}

const contextEvent = eventsOfType[event.id];
if (
contextEvent === null ||
isUndefined(contextEvent) ||
Expand All @@ -102,9 +110,9 @@ export function queryAndCountOrderedEvent(

const ordered =
(isUndefined(previousEventTimestamp) ||
context.events[id].timestamp >= previousEventTimestamp) &&
(isUndefined(to) || context.events[id].timestamp <= to);
previousEventTimestamp = context.events[id].timestamp;
contextEvent.timestamp >= previousEventTimestamp) &&
(isUndefined(to) || contextEvent.timestamp <= to);
previousEventTimestamp = contextEvent.timestamp;
return ordered;
});

Expand Down
Loading

0 comments on commit c496e9e

Please sign in to comment.