feat(assist): added filter's counter (#2153)

This commit is contained in:
Alexander 2024-04-30 15:56:43 +02:00 committed by GitHub
parent 372caf6c7c
commit 7eb3e208fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 34 deletions

View file

@ -63,39 +63,44 @@ const extractSessionIdFromRequest = function (req) {
return undefined;
}
const isValidSession = function (sessionInfo, filters) {
let foundAll = true;
for (const [key, body] of Object.entries(filters)) {
const isValidSession = function (sessionInfo, filters, counter) {
for (const [filterName, body] of Object.entries(filters)) { // range by filter names (key)
if (body.values === undefined || body.values === null) {
return false;
}
if (!counter[filterName]) {
counter[filterName] = {};
}
let found = false;
if (body.values !== undefined && body.values !== null) {
for (const [skey, svalue] of Object.entries(sessionInfo)) {
if (svalue !== undefined && svalue !== null) {
if (typeof (svalue) === "object") {
if (isValidSession(svalue, {[key]: body})) {
found = true;
break;
}
} else if (skey.toLowerCase() === key.toLowerCase()) {
for (let v of body["values"]) {
if (body.operator === "is" && v && String(svalue).toLowerCase() === String(v).toLowerCase()
|| body.operator !== "is" && String(svalue).toLowerCase().indexOf(String(v).toLowerCase()) >= 0) {
found = true;
break;
}
}
if (found) {
break;
}
for (const [sessKey, sessValue] of Object.entries(sessionInfo)) {
if (sessValue === undefined || sessValue === null) {
continue;
}
if (typeof (sessValue) === "object") {
if (isValidSession(sessValue, {[filterName]: body}, counter)) {
found = true;
break;
}
} else if (sessKey.toLowerCase() === filterName.toLowerCase()) {
for (let v of body.values) {
if (body.operator === "is" && v && String(sessValue).toLowerCase() === String(v).toLowerCase()
|| body.operator !== "is" && String(sessValue).toLowerCase().indexOf(String(v).toLowerCase()) >= 0) {
found = true;
if (counter[filterName][v]) counter[filterName][v]++;
else counter[filterName][v] = 1;
break;
}
}
if (found) {
break;
}
}
}
foundAll = foundAll && found;
if (!found) {
break;
return false;
}
}
return foundAll;
return true;
}
const getValidAttributes = function (sessionInfo, query) {
@ -209,7 +214,7 @@ const getValue = function (obj, key) {
return undefined;
}
const sortPaginate = function (list, filters) {
const sortPaginate = function (list, filters, counter) {
if (typeof (list) === "object" && !Array.isArray(list)) {
for (const [key, value] of Object.entries(list)) {
list[key] = sortPaginate(value, filters);
@ -239,7 +244,7 @@ const sortPaginate = function (list, filters) {
list = list.slice((filters.pagination.page - 1) * filters.pagination.limit,
filters.pagination.page * filters.pagination.limit);
}
return {"total": total, "sessions": list};
return {"total": total, "sessions": list, "counter": counter};
}
const uniqueAutocomplete = function (list) {

View file

@ -53,13 +53,13 @@ const getParticularSession = async function (roomId, filters) {
if (!hasFilters(filters)) {
return sessInfo;
}
if (isValidSession(sessInfo, filters.filter)) {
if (isValidSession(sessInfo, filters.filter, {})) {
return sessInfo;
}
return null;
}
const getAllSessions = async function (projectKey, filters, onlineOnly= false) {
const getAllSessions = async function (projectKey, filters, counters, onlineOnly= false) {
const sessions = [];
const connected_sockets = await fetchSockets();
if (connected_sockets.length === 0) {
@ -89,7 +89,7 @@ const getAllSessions = async function (projectKey, filters, onlineOnly= false)
}
// Add session to the list if it passes the filter
if (isValidSession(item.handshake.query.sessionInfo, filters.filter)) {
if (isValidSession(item.handshake.query.sessionInfo, filters.filter, counters)) {
sessions.push(item.handshake.query.sessionInfo);
}
}
@ -113,10 +113,11 @@ const socketsListByProject = async function (req, res) {
}
// find all sessions for a project
const sessions = await getAllSessions(_projectKey, filters);
const counters = {};
const sessions = await getAllSessions(_projectKey, filters, counters);
// send response
respond(req, res, sortPaginate(sessions, filters));
respond(req, res, sortPaginate(sessions, filters, counters));
}
// Sort by projectKey
@ -135,10 +136,11 @@ const socketsLiveByProject = async function (req, res) {
}
// find all sessions for a project
const sessions = await getAllSessions(_projectKey, filters, true);
const counters = {};
const sessions = await getAllSessions(_projectKey, filters, counters, true);
// send response
respond(req, res, sortPaginate(sessions, filters));
respond(req, res, sortPaginate(sessions, filters, counters));
}
// Sort by roomID (projectKey+sessionId)