generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 201
/
logs.js
259 lines (237 loc) · 7.14 KB
/
logs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const firestore = require("../utils/firestore");
const { getBeforeHourTime } = require("../utils/time");
const logsModel = firestore.collection("logs");
const admin = require("firebase-admin");
const { logType, ERROR_WHILE_FETCHING_LOGS } = require("../constants/logs");
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
const { getFullName } = require("../utils/users");
const {
getUsersListFromLogs,
formatLogsForFeed,
mapify,
convertTimestamp,
getTasksFromLogs,
} = require("../utils/logs");
const SIZE = 25;
/**
* Adds log
*
* @param type { String }: Type of the log
* @param meta { Object }: Meta data of the log
* @param body { Object }: Body of the log
*/
const addLog = async (type, meta, body) => {
try {
const log = {
type,
timestamp: admin.firestore.Timestamp.fromDate(new Date()),
meta,
body,
};
return await logsModel.add(log);
} catch (err) {
logger.error("Error in adding log", err);
throw new Error(INTERNAL_SERVER_ERROR);
}
};
/**
* Fetches logs
*
* @param query { String }: Type of the log
* @param param { Object }: Fields to filter logs
*/
const fetchLogs = async (query, param) => {
try {
let call = logsModel.where("type", "==", param);
Object.keys(query).forEach((key) => {
// eslint-disable-next-line security/detect-object-injection
if (key !== "limit" && key !== "lastDocId") {
// eslint-disable-next-line security/detect-object-injection
call = call.where(key, "==", query[key]);
}
});
const { limit, lastDocId, userId } = query;
let lastDoc;
const limitDocuments = Number(limit);
if (lastDocId) {
lastDoc = await logsModel.doc(lastDocId).get();
}
if (userId) {
const logsSnapshot = await logsModel
.where("type", "==", param)
.where("body.archived_user.user_id", "==", userId)
.orderBy("timestamp", "desc")
.get();
const logs = [];
logsSnapshot.forEach((doc) => {
logs.push({
...doc.data(),
});
});
return logs;
}
const logsSnapshotQuery = call.orderBy("timestamp", "desc").startAfter(lastDoc ?? "");
const snapshot = limit
? await logsSnapshotQuery.limit(limitDocuments).get()
: await logsSnapshotQuery.limit(10).get();
const logs = [];
snapshot.forEach((doc) => {
logs.push({
...doc.data(),
});
});
// If dev flag is presend and extensionRequest logs are requested, populate userId
if (param === "extensionRequests") {
const userIdNameMap = {};
for await (const log of logs) {
if (log.meta.userId) {
if (userIdNameMap[log.meta.userId]) {
log.meta.name = userIdNameMap[log.meta.userId];
} else {
const name = await getFullName(log.meta.userId);
log.meta.name = `${name?.first_name} ${name?.last_name}`;
userIdNameMap[log.meta.userId] = log.meta.name;
}
}
}
}
return logs;
} catch (err) {
logger.error("Error in adding log", err);
throw new Error(INTERNAL_SERVER_ERROR);
}
};
/**
* Fetches purged cache logs
*
* @param userId { String }: Unique ID of the User
*/
const fetchCacheLogs = async (id) => {
try {
const logsSnapshot = await logsModel
.where("type", "==", logType.CLOUDFLARE_CACHE_PURGED)
.where("timestamp", ">=", getBeforeHourTime(admin.firestore.Timestamp.fromDate(new Date()), 24))
.where("meta.userId", "==", id)
.get();
const logs = [];
logsSnapshot.forEach((doc) => {
const { timestamp } = doc.data();
if (logs.length < 3) {
logs.push({ timestamp });
}
});
return logs;
} catch (err) {
logger.error("Error in fetching cache logs", err);
throw err;
}
};
/**
* Fetches last purged cache log added
*
* @param userId { String }: Unique ID of the User
*/
const fetchLastAddedCacheLog = async (id) => {
try {
const lastLogSnapshot = await logsModel
.where("type", "==", logType.CLOUDFLARE_CACHE_PURGED)
.where("meta.userId", "==", id)
.limit(1)
.orderBy("timestamp", "desc")
.get();
const logs = [];
lastLogSnapshot.forEach((doc) => {
const { timestamp } = doc.data();
logs.push({ timestamp });
});
return logs;
} catch (err) {
logger.error("Error in fetching purged cache logs", err);
throw err;
}
};
const fetchAllLogs = async (query) => {
let { type, prev, next, page, size = SIZE, format } = query;
size = parseInt(size);
page = parseInt(page);
try {
let requestQuery = logsModel;
if (type) {
const logType = type.split(",");
if (logType.length >= 1) requestQuery = requestQuery.where("type", "in", logType);
}
requestQuery = requestQuery.orderBy("timestamp", "desc");
let requestQueryDoc = requestQuery;
if (prev) {
requestQueryDoc = requestQueryDoc.limitToLast(size);
} else {
requestQueryDoc = requestQueryDoc.limit(size);
}
if (page) {
const startAfter = (page - 1) * size;
requestQueryDoc = requestQueryDoc.offset(startAfter);
} else if (next) {
const doc = await logsModel.doc(next).get();
requestQueryDoc = requestQueryDoc.startAt(doc);
} else if (prev) {
const doc = await logsModel.doc(prev).get();
requestQueryDoc = requestQueryDoc.endAt(doc);
}
const snapshot = await requestQueryDoc.get();
let nextDoc, prevDoc;
if (!snapshot.empty) {
const first = snapshot.docs[0];
prevDoc = await requestQuery.endBefore(first).limitToLast(1).get();
const last = snapshot.docs[snapshot.docs.length - 1];
nextDoc = await requestQuery.startAfter(last).limit(1).get();
}
const allLogs = [];
if (!snapshot.empty) {
snapshot.forEach((doc) => {
allLogs.push({ ...doc.data() });
});
}
if (allLogs.length === 0) {
return {
allLogs: [],
prev: null,
next: null,
page: page ? page + 1 : null,
};
}
if (format === "feed") {
let logsData = [];
const userList = await getUsersListFromLogs(allLogs);
const taskIdList = await getTasksFromLogs(allLogs);
const usersMap = mapify(userList, "id");
const tasksMap = mapify(taskIdList, "id");
logsData = allLogs.map((data) => {
const formattedLogs = formatLogsForFeed(data, usersMap, tasksMap);
if (!Object.keys(formattedLogs).length) return null;
return { ...formattedLogs, type: data.type, timestamp: convertTimestamp(data.timestamp) };
});
return {
allLogs: logsData.filter((log) => log),
prev: prevDoc.empty ? null : prevDoc.docs[0].id,
next: nextDoc.empty ? null : nextDoc.docs[0].id,
page: page ? page + 1 : null,
};
}
return {
allLogs: allLogs.filter((log) => log),
prev: prevDoc.empty ? null : prevDoc.docs[0].id,
next: nextDoc.empty ? null : nextDoc.docs[0].id,
page: page ? page + 1 : null,
};
} catch (error) {
logger.error(ERROR_WHILE_FETCHING_LOGS, error);
throw error;
}
};
module.exports = {
addLog,
fetchLogs,
fetchCacheLogs,
fetchLastAddedCacheLog,
fetchAllLogs,
};