Skip to content
Snippets Groups Projects
Commit fa5d8f51 authored by Olivier Goulet's avatar Olivier Goulet Committed by Christopher Guindon
Browse files

Improve error handling of weighted working groups and change api endpoint

parent b06adf1f
No related branches found
No related tags found
1 merge request!50Improve error handling of weighted working groups and change api endpoint
......@@ -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
......@@ -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,
......
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