Skip to content
Snippets Groups Projects
Commit d0081f75 authored by Christopher Guindon's avatar Christopher Guindon :palm_tree:
Browse files

Merge branch 'oliviergoulet/main/250' into 'main'

Improve error handling of weighted working groups and change api endpoint

Closes #250

See merge request !50
parents b06adf1f fa5d8f51
No related branches found
No related tags found
1 merge request!50Improve error handling of weighted working groups and change api endpoint
Pipeline #14131 passed
...@@ -92,8 +92,4 @@ server { ...@@ -92,8 +92,4 @@ server {
# deny access to .htaccess files, if Apache's document root # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one # concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} }
\ No newline at end of file
...@@ -73,34 +73,40 @@ function getUniqueRandomWorkingGroups(workingGroupsCategorizedByWeight, weightBu ...@@ -73,34 +73,40 @@ function getUniqueRandomWorkingGroups(workingGroupsCategorizedByWeight, weightBu
} }
async function getWeightedRandomWorkingGroups(count) { async function getWeightedRandomWorkingGroups(count) {
const cachedWorkingGroups = JSON.parse(sessionStorage.getItem('weighted-working-groups')); try {
const isCached = cachedWorkingGroups != null; 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 // Only return the cached working groups if the count hasn't changed since last time run
if (isCached && cachedWorkingGroups.length === count) return cachedWorkingGroups; if (isCached && cachedWorkingGroups.length === count) return [cachedWorkingGroups, null];
const response = await fetch('https://membership.eclipse.org/api/working_groups'); const response = await fetch('https://api.eclipse.org/working-groups/');
const workingGroups = await response.json(); if (!response.ok) throw new Error('Could not fetch from the Working Group API');
const weightBuckets = Object const workingGroups = await response.json();
.values(weights)
.filter(removeDuplicates);
// Create an object where the key is a bucket (or weight), and the value an array of working groups tied to that bucket const weightBuckets = Object
const weightBucketObject = weightBuckets.reduce((acc, bucket) => ({...acc, [bucket]: [] }), {}) .values(weights)
.filter(removeDuplicates);
const workingGroupsCategorizedByWeight = workingGroups.reduce((acc, 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 weight = weights[wg.alias] || weights.default; const weightBucketObject = weightBuckets.reduce((acc, bucket) => ({...acc, [bucket]: [] }), {})
acc[weight].push(wg);
return acc; const workingGroupsCategorizedByWeight = workingGroups.reduce((acc, wg) => {
}, { ...weightBucketObject }); const weight = weights[wg.alias] || weights.default;
acc[weight].push(wg);
// Retrieves weighted random working groups and stores it in session storage cache return acc;
const randomWorkingGroups = getUniqueRandomWorkingGroups(workingGroupsCategorizedByWeight, weightBuckets, count); }, { ...weightBucketObject });
sessionStorage.setItem('weighted-working-groups', JSON.stringify(randomWorkingGroups));
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) => { const matchHeightForLogos = (baseElement) => {
...@@ -132,8 +138,14 @@ const getWorkingGroupLogo = ({ alias, logo } = workingGroup) => { ...@@ -132,8 +138,14 @@ const getWorkingGroupLogo = ({ alias, logo } = workingGroup) => {
element.innerHTML = template({ isFetching: true, items: new Array(options.count) , wrapperClass: options.wrapperClass }) 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 = { const data = {
isFetching: false, isFetching: false,
wrapperClass: options.wrapperClass, wrapperClass: options.wrapperClass,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment