/** * statsviewcontroller.js stores the JavaScript functions to update the stats page * when the websocket updates. */ const BUSY_SERVER_BOUND = 2; const VERY_BUSY_SERVER_BOUND = 3; const MEDIUM_USE_BOUND = 100; // operations per 10 seconds const HIGH_USE_BOUND = 300; const serverTrafficMessages = { 0: 'Not Busy', 1: 'Busy', 2: 'Very Busy', }; /** * userDataComparator sorts the users based on the rate * * @param {*} user1 the first user to compare * @param {*} user2 the second user to comapre * @returns an integer indiciating which user should come first */ function userDataComparator(user1, user2) { if (user1.rate < user2.rate) { return 1; } if (user1.rate > user2.rate) { return -1; } return 0; } /** * calculateServerTraffic() returns an integer corresponding * to the current traffic that can be used to get the message * from "serverTrafficMessages" * * @param {*} data the incoming data from the backend * @returns an integer where 0 is not busy, 1 is busy, and 2 is very busy. */ function calculateServerTraffic(data) { const currentTraffic = data.connectedUsers.length; let serverTraffic = 0; if (currentTraffic < BUSY_SERVER_BOUND) { serverTraffic = 0; } else if (currentTraffic >= BUSY_SERVER_BOUND && currentTraffic < VERY_BUSY_SERVER_BOUND) { serverTraffic = 1; } else { serverTraffic = 2; } return serverTraffic; } /** * getUserRateColor determines what color the user's rate should * be on the front end * @param {*} rate the operations per time interval for a specific user * @returns a string representing the color to make the user rate */ function getUserRateColor(rate) { if (rate < MEDIUM_USE_BOUND) { return 'black'; } if (rate >= MEDIUM_USE_BOUND && rate < HIGH_USE_BOUND) { return 'orange'; } if (rate >= HIGH_USE_BOUND) { return 'red'; } return 'black'; } /** * handleStatsUpdats() is called when new data is received from the backend * from a websocket event. The method updates the HTML site to reflect the * updated data * * @param {*} data the data coming from the backend. */ function handleStatsUpdate(data) { let userListInnerHTML = ''; data.connectedUsers.sort(userDataComparator); data.connectedUsers.forEach(userData => { const userRateColor = getUserRateColor(userData.rate); const userEntry = `
${userData.time}
${userData.username}
Operations: ${userData.operations}
Rate: ${userData.rate} operations per last 10 seconds
`; // user data comes as last 10 seconds but it can be adjusted in DastStats.ts and websocket.ts userListInnerHTML += '${serverTrafficMessage}
`; }