From 08bd3833773c390ecccdfa4237b2f49422ebb93a Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Tue, 16 Jul 2024 10:38:48 -0400 Subject: [PATCH] Add available studies count calculation and caching The update introduces functions to calculate the count of available studies by parsing the study access configuration. It also adds caching for these counts, using the sessionStorage API. Additionally, a minor typo fix in a comment has also been made. --- .../picsureui/studyAccess/study-utility.js | 60 +++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/biodatacatalyst-ui/src/main/webapp/picsureui/studyAccess/study-utility.js b/biodatacatalyst-ui/src/main/webapp/picsureui/studyAccess/study-utility.js index c2ed0a46..4d5b5196 100644 --- a/biodatacatalyst-ui/src/main/webapp/picsureui/studyAccess/study-utility.js +++ b/biodatacatalyst-ui/src/main/webapp/picsureui/studyAccess/study-utility.js @@ -24,8 +24,8 @@ define(["jquery", "backbone", "handlebars", "text!studyAccess/studies-data.json" }; let configurationData = JSON.parse(studyAccessConfiguration); for (let groupid in configurationData) { - for (idx = 0; idx < configurationData[groupid].length; idx++) { - // determine if logged in user is permmited access + for (let idx = 0; idx < configurationData[groupid].length; idx++) { + // determine if logged in user is permitted access let tmpStudy = configurationData[groupid][idx]; const cvc = parseInt(tmpStudy["clinical_variable_count"]).toLocaleString(); tmpStudy["clinical_variable_count"] = cvc == '-1' || cvc == 'NaN' ? 'N/A' : cvc; @@ -70,8 +70,60 @@ define(["jquery", "backbone", "handlebars", "text!studyAccess/studies-data.json" return records; } + function calculateAvailableStudiesAndParticipants() { + let configurationData = JSON.parse(studyAccessConfiguration); + let availableStudiesCount = 0; + let countedStudies = []; + + for (let studyMetadata in configurationData) { + for (let idx = 0; idx < configurationData[studyMetadata].length; idx++) { + let tmpStudy = configurationData[studyMetadata][idx]; + if (tmpStudy['authZ'] !== "" && !countedStudies.includes(tmpStudy["study_identifier"])) { + // We need to keep track of the studies we have already counted. + // It seems there are duplicate studies in the fence mapping + availableStudiesCount++; + countedStudies.push(tmpStudy["study_identifier"]); + } + } + } + + // Cache the values (for demonstration, using sessionStorage) + sessionStorage.setItem("availableStudiesCount", availableStudiesCount); + + return { + availableStudiesCount: availableStudiesCount, + }; + } + + function getAvailableStudiesCount() { + if (!sessionStorage.getItem("availableStudiesCount") || !sessionStorage.getItem("cachedStudyDataHash") || sessionStorage.getItem("cachedStudyDataHash") !== studyAccessConfiguration.hashCode()) { + const { availableStudiesCount } = calculateAvailableStudiesAndParticipants(); + return availableStudiesCount; + } + return parseInt(sessionStorage.getItem("availableStudiesCount")); + } + + // Calculate and cache available studies and total participants if the data is updated + if (!sessionStorage.getItem("cachedStudyDataHash") || sessionStorage.getItem("cachedStudyDataHash") !== studyAccessConfiguration.hashCode()) { + calculateAvailableStudiesAndParticipants(); + sessionStorage.setItem("cachedStudyDataHash", studyAccessConfiguration.hashCode()); + } + return { getStudyAccessConfiguration: getStudyAccessConfiguration, - groupRecordsByAccess: groupRecordsByAccess + groupRecordsByAccess: groupRecordsByAccess, + getAvailableStudiesCount: getAvailableStudiesCount, }; - }); \ No newline at end of file + }); + +// Helper function to calculate a simple hash code for the JSON data (not cryptographic) +String.prototype.hashCode = function () { + var hash = 0, i, chr; + if (this.length === 0) return hash; + for (i = 0; i < this.length; i++) { + chr = this.charCodeAt(i); + hash = ((hash << 5) - hash) + chr; + hash |= 0; // Convert to 32bit integer + } + return hash; +};