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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
import { cyan, magenta } from 'colors';
import { Response } from 'express';
import * as fs from 'fs';
import { socketMap, timeMap, userOperations } from './SocketData';
/**
* DashStats focuses on tracking user data for each session.
*
* This includes time connected, number of operations, and
* the rate of their operations
*/
export namespace DashStats {
export const SAMPLING_INTERVAL = 1000; // in milliseconds (ms) - Time interval to update the frontend.
export const RATE_INTERVAL = 10; // in seconds (s) - Used to calculate rate
const statsCSVDirectory = './src/server/stats/';
const statsCSVFilename = statsCSVDirectory + 'userLoginStats.csv';
/**
* UserStats holds the stats associated with a particular user.
*/
interface UserStats {
socketId: string;
username: string;
time: string;
operations: number;
rate: number;
}
/**
* UserLastOperations is the queue object for each user
* storing their past operations.
*/
interface UserLastOperations {
sampleOperations: number; // stores how many operations total are in this rate section (10 sec, for example)
lastSampleOperations: number; // stores how many total operations were recorded at the last sample
previousOperationsQueue: number[]; // stores the operations to calculate rate.
}
/**
* StatsDataBundle represents an object that will be sent to the frontend view
* on each websocket update.
*/
interface StatsDataBundle {
connectedUsers: UserStats[];
}
/**
* CSVStore represents how objects will be stored in the CSV
*/
interface CSVStore {
USERNAME: string;
ACTION: string;
TIME: string;
}
/**
* ServerTraffic describes the current traffic going to the backend.
*/
enum ServerTraffic {
NOT_BUSY,
BUSY,
VERY_BUSY,
}
// These values can be changed after further testing how many
// users correspond to each traffic level in Dash.
const BUSY_SERVER_BOUND = 2;
const VERY_BUSY_SERVER_BOUND = 3;
const serverTrafficMessages = ['Not Busy', 'Busy', 'Very Busy'];
// lastUserOperations maps each username to a UserLastOperations
// structure
export const lastUserOperations = new Map<string, UserLastOperations>();
/**
* convertToCSV() is a helper method that stringifies a CSVStore object
* that can be written to the CSV file later.
* @param dataObject the object to stringify
* @returns the object as a string.
*/
function convertToCSV(dataObject: CSVStore): string {
return `${dataObject.USERNAME},${dataObject.ACTION},${dataObject.TIME}\n`;
}
/**
* getLastOperationsOrDefault() is a helper method that will attempt
* to query the lastUserOperations map for a specified username. If the
* username is not in the map, an empty UserLastOperations object is returned.
* @param username
* @returns the user's UserLastOperations structure or an empty
* UserLastOperations object (All values set to 0) if the username is not found.
*/
function getLastOperationsOrDefault(username: string): UserLastOperations {
if (lastUserOperations.get(username) === undefined) {
const initializeOperationsQueue = [];
for (let i = 0; i < RATE_INTERVAL; i++) {
initializeOperationsQueue.push(0);
}
return {
sampleOperations: 0,
lastSampleOperations: 0,
previousOperationsQueue: initializeOperationsQueue,
};
}
return lastUserOperations.get(username)!;
}
/**
* updateLastOperations updates a specific user's UserLastOperations information
* for the current sampling cycle. The method removes old/outdated counts for
* operations from the queue and adds new data for the current sampling
* cycle to the queue, updating the total count as it goes.
* @param lastOperationData the old UserLastOperations data that must be updated
* @param currentOperations the total number of operations measured for this sampling cycle.
* @returns the udpated UserLastOperations structure.
*/
function updateLastOperations(lastOperationData: UserLastOperations, currentOperations: number): UserLastOperations {
// create a copy of the UserLastOperations to modify
const newLastOperationData: UserLastOperations = {
sampleOperations: lastOperationData.sampleOperations,
lastSampleOperations: lastOperationData.lastSampleOperations,
previousOperationsQueue: lastOperationData.previousOperationsQueue.slice(),
};
let newSampleOperations = newLastOperationData.sampleOperations;
newSampleOperations -= newLastOperationData.previousOperationsQueue.shift()!; // removes and returns the first element of the queue
const operationsThisCycle = currentOperations - lastOperationData.lastSampleOperations;
newSampleOperations += operationsThisCycle; // add the operations this cycle to find out what our count for the interval should be (e.g operations in the last 10 seconds)
// update values for the copy object
newLastOperationData.sampleOperations = newSampleOperations;
newLastOperationData.previousOperationsQueue.push(operationsThisCycle);
newLastOperationData.lastSampleOperations = currentOperations;
return newLastOperationData;
}
/**
* getUserOperationsOrDefault() is a helper method to get the user's total
* operations for the CURRENT sampling interval. The method will return 0
* if the username is not in the userOperations map.
* @param username the username to search the map for
* @returns the total number of operations recorded up to this sampling cycle.
*/
function getUserOperationsOrDefault(username: string): number {
return userOperations.get(username) === undefined ? 0 : userOperations.get(username)!;
}
/**
* getCurrentStats() calculates the total stats for this cycle. In this case,
* getCurrentStats() returns an Array of UserStats[] objects describing
* the stats for each user
* @returns an array of UserStats storing data for each user at the current moment.
*/
function getCurrentStats(): UserStats[] {
const socketPairs: UserStats[] = [];
Array.from(socketMap.entries()).forEach(([key, value]) => {
const username = value.split(' ')[0];
const connectionTime = new Date(timeMap[username]);
const connectionTimeString = connectionTime.toLocaleDateString() + ' ' + connectionTime.toLocaleTimeString();
if (!key.disconnected) {
const lastRecordedOperations = getLastOperationsOrDefault(username);
const currentUserOperationCount = getUserOperationsOrDefault(username);
socketPairs.push({
socketId: key.id,
username: username,
time: connectionTimeString.includes('Invalid Date') ? '' : connectionTimeString,
operations: userOperations.get(username) ? userOperations.get(username)! : 0,
rate: lastRecordedOperations.sampleOperations,
});
lastUserOperations.set(username, updateLastOperations(lastRecordedOperations, currentUserOperationCount));
}
});
return socketPairs;
}
/**
* handleStats is called when the /stats route is called, providing a JSON
* object with relevant stats. In this case, we return the number of
* current connections and
* @param res Response object from Express
*/
export function handleStats(res: Response) {
const current = getCurrentStats();
res.json({
currentConnections: current.length,
socketMap: current,
});
}
/**
* getUpdatedStatesBundle() sends an updated copy of the current stats to the
* frontend /statsview route via websockets.
*
* @returns a StatsDataBundle that is sent to the frontend view on each websocket update
*/
export function getUpdatedStatsBundle(): StatsDataBundle {
const current = getCurrentStats();
return {
connectedUsers: current,
};
}
/**
* handleStatsView() is called when the /statsview route is called. This
* will use pug to render a frontend view of the current stats
*
* @param res
*/
export function handleStatsView(res: Response) {
const current = getCurrentStats();
const connectedUsers = current.map(({ time, username, operations }) => time + ' - ' + username + ' Operations: ' + operations);
let serverTraffic = ServerTraffic.NOT_BUSY;
if (current.length < BUSY_SERVER_BOUND) {
serverTraffic = ServerTraffic.NOT_BUSY;
} else if (current.length >= BUSY_SERVER_BOUND && current.length < VERY_BUSY_SERVER_BOUND) {
serverTraffic = ServerTraffic.BUSY;
} else {
serverTraffic = ServerTraffic.VERY_BUSY;
}
res.render('stats.pug', {
title: 'Dash Stats',
numConnections: connectedUsers.length,
serverTraffic: serverTraffic,
serverTrafficMessage: serverTrafficMessages[serverTraffic],
connectedUsers: connectedUsers,
});
}
/**
* logUserLogin() writes a login event to the CSV file.
*
* @param username the username in the format of "username@domain.com logged in"
* @param socket the websocket associated with the current connection
*/
export function logUserLogin(username: string | undefined) {
if (!(username === undefined)) {
const currentDate = new Date();
console.log(magenta(`User ${username.split(' ')[0]} logged in at: ${currentDate.toISOString()}`));
const toWrite: CSVStore = {
USERNAME: username,
ACTION: 'loggedIn',
TIME: currentDate.toISOString(),
};
if (!fs.existsSync(statsCSVDirectory)) fs.mkdirSync(statsCSVDirectory);
const statsFile = fs.createWriteStream(statsCSVFilename, { flags: 'a' });
statsFile.write(convertToCSV(toWrite));
statsFile.end();
console.log(cyan(convertToCSV(toWrite)));
}
}
/**
* logUserLogout() writes a logout event to the CSV file.
*
* @param username the username in the format of "username@domain.com logged in"
* @param socket the websocket associated with the current connection.
*/
export function logUserLogout(username: string | undefined) {
if (!(username === undefined)) {
const currentDate = new Date();
const statsFile = fs.createWriteStream(statsCSVFilename, { flags: 'a' });
const toWrite: CSVStore = {
USERNAME: username,
ACTION: 'loggedOut',
TIME: currentDate.toISOString(),
};
statsFile.write(convertToCSV(toWrite));
statsFile.end();
}
}
}
|