Skip to content

Commit

Permalink
Merge pull request #1297 from hpehl/HAL-2007
Browse files Browse the repository at this point in the history
HAL-2007: Fix utilization bar if current > total
  • Loading branch information
hpehl authored Jan 16, 2025
2 parents b502df8 + e16caf4 commit 0b7d098
Showing 1 changed file with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@

import static org.jboss.elemento.Elements.div;
import static org.jboss.elemento.Elements.span;
import static org.jboss.hal.resources.CSS.*;
import static org.jboss.hal.resources.CSS.progress;
import static org.jboss.hal.resources.CSS.progressBar;
import static org.jboss.hal.resources.CSS.progressBarDanger;
import static org.jboss.hal.resources.CSS.progressBarRemaining;
import static org.jboss.hal.resources.CSS.progressBarSuccess;
import static org.jboss.hal.resources.CSS.progressBarWarning;
import static org.jboss.hal.resources.CSS.progressContainer;
import static org.jboss.hal.resources.CSS.progressDescription;
import static org.jboss.hal.resources.CSS.progressDescriptionLeft;
import static org.jboss.hal.resources.CSS.progressLabelRight;
import static org.jboss.hal.resources.CSS.progressLabelTopRight;
import static org.jboss.hal.resources.CSS.srOnly;
import static org.jboss.hal.resources.CSS.width;
import static org.jboss.hal.resources.UIConstants.PROGRESSBAR;
import static org.jboss.hal.resources.UIConstants.ROLE;
import static org.jboss.hal.resources.UIConstants.TOGGLE;
Expand All @@ -41,7 +53,7 @@
* percentage of that resource that has been utilized.
*
* @see <a href=
* "https://www.patternfly.org/pattern-library/data-visualization/utilization-bar-chart/">https://www.patternfly.org/pattern-library/data-visualization/utilization-bar-chart/</a>
* "https://www.patternfly.org/pattern-library/data-visualization/utilization-bar-chart/">https://www.patternfly.org/pattern-library/data-visualization/utilization-bar-chart/</a>
*/
public class Utilization implements IsElement {

Expand All @@ -67,11 +79,11 @@ public Utilization(String label, String unit, boolean inline, boolean thresholds
this.total = 0;

String[] containerCss = inline
? new String[] { progressContainer, progressDescriptionLeft, progressLabelRight }
: new String[] { progressContainer };
? new String[]{progressContainer, progressDescriptionLeft, progressLabelRight}
: new String[]{progressContainer};
String[] progressCss = inline
? new String[] { progress }
: new String[] { progress, progressLabelTopRight };
? new String[]{progress}
: new String[]{progress, progressLabelTopRight};

root = div().css(containerCss)
.add(div().css(progressDescription)
Expand Down Expand Up @@ -106,46 +118,44 @@ public void update(long current) {
}

public void update(long current, long total) {
if (current <= total) {
this.total = total;
double currentPercent = Math.round(((double) current) / ((double) total) * 100.0);
long remaining = total - current;
double remainingPercent = 100.0 - currentPercent;

valueBar.setAttribute(aria(VALUE_NOW), String.valueOf(current));
valueBar.setAttribute(aria(VALUE_MAX), String.valueOf(total));
valueBar.style.width = width(currentPercent + "%");
Tooltip.element(valueBar).setTitle(MESSAGES.used(currentPercent));
// noinspection HardCodedStringLiteral
valueElement.innerHTML = new SafeHtmlBuilder()
.appendHtmlConstant("<strong>")
.appendEscaped(MESSAGES.currentOfTotal(current, total))
.appendHtmlConstant("</strong>")
.appendEscaped(" " + unit)
.toSafeHtml().asString();

remainingBar.setAttribute(aria(VALUE_NOW), String.valueOf(remaining));
remainingBar.setAttribute(aria(VALUE_MAX), String.valueOf(total));
remainingBar.style.width = width(remainingPercent + "%");
Tooltip.element(remainingBar).setTitle(MESSAGES.available(remainingPercent));
remainingElement.textContent = MESSAGES.available(remainingPercent);

if (thresholds) {
valueBar.classList.remove(progressBarDanger);
valueBar.classList.remove(progressBarWarning);
valueBar.classList.remove(progressBarSuccess);
if (currentPercent > 90) {
valueBar.classList.add(progressBarDanger);
} else if (currentPercent > 75) {
valueBar.classList.add(progressBarWarning);
} else {
valueBar.classList.add(progressBarSuccess);
}
}

} else {
if (current > total) {
logger.error("Invalid values for utilization bar chart: current > total ({} > {})", current, total);
}
this.total = total;
double currentPercent = Math.round(((double) current) / ((double) total) * 100.0);
long remaining = total - current;
double remainingPercent = 100.0 - currentPercent;

valueBar.setAttribute(aria(VALUE_NOW), String.valueOf(current));
valueBar.setAttribute(aria(VALUE_MAX), String.valueOf(total));
valueBar.style.width = width(currentPercent + "%");
Tooltip.element(valueBar).setTitle(MESSAGES.used(currentPercent));
// noinspection HardCodedStringLiteral
valueElement.innerHTML = new SafeHtmlBuilder()
.appendHtmlConstant("<strong>")
.appendEscaped(MESSAGES.currentOfTotal(current, total))
.appendHtmlConstant("</strong>")
.appendEscaped(" " + unit)
.toSafeHtml().asString();

remainingBar.setAttribute(aria(VALUE_NOW), String.valueOf(remaining));
remainingBar.setAttribute(aria(VALUE_MAX), String.valueOf(total));
remainingBar.style.width = width(remainingPercent + "%");
Tooltip.element(remainingBar).setTitle(MESSAGES.available(remainingPercent));
remainingElement.textContent = MESSAGES.available(remainingPercent);

if (thresholds) {
valueBar.classList.remove(progressBarDanger);
valueBar.classList.remove(progressBarWarning);
valueBar.classList.remove(progressBarSuccess);
if (currentPercent > 90) {
valueBar.classList.add(progressBarDanger);
} else if (currentPercent > 75) {
valueBar.classList.add(progressBarWarning);
} else {
valueBar.classList.add(progressBarSuccess);
}
}
}

private String aria(String name) {
Expand Down

0 comments on commit 0b7d098

Please sign in to comment.