- Major Progress Commit
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
const moment = require('moment');
|
||||
const durationToHumanReadable = require("./durationToHumanReadable");
|
||||
/**
|
||||
* Calculate the duration of each status of a job
|
||||
* @param transitions
|
||||
* @returns {{}}
|
||||
*/
|
||||
const moment = require("moment");
|
||||
const _ = require("lodash");
|
||||
const crypto = require('crypto');
|
||||
|
||||
const getColor = (key) => {
|
||||
const hash = crypto.createHash('sha256');
|
||||
hash.update(key);
|
||||
const hashedKey = hash.digest('hex');
|
||||
const num = parseInt(hashedKey, 16);
|
||||
return '#' + (num % 16777215).toString(16).padStart(6, '0');
|
||||
};
|
||||
|
||||
const calculateStatusDuration = (transitions) => {
|
||||
let statusDuration = {};
|
||||
let totalDuration = 0;
|
||||
let summations = [];
|
||||
|
||||
transitions.forEach((transition, index) => {
|
||||
let duration = transition.duration_minutes;
|
||||
let duration = transition.duration;
|
||||
totalDuration += duration;
|
||||
|
||||
if (!transition.prev_value) {
|
||||
@@ -42,16 +48,31 @@ const calculateStatusDuration = (transitions) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Calculate the percentage for each status
|
||||
// Calculate the percentage for each status
|
||||
let totalPercentage = 0;
|
||||
const statusKeys = Object.keys(statusDuration);
|
||||
statusKeys.forEach((status, index) => {
|
||||
if (index !== statusKeys.length - 1) {
|
||||
const percentage = (statusDuration[status].value / totalDuration) * 100;
|
||||
totalPercentage += percentage;
|
||||
statusDuration[status].percentage = percentage;
|
||||
} else {
|
||||
statusDuration[status].percentage = 100 - totalPercentage;
|
||||
}
|
||||
});
|
||||
|
||||
for (let [status, {value, humanReadable}] of Object.entries(statusDuration)) {
|
||||
if (status !== 'total') {
|
||||
summations.push({status, value, humanReadable});
|
||||
summations.push({status, value, humanReadable, percentage: statusDuration[status].percentage, color: getColor(status), roundedPercentage: `${Math.round(statusDuration[status].percentage)}%`});
|
||||
}
|
||||
}
|
||||
|
||||
const humanReadableTotal = durationToHumanReadable(moment.duration(totalDuration));
|
||||
|
||||
return {
|
||||
summations,
|
||||
summations: _.orderBy(summations, ['value'], ['asc']),
|
||||
totalStatuses: summations.length,
|
||||
total: totalDuration,
|
||||
humanReadableTotal
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user