|
1 | 1 | import winston from 'winston';
|
2 | 2 | import { connectAndPrepare } from '../modules/database';
|
3 | 3 | import { StartAuditLogsQueryParams, startAuditLogsQuery, getAuditLogQueryResults } from '../endpoints';
|
4 |
| -import { getTeamDeviceCredentials } from '../utils'; |
| 4 | +import { getTeamDeviceCredentials, jsonToCsv } from '../utils'; |
5 | 5 |
|
6 |
| -export const runTeamLogs = async (options: { start: string; end: string; type: string; category: string }) => { |
| 6 | +export const runTeamLogs = async (options: { |
| 7 | + start: string; |
| 8 | + end: string; |
| 9 | + type: string; |
| 10 | + category: string; |
| 11 | + csv: boolean; |
| 12 | +}) => { |
7 | 13 | const teamDeviceCredentials = getTeamDeviceCredentials();
|
8 | 14 |
|
9 | 15 | const { start, type, category } = options;
|
10 | 16 | const end = options.end === 'now' ? Date.now().toString() : options.end;
|
11 | 17 |
|
12 | 18 | const { db } = await connectAndPrepare({ autoSync: false });
|
13 |
| - await getAuditLogs({ |
| 19 | + const logs = await getAuditLogs({ |
14 | 20 | teamDeviceCredentials,
|
15 | 21 | startDateRangeUnix: parseInt(start),
|
16 | 22 | endDateRangeUnix: parseInt(end),
|
17 | 23 | logType: type,
|
18 | 24 | category,
|
19 | 25 | });
|
20 | 26 | db.close();
|
| 27 | + |
| 28 | + if (options.csv) { |
| 29 | + console.log(jsonToCsv(logs.map((log) => JSON.parse(log) as object))); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + logs.forEach((log) => console.log(log)); |
21 | 34 | };
|
22 | 35 |
|
23 | 36 | const MAX_RESULT = 1000;
|
24 | 37 |
|
25 |
| -export const getAuditLogs = async (params: StartAuditLogsQueryParams) => { |
| 38 | +export const getAuditLogs = async (params: StartAuditLogsQueryParams): Promise<string[]> => { |
26 | 39 | const { teamDeviceCredentials } = params;
|
27 | 40 |
|
28 | 41 | const { queryExecutionId } = await startAuditLogsQuery(params);
|
@@ -52,5 +65,5 @@ export const getAuditLogs = async (params: StartAuditLogsQueryParams) => {
|
52 | 65 | logs = logs.concat(result.results);
|
53 | 66 | }
|
54 | 67 |
|
55 |
| - logs.forEach((log) => console.log(log)); |
| 68 | + return logs; |
56 | 69 | };
|
0 commit comments