-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding
auditEventMultiPathGet
handler to support getting audi…
…t events while selecting multiple paths [ci skip]
- Loading branch information
1 parent
dc72c7c
commit 4705bc9
Showing
9 changed files
with
704 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { HandlerTypes } from '@matrixai/rpc'; | ||
import type AuditEventsMultiPathGet from '../handlers/AuditEventsMultiPathGet'; | ||
import { ServerCaller } from '@matrixai/rpc'; | ||
|
||
type CallerTypes = HandlerTypes<AuditEventsMultiPathGet>; | ||
|
||
const auditEventsMultiPathGet = new ServerCaller< | ||
CallerTypes['input'], | ||
CallerTypes['output'] | ||
>(); | ||
|
||
export default auditEventsMultiPathGet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import type { ContextTimed } from '@matrixai/contexts'; | ||
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types'; | ||
import type { | ||
AuditEvent, | ||
AuditEventSerialized, | ||
AuditEventToAuditEventSerialized, | ||
TopicPath, | ||
} from '../../audit/types'; | ||
import type { Audit } from '../../audit'; | ||
import type { AuditEventId, AuditEventIdEncoded } from '../../ids'; | ||
import { ServerHandler } from '@matrixai/rpc'; | ||
import * as auditUtils from '../../audit/utils'; | ||
|
||
class AuditEventsGet extends ServerHandler< | ||
{ | ||
audit: Audit; | ||
}, | ||
ClientRPCRequestParams<{ | ||
paths: Array<string>; | ||
seek?: AuditEventIdEncoded | number; | ||
seekEnd?: AuditEventIdEncoded | number; | ||
order?: 'asc' | 'desc'; | ||
limit?: number; | ||
awaitFutureEvents?: boolean; | ||
}>, | ||
ClientRPCResponseResult<AuditEventSerialized> | ||
> { | ||
public async *handle( | ||
{ | ||
paths, | ||
seek, | ||
seekEnd, | ||
order = 'asc', | ||
limit, | ||
awaitFutureEvents = false, | ||
}: ClientRPCRequestParams<{ | ||
seek?: AuditEventIdEncoded | number; | ||
seekEnd?: AuditEventIdEncoded | number; | ||
order?: 'asc' | 'desc'; | ||
limit?: number; | ||
awaitFutureEvents?: boolean; | ||
}> & { | ||
paths: Array<string>; | ||
}, | ||
_cancel, | ||
_meta, | ||
ctx: ContextTimed, | ||
): AsyncGenerator< | ||
ClientRPCResponseResult<AuditEventToAuditEventSerialized<AuditEvent>> | ||
> { | ||
const { audit } = this.container; | ||
const iterators: Array<AsyncGenerator<AuditEvent>> = []; | ||
let seek_: AuditEventId | number | undefined; | ||
if (seek != null) { | ||
seek_ = | ||
typeof seek === 'string' ? auditUtils.decodeAuditEventId(seek) : seek; | ||
} | ||
let seekEnd_: AuditEventId | number | undefined; | ||
if (seekEnd != null) { | ||
seekEnd_ = | ||
typeof seekEnd === 'string' | ||
? auditUtils.decodeAuditEventId(seekEnd) | ||
: seekEnd; | ||
} | ||
|
||
// Convert the paths | ||
const topicPaths: Array<TopicPath> = []; | ||
for (const filteredPath of auditUtils.filterSubPaths(paths)) { | ||
const topicPath = filteredPath.split('.'); | ||
if (auditUtils.isTopicPath(topicPath)) topicPaths.push(topicPath); | ||
} | ||
|
||
// If the call is descending chronologically, or does not want to await future events, | ||
// it should not await future events. | ||
for (const topicPath of topicPaths) { | ||
if (!awaitFutureEvents) { | ||
const iterator = audit.getAuditEvents(topicPath, { | ||
seek: seek_, | ||
seekEnd: seekEnd_, | ||
order: order, | ||
limit: limit, | ||
}); | ||
iterators.push(iterator); | ||
} else { | ||
const iterator = audit.getAuditEventsLongRunning(topicPath, { | ||
seek: seek_, | ||
seekEnd: seekEnd_, | ||
limit: limit, | ||
}); | ||
iterators.push(iterator); | ||
} | ||
} | ||
|
||
// We need to reverse the compare if we are descending in time | ||
const orderSwitchMultiplier = awaitFutureEvents || order === 'asc' ? 1 : -1; | ||
function sortFn(a: AuditEvent, b: AuditEvent) { | ||
return Buffer.compare(a.id, b.id) * orderSwitchMultiplier; | ||
} | ||
|
||
const combinedIterator = auditUtils.genSort<AuditEvent>( | ||
sortFn, | ||
...iterators, | ||
); | ||
ctx.signal.addEventListener('abort', async () => { | ||
await combinedIterator.return(ctx.signal.reason); | ||
}); | ||
for await (const auditEvent of combinedIterator) { | ||
yield { | ||
id: auditUtils.encodeAuditEventId(auditEvent.id), | ||
path: auditEvent.path, | ||
data: auditEvent.data, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
export default AuditEventsGet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.