2020-06-23 00:09:42 +05:30
|
|
|
import { pickBy } from 'lodash';
|
|
|
|
import { SUPPORTED_FILTER_PARAMETERS } from './constants';
|
|
|
|
|
|
|
|
export const validateParams = params => {
|
|
|
|
return pickBy(params, (val, key) => SUPPORTED_FILTER_PARAMETERS.includes(key) && val);
|
|
|
|
};
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
export const createUniqueLinkId = (stageName, jobName) => `${stageName}-${jobName}`;
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
/**
|
2021-02-22 17:27:13 +05:30
|
|
|
* This function takes the stages array and transform it
|
|
|
|
* into a hash where each key is a job name and the job data
|
|
|
|
* is associated to that key.
|
|
|
|
* @param {Array} stages
|
|
|
|
* @returns {Object} - Hash of jobs
|
2020-11-24 15:15:51 +05:30
|
|
|
*/
|
2021-02-22 17:27:13 +05:30
|
|
|
export const createJobsHash = (stages = []) => {
|
|
|
|
const jobsHash = {};
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
stages.forEach(stage => {
|
|
|
|
if (stage.groups.length > 0) {
|
|
|
|
stage.groups.forEach(group => {
|
|
|
|
group.jobs.forEach(job => {
|
|
|
|
jobsHash[job.name] = job;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
return jobsHash;
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
/**
|
|
|
|
* This function takes the jobs hash generated by
|
|
|
|
* `createJobsHash` function and returns an easier
|
|
|
|
* structure to work with for needs relationship
|
|
|
|
* where the key is the job name and the value is an
|
|
|
|
* array of all the needs this job has recursively
|
|
|
|
* (includes the needs of the needs)
|
|
|
|
* @param {Object} jobs
|
|
|
|
* @returns {Object} - Hash of jobs and array of needs
|
|
|
|
*/
|
|
|
|
export const generateJobNeedsDict = (jobs = {}) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
const arrOfJobNames = Object.keys(jobs);
|
|
|
|
|
|
|
|
return arrOfJobNames.reduce((acc, value) => {
|
|
|
|
const recursiveNeeds = jobName => {
|
|
|
|
if (!jobs[jobName]?.needs) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return jobs[jobName].needs
|
|
|
|
.map(job => {
|
|
|
|
// If we already have the needs of a job in the accumulator,
|
|
|
|
// then we use the memoized data instead of the recursive call
|
|
|
|
// to save some performance.
|
2021-02-22 17:27:13 +05:30
|
|
|
const newNeeds = acc[job] ?? recursiveNeeds(job);
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
return [job, ...newNeeds];
|
2021-01-03 14:25:43 +05:30
|
|
|
})
|
|
|
|
.flat(Infinity);
|
|
|
|
};
|
|
|
|
|
|
|
|
// To ensure we don't have duplicates job relationship when 2 jobs
|
|
|
|
// needed by another both depends on the same jobs, we remove any
|
|
|
|
// duplicates from the array.
|
|
|
|
const uniqueValues = Array.from(new Set(recursiveNeeds(value)));
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
return { ...acc, [value]: uniqueValues };
|
2021-01-03 14:25:43 +05:30
|
|
|
}, {});
|
2020-11-24 15:15:51 +05:30
|
|
|
};
|