Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate audit entries #160

Merged
merged 2 commits into from
Apr 11, 2021
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
17 changes: 17 additions & 0 deletions packages/tabler-world-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/tabler-world-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"license": "Copyright 2019-present Markus Kling",
"dependencies": {},
"devDependencies": {
"@types/lodash": "^4.14.149",
"@types/node": "^14.6.0",
"typescript": "4.0.2"
}
}
}
21 changes: 15 additions & 6 deletions packages/tabler-world-common/src/Audit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { values } from 'lodash';
import { format } from 'util';

export type AuditEntry = {
Expand All @@ -18,7 +19,7 @@ export enum AuditAction {
}

export class Audit {
entries: InternalAuditEntry[] = [];
entries: { [key: string]: InternalAuditEntry } = {};

constructor(
private requestId?: string,
Expand All @@ -28,22 +29,30 @@ export class Audit {
}

public async clear() {
this.entries = [];
this.entries = {};
}

public async add(entry: AuditEntry) {
this.entries.push({
const type = entry.type.replace(/ /ig, '_').toLocaleLowerCase();
const key = `${type}:${entry.id}`;

if (this.entries[key] != null) {
// we don't override the time if it has been access more than once
return;
}

this.entries[key] = {
...entry,
type: entry.type.replace(/ /ig, '_').toLocaleLowerCase(),
type,
time: new Date().toISOString(),
});
};
}

/**
* We don't want lambda to play with our log messages
*/
public async dump() {
this.entries.forEach((e) => {
values(this.entries).forEach((e) => {
process.stdout.write(
// tslint:disable-next-line: prefer-template
format('AUDIT', e.time, this.requestId, this.deviceId, this.principal, e.action, e.type, e.id) + '\n',
Expand Down