Skip to content

Commit

Permalink
?
Browse files Browse the repository at this point in the history
  • Loading branch information
TuvalSimha committed Sep 22, 2024
1 parent a37a6fa commit 5cae3e5
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable, Scope } from 'graphql-modules';
import { ClickHouse, sql } from '../../operations/providers/clickhouse-client';
import { Logger } from '../../shared/providers/logger';
import { AuditLogEvent, auditLogSchema } from './audit-logs-types';
import { sql as c_sql, ClickHouse } from '../../operations/providers/clickhouse-client';


@Injectable({
scope: Scope.Operation,
Expand All @@ -18,38 +19,45 @@ export class AuditLogManager {
}

async createLogAuditEvent(event: AuditLogEvent): Promise<void> {
const { eventType, organizationId, user } = event;
const { organizationId, user } = event;
this.logger.info('Creating a log audit event (event=%o)', event);

const parsedEvent = auditLogSchema.parse(event);
const query = sql`
INSERT INTO audit_log event_time, user_id, user_email, organization_id, event_action, metadata)
FORMAT CSV
`;

const eventTime = new Date().toISOString();

const values = [
eventTime,
user.userId,
user.userEmail,
organizationId,
eventType,
parsedEvent.eventType,
JSON.stringify(parsedEvent),
];

const result = await this.clickHouse.insert({
await this.clickHouse.insert({
query: c_sql`
INSERT INTO "audit_log" (
"id"
, "event_time"
, "user_id"
, "user_email"
, "organization_id"
, "event_action"
, "metadata"
)
FORMAT CSV`,
data: [values],
query,
queryId: 'audit-log-create',
timeout: 5000,
queryId: 'create-audit-log',
});
return result;
}


async getPaginatedAuditLogs(limit: string, offset: string): Promise<AuditLogEvent[]> {
this.logger.info('Getting paginated audit logs (limit=%s, offset=%s)', limit, offset);

const query = sql`
const query = c_sql`
SELECT *
FROM audit_log
ORDER BY event_time DESC
Expand All @@ -70,7 +78,7 @@ export class AuditLogManager {

async getAuditLogsCount(): Promise<number> {
this.logger.info('Getting audit logs count');
const query = sql`
const query = c_sql`
SELECT COUNT(*)
FROM audit_log
`;
Expand Down

0 comments on commit 5cae3e5

Please sign in to comment.