Skip to content

Commit

Permalink
get total logs count, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Aug 27, 2024
1 parent 839ec0d commit f71b4e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,16 @@ export class LogDatabase {
)
}
}

async getLogsCount(): Promise<number> {
try {
const res = await this.provider.collections(this.schema.name).retrieve()
return res && res.num_documents ? res.num_documents : 0
} catch (e) {
DATABASE_LOGGER.error('Unable to retrieve logs count: ' + e.message)
return 0
}
}
}

export class Database {
Expand Down
19 changes: 12 additions & 7 deletions src/test/integration/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,29 +441,34 @@ describe('LogDatabase retrieveMultipleLogs with pagination', () => {
expect(logs.length).to.be.at.most(5)
})

it('should retrieve the total number of log entries', async () => {
const numLogs = await database.logs.getLogsCount()
expect(numLogs).to.be.at.least(logCount)
})

it('should retrieve logs for a specific page', async () => {
const page = 2
const LOGS_PER_PAGE = 5
const logsPage1 = await database.logs.retrieveMultipleLogs(
new Date(Date.now() - 10000), // 10 seconds ago
new Date(), // now
5, // Limit the number of logs to 5 for pagination
LOGS_PER_PAGE, // Limit the number of logs to 5 for pagination
undefined,
undefined,
1 // Page 1
)
console.log('logs 1:', logsPage1)
const logsPage2 = await database.logs.retrieveMultipleLogs(
new Date(Date.now() - 10000), // 10 seconds ago
new Date(), // now
5, // Limit the number of logs to 5 for pagination
LOGS_PER_PAGE, // Limit the number of logs to 5 for pagination
undefined,
undefined,
page // Page 2
2 // Page 2
)
console.log('logs 2:', logsPage2)

// make sure we have enough logs for 2 pages
const logsCount = await database.logs.getLogsCount()
// Ensure that the logs on page 2 are different from those on page 1 if logsPage2 is not empty
if (logsPage2.length > 0) {
if (logsCount > LOGS_PER_PAGE && logsPage2.length > 0) {
expect(logsPage1[0].id).to.not.equal(logsPage2[0].id)
} else {
assert.isEmpty(logsPage2, 'Expected logs to be empty')
Expand Down

0 comments on commit f71b4e6

Please sign in to comment.