Assist counter for search filters (#2148)

* feat(assist): added filter's counter

* feat(assist): fixed panic
This commit is contained in:
Alexander 2024-04-29 15:37:24 +02:00 committed by GitHub
parent 5c0974fbba
commit 24d15db93b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 34 deletions

View file

@ -65,39 +65,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) {
@ -211,7 +216,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);
@ -241,7 +246,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

@ -52,13 +52,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) {
@ -88,7 +88,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);
}
}
@ -112,10 +112,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
@ -134,10 +135,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)