Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ export async function createEventbase(config: EventbaseConfig) {
const result = await db.query(queryObject as any, options as any);
await publishStats({
operation: 'QUERY',
query: queryObject,
queryResultCount: result.length,
timestamp: start,
duration: Date.now() - start
});
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type StatsEvent = {
operation: 'GET' | 'QUERY' | 'PUT' | 'DELETE' | 'KEYS' | 'SUBSCRIBE' | 'SUBSCRIBE_EMIT';
id?: string;
pattern?: string;
query?: object;
queryResultCount?: number;
timestamp: number;
duration: number;
};
Expand Down
28 changes: 27 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,33 @@ describe('Eventbase with Stats', async () => {
subscription();
});

// Your original tests can continue from here
test('should publish stats events for query operation with query details', async () => {
// Setup some test data
await eventbase1.put('person1', { firstName: 'Joe', lastName: 'Smith' });
await eventbase1.put('person2', { firstName: 'Joe', lastName: 'Brown' });
await eventbase1.put('person3', { firstName: 'Jane', lastName: 'Doe' });

// Clear previous stats events
statsEvents = [];

// Perform the query
const queryObject = { firstName: { $eq: 'Joe' } };
const result = await eventbase1.query(queryObject);

// Wait for stats event to be published
await new Promise((resolve) => setTimeout(resolve, 100));

// Verify stats event
assert.equal(statsEvents.length, 1);
const statsEvent = statsEvents[0];

assert.equal(statsEvent.operation, 'QUERY');
assert.deepEqual(statsEvent.query, queryObject);
assert.equal(statsEvent.queryResultCount, 2);
assert.ok(typeof statsEvent.timestamp === 'number');
assert.ok(typeof statsEvent.duration === 'number');
});

test('should store and retrieve data with metadata', async () => {
const testData = { name: 'John Doe', age: 30 };
await eventbase1.put('user1', testData);
Expand Down
Loading