From fa5d8f512c05d3f4e645a85d3aa0b4e88138aa52 Mon Sep 17 00:00:00 2001 From: Olivier Goulet <olivier.goulet@eclipse-foundation.org> Date: Tue, 17 Jan 2023 19:47:08 +0000 Subject: [PATCH] Improve error handling of weighted working groups and change api endpoint --- config/nginx/default.conf | 4 -- .../eclipsefdn.weighted-working-groups.js | 54 +++++++++++-------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/config/nginx/default.conf b/config/nginx/default.conf index 74e32127..5b1c74c6 100644 --- a/config/nginx/default.conf +++ b/config/nginx/default.conf @@ -92,8 +92,4 @@ server { # deny access to .htaccess files, if Apache's document root # concurs with nginx's one - # - #location ~ /\.ht { - # deny all; - #} } \ No newline at end of file diff --git a/js/src/collaborations/eclipsefdn.weighted-working-groups.js b/js/src/collaborations/eclipsefdn.weighted-working-groups.js index e1232e60..9ed652a9 100644 --- a/js/src/collaborations/eclipsefdn.weighted-working-groups.js +++ b/js/src/collaborations/eclipsefdn.weighted-working-groups.js @@ -73,34 +73,40 @@ function getUniqueRandomWorkingGroups(workingGroupsCategorizedByWeight, weightBu } async function getWeightedRandomWorkingGroups(count) { - const cachedWorkingGroups = JSON.parse(sessionStorage.getItem('weighted-working-groups')); - const isCached = cachedWorkingGroups != null; + try { + const cachedWorkingGroups = JSON.parse(sessionStorage.getItem('weighted-working-groups')); + const isCached = cachedWorkingGroups != null; - // Only return the cached working groups if the count hasn't changed since last time run - if (isCached && cachedWorkingGroups.length === count) return cachedWorkingGroups; + // Only return the cached working groups if the count hasn't changed since last time run + if (isCached && cachedWorkingGroups.length === count) return [cachedWorkingGroups, null]; - const response = await fetch('https://membership.eclipse.org/api/working_groups'); - const workingGroups = await response.json(); + const response = await fetch('https://api.eclipse.org/working-groups/'); + if (!response.ok) throw new Error('Could not fetch from the Working Group API'); - const weightBuckets = Object - .values(weights) - .filter(removeDuplicates); + const workingGroups = await response.json(); - // Create an object where the key is a bucket (or weight), and the value an array of working groups tied to that bucket - const weightBucketObject = weightBuckets.reduce((acc, bucket) => ({...acc, [bucket]: [] }), {}) + const weightBuckets = Object + .values(weights) + .filter(removeDuplicates); - const workingGroupsCategorizedByWeight = workingGroups.reduce((acc, wg) => { - const weight = weights[wg.alias] || weights.default; - acc[weight].push(wg); + // Create an object where the key is a bucket (or weight), and the value an array of working groups tied to that bucket + const weightBucketObject = weightBuckets.reduce((acc, bucket) => ({...acc, [bucket]: [] }), {}) - return acc; - }, { ...weightBucketObject }); + const workingGroupsCategorizedByWeight = workingGroups.reduce((acc, wg) => { + const weight = weights[wg.alias] || weights.default; + acc[weight].push(wg); - // Retrieves weighted random working groups and stores it in session storage cache - const randomWorkingGroups = getUniqueRandomWorkingGroups(workingGroupsCategorizedByWeight, weightBuckets, count); - sessionStorage.setItem('weighted-working-groups', JSON.stringify(randomWorkingGroups)); + return acc; + }, { ...weightBucketObject }); - return randomWorkingGroups; + // Retrieves weighted random working groups and stores it in session storage cache + const randomWorkingGroups = getUniqueRandomWorkingGroups(workingGroupsCategorizedByWeight, weightBuckets, count); + sessionStorage.setItem('weighted-working-groups', JSON.stringify(randomWorkingGroups)); + + return [randomWorkingGroups, null]; + } catch (error) { + return [null, error]; + } }; const matchHeightForLogos = (baseElement) => { @@ -132,8 +138,14 @@ const getWorkingGroupLogo = ({ alias, logo } = workingGroup) => { element.innerHTML = template({ isFetching: true, items: new Array(options.count) , wrapperClass: options.wrapperClass }) - const workingGroups = await getWeightedRandomWorkingGroups(options.count); + const [workingGroups, error] = await getWeightedRandomWorkingGroups(options.count); + if (error) { + element.innerHTML = `<p class="alert alert-danger">Error when attempting to load working groups.</p>`; + console.error(error); + return; + } + const data = { isFetching: false, wrapperClass: options.wrapperClass, -- GitLab