Skip to content

Commit 2cf2c24

Browse files
committed
Added sender and data conditions to event example
1 parent 0d8b2f0 commit 2cf2c24

2 files changed

Lines changed: 129 additions & 26 deletions

File tree

examples/event.js

Lines changed: 128 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,138 @@ global.WebSocket = require('ws');
33
const Client = require('../client');
44
// NOTE: Event support is only on API version 4.0.0+
55

6+
const { DBMessaging } = require('../generated/containerPb');
7+
8+
/**
9+
* Helper: Create an exact match condition for filtering.
10+
* Uses the EventQuery.Condition message.
11+
*/
12+
function createExactCondition(value) {
13+
return DBMessaging.Protobuf.EventQuery.Condition.create({
14+
value: value,
15+
type: DBMessaging.Protobuf.EventQuery.MatchType.Exact // 0
16+
});
17+
}
18+
19+
/**
20+
* Helper: Create a ConditionList from an array of conditions.
21+
*/
22+
function createConditionList(conditions) {
23+
return DBMessaging.Protobuf.EventQuery.ConditionList.create({
24+
conditions: conditions
25+
});
26+
}
27+
28+
/**
29+
* Helper: Create sender conditions.
30+
* @param {Array<string>} senders - Array of exact-match sender names.
31+
* @param {Array<string>} senderPatterns - Array of wildcard patterns.
32+
* @returns {DBMessaging.Protobuf.EventQuery.ConditionList}
33+
*/
34+
function createSenderConditions(senders = [], senderPatterns = []) {
35+
const conditions = [
36+
...senders.map(sender => createExactCondition(sender)),
37+
...senderPatterns.map(pattern =>
38+
DBMessaging.Protobuf.EventQuery.Condition.create({
39+
value: pattern,
40+
type: DBMessaging.Protobuf.EventQuery.MatchType.Wildcard // 1
41+
})
42+
)
43+
];
44+
return createConditionList(conditions);
45+
}
46+
47+
/**
48+
* Helper: Create data conditions.
49+
* @param {Object} dataConditions - Map of data field keys to values (or arrays of values)
50+
* @returns {Object} - Map of field name to ConditionList
51+
*/
52+
function createDataConditions(dataConditions = {}) {
53+
const result = {};
54+
for (const [key, value] of Object.entries(dataConditions)) {
55+
let conditions;
56+
if (Array.isArray(value)) {
57+
conditions = value.map(v => {
58+
if (typeof v === "string" && (v.includes("*") || v.includes("?"))) {
59+
return DBMessaging.Protobuf.EventQuery.Condition.create({
60+
value: v,
61+
type: DBMessaging.Protobuf.EventQuery.MatchType.Wildcard
62+
});
63+
} else {
64+
return createExactCondition(String(v));
65+
}
66+
});
67+
} else {
68+
if (typeof value === "string" && (value.includes("*") || value.includes("?"))) {
69+
conditions = [
70+
DBMessaging.Protobuf.EventQuery.Condition.create({
71+
value: value,
72+
type: DBMessaging.Protobuf.EventQuery.MatchType.Wildcard
73+
})
74+
];
75+
} else {
76+
conditions = [createExactCondition(String(value))];
77+
}
78+
}
79+
result[key] = createConditionList(conditions);
80+
}
81+
return result;
82+
}
83+
84+
/**
85+
* Create an EventQuery message.
86+
* @param {Object} options - Options for filtering.
87+
* @returns {DBMessaging.Protobuf.EventQuery}
88+
*/
89+
function createEventQuery(options = {}) {
90+
const queryObj = {
91+
timeRangeBegin: options.timeRangeBegin || 0,
92+
timeRangeEnd: options.timeRangeEnd || 2147483647,
93+
codeMask: options.codeMask !== undefined ? options.codeMask : 0xFFFFFFFF,
94+
limit: options.limit || 20,
95+
offset: options.offset || 0,
96+
flags: options.flags || 1
97+
};
98+
99+
if ((options.senders && options.senders.length) || (options.senderPatterns && options.senderPatterns.length)) {
100+
queryObj.senderConditions = createSenderConditions(options.senders || [], options.senderPatterns || []);
101+
}
102+
if (options.dataConditions && Object.keys(options.dataConditions).length > 0) {
103+
queryObj.dataConditions = createDataConditions(options.dataConditions);
104+
}
105+
return DBMessaging.Protobuf.EventQuery.create(queryObj);
106+
}
107+
108+
/**
109+
* Format an event for display.
110+
*/
111+
function formatEvent(evt) {
112+
return `
113+
Timestamp: ${evt.timestampSec}
114+
Code: ${evt.code} (${evt.codeDescription})
115+
Sender: ${evt.sender}
116+
Data: ${JSON.stringify(evt.data)}
117+
--------------------`;
118+
}
119+
6120
(async function main() {
7121
const client = new Client('ws://127.0.0.1:17000', false);
8122
try {
9-
// Wide time range and large limit
10-
const query = {
11-
timeRangeBegin: 0, // start from the epoch
12-
timeRangeEnd: 2147483647, // far in the future
13-
codeMask: 0xFFFFFFFF, // all codes
14-
limit: 50, // max number of events to retrieve
15-
offset: 0,
16-
flags: 1 // e.g. 'NewestFirst'
17-
};
18-
19-
const events = await client.requestEvents(query);
20-
21-
// Print events directly (no deduplication or normalization)
22-
if (events.length === 0) {
23-
console.log("No events found.");
24-
} else {
25-
console.log(`Showing ${events.length} events:\n`);
26-
for (const evt of events) {
27-
console.log(`Timestamp: ${evt.timestampSec}`);
28-
// Now we show the numeric code and the human-readable description:
29-
console.log(`Code: ${evt.code} (${evt.codeDescription})`);
30-
console.log(`Sender: ${evt.sender}`);
31-
console.log(`Data: ${JSON.stringify(evt.data)}`);
32-
console.log('--------------------');
123+
// Wait a moment for the connection to establish.
124+
await new Promise(resolve => setTimeout(resolve, 1000));
125+
126+
console.log("----- Example: Combined Server‑Side Filtering (Sender + Data) -----");
127+
// Build a query that combines both conditions.
128+
const combinedQuery = createEventQuery({
129+
senders: ["CDPLoggerDemoApp.InvalidLicense"],
130+
dataConditions: {
131+
"Text": "Invalid or missing feature license detected."
33132
}
34-
}
133+
});
134+
const combinedEvents = await client.requestEvents(combinedQuery);
135+
console.log(`Showing ${combinedEvents.length} events matching both conditions:`);
136+
combinedEvents.forEach(evt => console.log(formatEvent(evt)));
137+
35138
} catch (err) {
36139
console.error("Error retrieving events:", err);
37140
} finally {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)