Skip to content

Commit

Permalink
Deduplicate audit entries, resolves #158 (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskg committed Apr 11, 2021
1 parent cc20984 commit caad881
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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

0 comments on commit caad881

Please sign in to comment.