aboutsummaryrefslogtreecommitdiff
path: root/views/resources/statsviewcontroller.js
diff options
context:
space:
mode:
authorNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2024-05-05 18:28:35 -0400
committerNathan-SR <144961007+Nathan-SR@users.noreply.github.com>2024-05-05 18:28:35 -0400
commit86f55d8aa12268fe847eaa344e8efbab5d293f34 (patch)
tree6bbc5c6fb6825ef969ed0342e4851667b81577cc /views/resources/statsviewcontroller.js
parent2a9db784a6e3492a8f7d8ce9a745b4f1a0494241 (diff)
parent139600ab7e8a82a31744cd3798247236cd5616fc (diff)
Merge branch 'nathan-starter' of https://github.com/brown-dash/Dash-Web into nathan-starter
Diffstat (limited to 'views/resources/statsviewcontroller.js')
-rw-r--r--views/resources/statsviewcontroller.js86
1 files changed, 44 insertions, 42 deletions
diff --git a/views/resources/statsviewcontroller.js b/views/resources/statsviewcontroller.js
index 090e112e7..eceb9cf4e 100644
--- a/views/resources/statsviewcontroller.js
+++ b/views/resources/statsviewcontroller.js
@@ -1,52 +1,52 @@
/**
* statsviewcontroller.js stores the JavaScript functions to update the stats page
- * when the websocket updates.
+ * 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 MEDIUM_USE_BOUND = 100; // operations per 10 seconds
const HIGH_USE_BOUND = 300;
const serverTrafficMessages = {
- 0 : "Not Busy",
- 1 : "Busy",
- 2: "Very Busy"
+ 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) {
+ if (user1.rate < user2.rate) {
return 1;
- } else if(user1.rate > user2.rate) {
+ }
+ if (user1.rate > user2.rate) {
return -1;
- } else {
- return 0;
}
+ 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.
+ * @returns an integer where 0 is not busy, 1 is busy, and 2 is very busy.
*/
function calculateServerTraffic(data) {
- let currentTraffic = data.connectedUsers.length;
+ const currentTraffic = data.connectedUsers.length;
let serverTraffic = 0;
- if(currentTraffic < BUSY_SERVER_BOUND) {
+ if (currentTraffic < BUSY_SERVER_BOUND) {
serverTraffic = 0;
- } else if(currentTraffic >= BUSY_SERVER_BOUND && currentTraffic < VERY_BUSY_SERVER_BOUND) {
+ } else if (currentTraffic >= BUSY_SERVER_BOUND && currentTraffic < VERY_BUSY_SERVER_BOUND) {
serverTraffic = 1;
} else {
serverTraffic = 2;
@@ -62,53 +62,55 @@ function calculateServerTraffic(data) {
* @returns a string representing the color to make the user rate
*/
function getUserRateColor(rate) {
- if(rate < MEDIUM_USE_BOUND) {
- return "black";
- } else if(rate >= MEDIUM_USE_BOUND && rate < HIGH_USE_BOUND) {
- return "orange";
- } else if(rate >= HIGH_USE_BOUND){
- return "red";
- } else {
- return "black";
+ 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.
+ *
+ * @param {*} data the data coming from the backend.
*/
function handleStatsUpdate(data) {
- let userListInnerHTML = "";
+ let userListInnerHTML = '';
data.connectedUsers.sort(userDataComparator);
- data.connectedUsers.map((userData, index) => {
- let userRateColor = getUserRateColor(userData.rate);
- let userEntry = `<p>${userData.time}</p>
+ data.connectedUsers.forEach(userData => {
+ const userRateColor = getUserRateColor(userData.rate);
+ const userEntry = `<p>${userData.time}</p>
<p>${userData.username}</p>
<p>Operations: ${userData.operations}</p>
<p style="color:${userRateColor}">Rate: ${userData.rate} operations per last 10 seconds</p>
`; // user data comes as last 10 seconds but it can be adjusted in DastStats.ts and websocket.ts
- userListInnerHTML += "<li class=\"none\">" + userEntry + "</li>";
- })
+ userListInnerHTML += '<li class="none">' + userEntry + '</li>';
+ });
- document.getElementById("connection-count").innerHTML = `Current Connections: ${data.connectedUsers.length}`
- document.getElementById("connected-user-list").innerHTML = userListInnerHTML;
+ document.getElementById('connection-count').innerHTML = `Current Connections: ${data.connectedUsers.length}`;
+ document.getElementById('connected-user-list').innerHTML = userListInnerHTML;
- let serverTraffic = calculateServerTraffic(data);
- let serverTrafficMessage = "Not Busy";
- switch(serverTraffic) {
+ const serverTraffic = calculateServerTraffic(data);
+ let serverTrafficMessage = 'Not Busy';
+ switch (serverTraffic) {
case 0:
- serverTrafficMessage = "Not Busy";
+ serverTrafficMessage = 'Not Busy';
break;
case 1:
- serverTrafficMessage = "Busy";
+ serverTrafficMessage = 'Busy';
break;
case 2:
- serverTrafficMessage = "Very Busy";
+ serverTrafficMessage = 'Very Busy';
break;
+ default:
}
- document.getElementById("stats-traffic-message").className="stats-server-status-item stats-server-status-" + serverTraffic;
- document.getElementById("stats-traffic-message").innerHTML = `<p>${serverTrafficMessage}</p>`;
-} \ No newline at end of file
+ document.getElementById('stats-traffic-message').className = 'stats-server-status-item stats-server-status-' + serverTraffic;
+ document.getElementById('stats-traffic-message').innerHTML = `<p>${serverTrafficMessage}</p>`;
+}