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

View file

@ -52,13 +52,13 @@ const getParticularSession = async function (roomId, filters) {
if (!hasFilters(filters)) { if (!hasFilters(filters)) {
return sessInfo; return sessInfo;
} }
if (isValidSession(sessInfo, filters.filter)) { if (isValidSession(sessInfo, filters.filter, {})) {
return sessInfo; return sessInfo;
} }
return null; return null;
} }
const getAllSessions = async function (projectKey, filters, onlineOnly= false) { const getAllSessions = async function (projectKey, filters, counters, onlineOnly= false) {
const sessions = []; const sessions = [];
const connected_sockets = await fetchSockets(); const connected_sockets = await fetchSockets();
if (connected_sockets.length === 0) { 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 // 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); sessions.push(item.handshake.query.sessionInfo);
} }
} }
@ -112,10 +112,11 @@ const socketsListByProject = async function (req, res) {
} }
// find all sessions for a project // find all sessions for a project
const sessions = await getAllSessions(_projectKey, filters); const counters = {};
const sessions = await getAllSessions(_projectKey, filters, counters);
// send response // send response
respond(req, res, sortPaginate(sessions, filters)); respond(req, res, sortPaginate(sessions, filters, counters));
} }
// Sort by projectKey // Sort by projectKey
@ -134,10 +135,11 @@ const socketsLiveByProject = async function (req, res) {
} }
// find all sessions for a project // 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 // send response
respond(req, res, sortPaginate(sessions, filters)); respond(req, res, sortPaginate(sessions, filters, counters));
} }
// Sort by roomID (projectKey+sessionId) // Sort by roomID (projectKey+sessionId)