Skip to content

Commit 8aa9209

Browse files
Goal lines refactoring (#5258)
**Story card:** None - bug fix ## Because Needed to refactor monthIndexFromDateKey() function. This will be needed in a follow up PR that fixes a bug on the dashed lines of the overdue chart ## This addresses Refactor complete plus some additional work to simplify the goal lines functionality and make it easier to follow Changes: - goalPeriodValue() renamed to getThreeMonthAverageAndImprovementRatio() - improvement ratio is calculated inside the getThreeMonthAverageAndImprovementRatio() rather than passing data around - simplified check for isEndMonthOfYear() (previously named isLastDateKeysArrayMonthDec) - improved variable naming - monthIndexFromDateKey() uses built in javascript function - calculateGoalUpwards() and calculateGoalDownwards() have parameter names that match the data being passed (monthValue -> threeMonthAverage) - correction to how the remaining months in the year is calculated: ``` const monthsRemainingForYear = monthThreeIndex === 11 ? 12 : 11 - monthThreeIndex; ``` ## Test instructions Switch between this branch and the current master branch to check that the figures are the same. Note: there was a previous miscalculation on the goal lines. When checking the figures are the same there might be a small chance 'this branch/PR' shows a figure that is 1% less than that shows on the current master. This is correct - the master was +0.0083 higher than it should have been after running the calculations. Figures are rounded so there should be no change unless it crosses to the next integer. To view the goal lines add the commented line to your reports.js file ``` const enabledRegions = { IN: ["state"], // add this line <- BD: ["organization", "state", "district"], ET: ["organization", "state"], LK: ["organization"], }; ``` the line for copying ``` IN: ["state"], ``` --------- Co-authored-by: Priyanga P Kini <[email protected]>
1 parent 7eead8b commit 8aa9209

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

app/assets/javascripts/common/reports.js

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -662,83 +662,70 @@ Reports = function ({
662662
}
663663

664664
function calculateGoal(periodValues, goalDownwards) {
665-
const { threeMonthAverage, monthThreeIndexOfYear } = goalPeriodValue(periodValues);
666-
const improvementRatio = relativeImprovementRatio(monthThreeIndexOfYear);
665+
const { threeMonthAverage, improvementRatio } = getThreeMonthAverageAndImprovementRatio(periodValues);
667666

668667
if (goalDownwards) {
669668
return calculateGoalDownwards(threeMonthAverage, improvementRatio);
670669
}
671670
return calculateGoalUpwards(threeMonthAverage, improvementRatio);
672671
}
673672

674-
function goalPeriodValue(periodValues) {
675-
const dateKeysArray = Object.keys(periodValues);
673+
function getThreeMonthAverageAndImprovementRatio(periodValues) {
674+
const evaluationMonth = "Dec"
675+
const dateKeys = Object.keys(periodValues);
676+
const evaluationMonthKeys = dateKeys.filter((item) => item.includes(evaluationMonth));
676677

677-
const decemberKeys = dateKeysArray.filter((item) => item.includes("Dec"));
678-
const isLastDateKeysArrayMonthDec =
679-
monthIndexFromDateString(dateKeysArray[dateKeysArray.length - 1]) === 11;
680-
if (isLastDateKeysArrayMonthDec) {
681-
decemberKeys.splice(-1);
678+
const [lastMonthKey] = dateKeys.slice(-1);
679+
const isEndMonthOfYear = lastMonthKey.includes(evaluationMonth);
680+
if (isEndMonthOfYear) {
681+
evaluationMonthKeys.splice(-1);
682682
}
683-
const mostRecentDecemberKey = decemberKeys[decemberKeys.length - 1];
684-
const indexOfLatestDecember = dateKeysArray.indexOf(mostRecentDecemberKey);
683+
684+
const mostEvaluationMonthKey = evaluationMonthKeys[evaluationMonthKeys.length - 1];
685+
const mostEvaluationMonthKeyIndex = dateKeys.indexOf(mostEvaluationMonthKey);
685686

686687
const monthThreeDateKey =
687-
indexOfLatestDecember < defaultMonthsRequired - 1
688-
? dateKeysArray[defaultMonthsRequired - 1] // month 6
689-
: mostRecentDecemberKey; // december
688+
mostEvaluationMonthKeyIndex < defaultMonthsRequired - 1
689+
? dateKeys[defaultMonthsRequired - 1] // month 6
690+
: mostEvaluationMonthKey; // eveluation month
691+
692+
const monthThreeIndex = dateKeys.indexOf(monthThreeDateKey);
690693

691694
const monthThreeValue = periodValues[monthThreeDateKey];
692-
const indexOfMonthThreeInDateKeys = dateKeysArray.indexOf(monthThreeDateKey);
693-
const monthTwoValue = periodValues[dateKeysArray[indexOfMonthThreeInDateKeys - 1]];
694-
const monthOneValue = periodValues[dateKeysArray[indexOfMonthThreeInDateKeys - 2]];
695+
const monthTwoValue = periodValues[dateKeys[monthThreeIndex - 1]];
696+
const monthOneValue = periodValues[dateKeys[monthThreeIndex - 2]];
695697

696698
const sumValues = monthOneValue + monthTwoValue + monthThreeValue;
697699
const threeMonthAverage = sumValues === 0 ? 0 : sumValues / 3;
700+
701+
const monthThreeIndexOfYear = monthIndexFromDateKey(monthThreeDateKey);
702+
const improvementRatio = relativeImprovementRatio(monthThreeIndexOfYear);
698703

699-
const monthThreeIndexOfYear = monthIndexFromDateString(monthThreeDateKey);
700704
return {
701705
threeMonthAverage,
702-
monthThreeIndexOfYear,
706+
improvementRatio,
703707
};
704708
}
705709

706-
function monthIndexFromDateString(dateString) {
707-
const [month, year] = dateString.split("-");
708-
const months = [
709-
"jan",
710-
"feb",
711-
"mar",
712-
"apr",
713-
"may",
714-
"jun",
715-
"jul",
716-
"aug",
717-
"sep",
718-
"oct",
719-
"nov",
720-
"dec",
721-
];
722-
723-
return months.indexOf(month.toLowerCase());
710+
function monthIndexFromDateKey(dateString) {
711+
return new Date(dateString).getMonth();
724712
}
725713

726-
function calculateGoalUpwards(monthValue, improvementRatio) {
727-
const goal = monthValue + (100 - monthValue) * improvementRatio;
728-
return Math.ceil(goal);
714+
function relativeImprovementRatio(monthThreeIndex) {
715+
const annualRelativeImprovement = 0.1; // 10%
716+
const monthlyRelativeImprovement = annualRelativeImprovement / 12;
717+
const monthsRemainingForYear = monthThreeIndex === 11 ? 12 : 11 - monthThreeIndex; // dec = 12, jan = 11... nov = 1
718+
return monthlyRelativeImprovement * monthsRemainingForYear;
729719
}
730720

731-
function calculateGoalDownwards(monthValue, improvementRatio) {
732-
const goal = monthValue - monthValue * improvementRatio;
733-
return Math.floor(goal);
721+
function calculateGoalUpwards(threeMonthAverage, improvementRatio) {
722+
const goal = threeMonthAverage + ((100 - threeMonthAverage) * improvementRatio);
723+
return Math.ceil(goal);
734724
}
735725

736-
function relativeImprovementRatio(goalMonthIndex) {
737-
const defaultRelativeImprovementPercentage = 10;
738-
const improvementPercentagePerMonth =
739-
defaultRelativeImprovementPercentage / 100 / 12;
740-
const monthsRemainingInYear = 12 - (goalMonthIndex % 11); // dec is full year (0 index)
741-
return improvementPercentagePerMonth * monthsRemainingInYear;
726+
function calculateGoalDownwards(threeMonthAverage, improvementRatio) {
727+
const goal = threeMonthAverage - (threeMonthAverage * improvementRatio);
728+
return Math.floor(goal);
742729
}
743730

744731
// - canvas drawing
@@ -1693,7 +1680,6 @@ function baseBarChartConfig() {
16931680
};
16941681
}
16951682

1696-
16971683
// [Segment] Functions
16981684
//
16991685
/**

0 commit comments

Comments
 (0)