Skip to content

Commit

Permalink
AdminSocketAnalytics.js: Remove duplicated code, factoring out data t…
Browse files Browse the repository at this point in the history
…ext processing into Util.consumeDataText

Signed-off-by: Sven Göthel <sven.gothel@collabora.com>
Change-Id: I65126d65ae2f43efaeefb8091385a405bd2544e0
  • Loading branch information
Sven Göthel committed Nov 22, 2024
1 parent b872229 commit d70172c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 66 deletions.
83 changes: 17 additions & 66 deletions browser/admin/src/AdminSocketAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ var AdminSocketAnalytics = AdminSocketBase.extend({
textMsg = textMsg.split(' ');

var memStatsSize, memStatsInterval, cpuStatsSize, cpuStatsInterval;
var i, j, data;
var i;
memStatsSize = this._memStatsSize;
memStatsInterval = this._memStatsInterval;
cpuStatsSize = this._cpuStatsSize;
Expand Down Expand Up @@ -510,101 +510,52 @@ var AdminSocketAnalytics = AdminSocketBase.extend({

}
else if (textMsg.startsWith('mem_stats')) {
textMsg = textMsg.split(' ')[1];
if (textMsg.endsWith(',')) {
// This is the result of query, not notification
data = textMsg.substring(0, textMsg.length - 1).split(',');
for (i = this._memStatsData.length - 1, j = data.length - 1; i >= 0 && j >= 0; i--, j--) {
this._memStatsData[i].value = parseInt(data[j]);
}

var data = Util.consumeDataText(textMsg, this._memStatsData);
if (data === undefined) {
this._createGraph('mem');
}
else {
// this is a notification data; append to _memStatsData
data = textMsg.trim();
} else {
this._addNewData(this._memStatsData, data, 'mem');
this._updateMemGraph();
}
}
else if (textMsg.startsWith('cpu_stats')) {
textMsg = textMsg.split(' ')[1];
if (textMsg.endsWith(',')) {
// This is the result of query, not notification
data = textMsg.substring(0, textMsg.length - 1).split(',');

for (i = this._cpuStatsData.length - 1, j = data.length - 1; i >= 0 && j >= 0; i--, j--) {
this._cpuStatsData[i].value = parseInt(data[j]);
}

var data = Util.consumeDataText(textMsg, this._cpuStatsData);
if (data === undefined) {
this._createGraph('cpu');
}
else {
// this is a notification data; append to _cpuStatsData
data = textMsg.trim();
} else {
this._addNewData(this._cpuStatsData, data, 'cpu');
this._updateCpuGraph();
}
}
else if (textMsg.startsWith('sent_activity')) {
textMsg = textMsg.split(' ')[1];
if (textMsg.endsWith(',')) {
// This is the result of query, not notification
data = textMsg.substring(0, textMsg.length - 1).split(',');

for (i = this._sentStatsData.length - 1, j = data.length - 1; i >= 0 && j >= 0; i--, j--) {
this._sentStatsData[i].value = parseInt(data[j]);
}

var data = Util.consumeDataText(textMsg, this._sentStatsData);
if (data === undefined) {
if ($('#NetVisualisation').html() === '')
this._createGraph('net');
}
else {
// this is a notification data; append to _sentStatsData
data = textMsg.trim();
} else {
this._addNewData(this._sentStatsData, parseInt(data), 'sent');
this._updateNetGraph();
}
}
else if (textMsg.startsWith('recv_activity')) {
textMsg = textMsg.split(' ')[1];
if (textMsg.endsWith(',')) {
// This is the result of query, not notification
data = textMsg.substring(0, textMsg.length - 1).split(',');

for (i = this._recvStatsData.length - 1, j = data.length - 1; i >= 0 && j >= 0; i--, j--) {
this._recvStatsData[i].value = parseInt(data[j]);
}

var data = Util.consumeDataText(textMsg, this._recvStatsData);
if (data === undefined) {
if ($('#NetVisualisation').html() === '')
this._createGraph('net');
}
else {
// this is a notification data; append to _recvStatsData
data = textMsg.trim();
} else {
this._addNewData(this._recvStatsData, parseInt(data), 'recv');
this._updateNetGraph();
}
}
else if (textMsg.startsWith('connection_activity')) {
textMsg = textMsg.split(' ')[1];
if (textMsg.endsWith(',')) {
// This is the result of query, not notification
data = textMsg.substring(0, textMsg.length - 1).split(',');

for (i = this._connStatsData.length - 1, j = data.length - 1; i >= 0 && j >= 0; i--, j--) {
this._connStatsData[i].value = parseInt(data[j]);
}

var data = Util.consumeDataText(textMsg, this._connStatsData);
if (data === undefined) {
if ($('#NetVisualisation').html() === '')
this._createGraph('net');
}
else {
// this is a notification data; append to _connStatsData
data = textMsg.trim();
} else {
this._addNewData(this._connStatsData, parseInt(data), 'connect');
this._updateNetGraph();
}
};
}
},

Expand Down
17 changes: 17 additions & 0 deletions browser/admin/src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,22 @@ var Util = Base.extend({
}

return res;
},

consumeDataText: function(textInput, dataArray) {
textInput = textInput.split(' ')[1];
if (textInput.endsWith(',')) {
// This is the result of query, not notification
var i, j, data;
data = textInput.substring(0, textInput.length - 1).split(',');
for (i = dataArray.length - 1, j = data.length - 1; i >= 0 && j >= 0; i--, j--) {
dataArray[i].value = parseInt(data[j]);
}
return undefined;
}
else {
// this is a notification data; append to dataArray
return textInput.trim();
}
}
});

0 comments on commit d70172c

Please sign in to comment.