Skip to content

Commit

Permalink
fix(xo-server-audit/getRecords): limit number of records returned (#6113
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MathieuRA committed Jan 28, 2022
1 parent 899be12 commit c8597bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

> Users must be able to say: “I had this issue, happy to know it's fixed”
- [Plugin/Audit] Fix long data loading when displaying logs (PR [#6113](https://github.com/vatesfr/xen-orchestra/pull/6113))

### Packages to release

> Packages will be released in the order they are here, therefore, they should
Expand All @@ -27,3 +29,5 @@
> - major: if the change breaks compatibility
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- xo-server-audit patch
14 changes: 11 additions & 3 deletions packages/xo-server-audit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class AuditXoPlugin {
getRecords.permission = 'admin'
getRecords.params = {
id: { type: 'string', optional: true },

// null is used to bypass the default limit
maxRecords: { type: ['number', 'null'], optional: true },

ndjson: { type: 'boolean', optional: true },
}

Expand Down Expand Up @@ -290,12 +294,12 @@ class AuditXoPlugin {
}
}

async _getRecords({ id, ndjson = false }) {
async _getRecords({ id, maxRecords = 10e3, ndjson = false }) {
if (ndjson) {
return this._xo
.registerHttpRequest((req, res) => {
res.set('Content-Type', 'application/json')
return fromCallback(pipeline, this._getRecordsStream(id), res)
return fromCallback(pipeline, this._getRecordsStream(id, maxRecords === null ? undefined : maxRecords), res)
})
.then($getFrom => ({
$getFrom,
Expand Down Expand Up @@ -427,8 +431,12 @@ class AuditXoPlugin {
}
}

AuditXoPlugin.prototype._getRecordsStream = asyncIteratorToStream(async function* (id) {
AuditXoPlugin.prototype._getRecordsStream = asyncIteratorToStream(async function* (id, maxRecords = Infinity) {
for await (const record of this._auditCore.getFrom(id)) {
if (--maxRecords < 0) {
break
}

yield JSON.stringify(record)
yield '\n'
}
Expand Down

0 comments on commit c8597bd

Please sign in to comment.