-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.js
80 lines (73 loc) · 2.63 KB
/
history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('fs');
const path = require('path');
// Function to get the last N log entries from a user's log file
const getRecentLogs = (userId, count) => {
const logFilePath = path.join(__dirname, 'logs', `${userId}.json`);
try {
const logs = JSON.parse(fs.readFileSync(logFilePath, 'utf8')).slice(-count);
return logs.reverse(); // Reverse to display the most recent logs first
} catch (error) {
console.error('Error reading log file:', error);
return [];
}
};
// Function to format a log entry into a table row
const formatLogRow = (log) => {
const { DocumentID, method, path, timestamp, body, params } = log;
return `
<tr>
<td>${log.user}</td>
<td>${DocumentID}</td>
<td>${method}</td>
<td>${path}</td>
<td>${timestamp}</td>
<td><button type="button" class="btn btn-primary" data-toggle="popover" data-content="${JSON.stringify(body)}">View</button></td>
<td><button type="button" class="btn btn-primary" data-toggle="popover" data-content="${JSON.stringify(params)}">View</button></td>
</tr>
`;
};
// Function to generate the history table
const generateHistoryTable = (userId, count) => {
const logs = getRecentLogs(userId, count);
if (logs.length === 0) return '<div class="alert alert-info">No history found.</div>';
let table = `
<div class="table-responsive">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">User</th>
<th scope="col">List Number</th>
<th scope="col">Operator</th>
<th scope="col">Document ID</th>
<th scope="col">Method</th>
<th scope="col">Path</th>
<th scope="col">Time</th>
<th scope="col">Body</th>
</tr>
</thead>
<tbody>
`;
logs.forEach((log, index) => {
table += `
<tr>
<th scope="row">${index + 1}</th>
<td>${log.user}</td>
<td>${log.DocumentID}</td>
<td>${log.method}</td>
<td>${log.path}</td>
<td>${log.timestamp}</td>
<td><a type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="bottom" data-content='${JSON.stringify(log.body)}'">View</a></td>
<td><a href="#" type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="bottom" onclick="showDetails('${JSON.stringify(log.params)}')">View</a></td>
</tr>
`;
});
table += `
</tbody>
</table>
</div>
`;
return table;
};
module.exports = {
generateHistoryTable,
};