Skip to content

Commit afd3dfe

Browse files
author
Павел Ахметчанов
committed
[#70] show rect procentile
1 parent bea34e1 commit afd3dfe

File tree

2 files changed

+99
-12
lines changed

2 files changed

+99
-12
lines changed

src/charts/AddSlaLine.js

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ const SLA_INPUT_FIELD_ID = 'jira-helper-sla-input';
1515

1616
const log10 = x => Math.log(x) / Math.log(10);
1717

18+
const getBasicSlaRect = (chartElement, slaPathElementIdentifier, strokeColor) => {
19+
const rectId = `${slaPathElementIdentifier}-rect`;
20+
if (document.getElementById(rectId)) {
21+
return document.getElementById(rectId);
22+
}
23+
24+
const namespace = chartElement.namespaceURI;
25+
26+
const slaRect = document.createElementNS(namespace, 'rect');
27+
slaRect.id = rectId;
28+
slaRect.setAttributeNS(null, 'fill', strokeColor || SLA_COLOR);
29+
slaRect.setAttributeNS(null, 'stroke', strokeColor || SLA_COLOR);
30+
slaRect.setAttributeNS(null, 'fill-opacity', '0.25');
31+
slaRect.setAttributeNS(null, 'stroke-width', '1');
32+
slaRect.setAttributeNS(null, 'x', '0');
33+
slaRect.setAttributeNS(null, 'y', '0');
34+
slaRect.setAttributeNS(null, 'width', '0');
35+
slaRect.setAttributeNS(null, 'height', '0');
36+
37+
chartElement.querySelector('.layer.mean').appendChild(slaRect);
38+
39+
return slaRect;
40+
};
41+
1842
const getBasicSlaPath = (chartElement, slaPathElementIdentifier, strokeColor) => {
1943
if (document.getElementById(slaPathElementIdentifier)) {
2044
return document.getElementById(slaPathElementIdentifier);
@@ -58,10 +82,11 @@ const getSlaLabel = (chartElement, slaPathElementIdentifier, fillColor) => {
5882
return slaLabelText;
5983
};
6084

61-
const renderSlaPercentageLabel = (chartElement, value, slaPosition, slaPathElementIdentifier, fillColor) => {
85+
const calculateSlaProcentile = ({ chartElement, slaPosition }) => {
6286
const singleIssuesUnderSlaCount = [...chartElement.querySelectorAll('g.layer.issues circle.issue')].filter(
6387
issue => issue.attributes.cy.value >= slaPosition
6488
).length;
89+
6590
const issuesInClustersUnderSlaCount = [...chartElement.querySelectorAll('g.layer.issue-clusters circle.cluster')]
6691
.filter(issue => issue.attributes.cy.value >= slaPosition)
6792
.map(cluster =>
@@ -76,18 +101,62 @@ const renderSlaPercentageLabel = (chartElement, value, slaPosition, slaPathEleme
76101
)
77102
)
78103
.reduce((a, b) => a + b, 0);
104+
79105
const totalIssuesCount = document.querySelector('.js-chart-snapshot-issue-count').innerText.replace(',', '');
106+
80107
const percentUnderSla = Math.round(
81108
((singleIssuesUnderSlaCount + issuesInClustersUnderSlaCount) / totalIssuesCount) * 100
82109
);
83110

84-
const slaLabel = getSlaLabel(chartElement, slaPathElementIdentifier, fillColor);
111+
return percentUnderSla;
112+
};
113+
114+
const renderSlaPercentageLabel = ({ chartElement, value, slaProcentile, slaPosition, pathId, strokeColor }) => {
115+
const slaLabel = getSlaLabel(chartElement, pathId, strokeColor);
85116

86117
slaLabel.firstChild.innerHTML = `${value}d`;
87-
slaLabel.lastChild.innerHTML = `${percentUnderSla}%`;
118+
slaLabel.lastChild.innerHTML = `${slaProcentile}%`;
88119
slaLabel.setAttributeNS(null, 'y', slaPosition + 12);
89120
};
90121

122+
const findDiaposonForSlaRectPosition = ({ chartElement, slaProcentile, ticsVals }) => {
123+
let pMin = 0;
124+
let pMax = ticsVals.length - 1;
125+
126+
let minSlaPosition = 0;
127+
let minProcentile = 0;
128+
let maxSlaPosition = 0;
129+
let maxProcentile = 0;
130+
let minValue = 0;
131+
let maxValue = 0;
132+
133+
while (pMin <= pMax) {
134+
if (minProcentile !== slaProcentile) {
135+
minValue = ticsVals[pMin].value;
136+
minSlaPosition = getChartLinePosition(ticsVals, minValue);
137+
minProcentile = calculateSlaProcentile({ chartElement, slaPosition: minSlaPosition });
138+
}
139+
if (maxProcentile !== slaProcentile) {
140+
maxValue = ticsVals[pMax].value;
141+
maxSlaPosition = getChartLinePosition(ticsVals, maxValue);
142+
maxProcentile = calculateSlaProcentile({ chartElement, slaPosition: maxSlaPosition });
143+
}
144+
pMin += 1;
145+
pMax -= 1;
146+
}
147+
window.console.log(
148+
{
149+
minSlaPosition,
150+
maxSlaPosition,
151+
},
152+
ticsVals
153+
);
154+
return {
155+
minSlaPosition,
156+
maxSlaPosition,
157+
};
158+
};
159+
91160
const renderSlaLine = (sla, chartElement, changingSlaValue = sla) => {
92161
const ticsVals = getChartTics(chartElement);
93162

@@ -96,10 +165,26 @@ const renderSlaLine = (sla, chartElement, changingSlaValue = sla) => {
96165
const [lineLength] = rightPoint.split(',');
97166

98167
const renderSvgLine = ({ value, pathId, strokeColor }) => {
99-
const slaPath = getBasicSlaPath(chartElement, pathId, strokeColor);
100168
const slaPosition = getChartLinePosition(ticsVals, value);
169+
if (Number.isNaN(slaPosition)) return;
170+
171+
const slaProcentile = calculateSlaProcentile({ chartElement, slaPosition });
172+
const { minSlaPosition, maxSlaPosition } = findDiaposonForSlaRectPosition({
173+
chartElement,
174+
slaProcentile,
175+
ticsVals,
176+
});
177+
178+
const slaPath = getBasicSlaPath(chartElement, pathId, strokeColor);
101179
slaPath.setAttributeNS(null, 'd', `M0,${slaPosition} L${lineLength},${slaPosition}`);
102-
renderSlaPercentageLabel(chartElement, value, slaPosition, pathId, strokeColor);
180+
181+
const slaRect = getBasicSlaRect(chartElement, pathId, strokeColor);
182+
const slaRectHeight = minSlaPosition - maxSlaPosition;
183+
slaRect.setAttributeNS(null, 'y', maxSlaPosition);
184+
slaRect.setAttributeNS(null, 'width', lineLength);
185+
slaRect.setAttributeNS(null, 'height', slaRectHeight);
186+
187+
renderSlaPercentageLabel({ chartElement, value, slaProcentile, slaPosition, pathId, strokeColor });
103188
};
104189

105190
renderSvgLine({

src/charts/utils.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ export const getChartTics = chartElement => {
22
const ticks = [...chartElement.querySelectorAll('.tick')].filter(
33
elem => elem.lastChild.attributes.y.value === '0' && elem.lastChild.textContent
44
);
5-
return ticks.map(elem => {
6-
const [, transform] = elem.attributes.transform.value.split(',');
7-
return {
8-
position: Number(transform.slice(0, -1)),
9-
value: Number(elem.lastChild.textContent),
10-
};
11-
});
5+
return ticks
6+
.map(elem => {
7+
const [, transform] = elem.attributes.transform.value.split(',');
8+
return {
9+
position: Number(transform.slice(0, -1)),
10+
value: Number(elem.lastChild.textContent),
11+
};
12+
})
13+
.sort((a, b) => a.value - b.value);
1214
};
1315

1416
export const getChartLinePosition = (ticksVals, value) => {

0 commit comments

Comments
 (0)