Skip to content

Commit

Permalink
Merge pull request #105077 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-22.2-105051

release-22.2: ui: add more precision to small percentages
  • Loading branch information
maryliag authored Jun 16, 2023
2 parents 2b49a9f + 561279e commit 8055184
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/ui/workspaces/cluster-ui/src/barCharts/barCharts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import * as protos from "@cockroachlabs/crdb-protobuf-client";
import { stdDevLong, longToInt } from "src/util";
import { Duration, Bytes, Percentage } from "src/util/format";
import { Duration, Bytes, PercentageCustom } from "src/util/format";
import classNames from "classnames/bind";
import styles from "./barCharts.module.scss";
import { bar, approximify } from "./utils";
Expand Down Expand Up @@ -140,6 +140,6 @@ export function workloadPctBarChart(
: 0,
),
],
v => Percentage(v, 1, 1),
v => PercentageCustom(v, 1, 1),
)(statements, defaultBarChartOptions);
}
16 changes: 16 additions & 0 deletions pkg/ui/workspaces/cluster-ui/src/util/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
HexStringToInt64String,
FixFingerprintHexValue,
EncodeUriName,
PercentageCustom,
} from "./format";

describe("Format utils", () => {
Expand Down Expand Up @@ -89,4 +90,19 @@ describe("Format utils", () => {
expect(EncodeUriName("12%abc")).toBe("12%252525abc");
});
});

describe("PercentageCustom", () => {
it("percentages bigger than 1", () => {
assert.equal(PercentageCustom(1, 1, 1), "100.0 %");
assert.equal(PercentageCustom(0.1234, 1, 1), "12.3 %");
assert.equal(PercentageCustom(0.23432, 1, 1), "23.4 %");
assert.equal(PercentageCustom(0.23432, 1, 2), "23.43 %");
});
it("percentages between 0 and 1", () => {
assert.equal(PercentageCustom(0, 1, 1), "0.0 %");
assert.equal(PercentageCustom(0.00023, 1, 1), "0.02 %");
assert.equal(PercentageCustom(0.0000023, 1, 1), "0.0002 %");
assert.equal(PercentageCustom(0.00000000000000004, 1, 1), "~0.0 %");
});
});
});
35 changes: 35 additions & 0 deletions pkg/ui/workspaces/cluster-ui/src/util/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ export function Percentage(
return Math.floor((numerator / denominator) * 100).toString() + " %";
}

/**
* PercentageCustom creates a string representation of a fraction as a percentage.
* Accepts a precision parameter as optional indicating how many digits
* after the decimal point are desired. (e.g. precision 2 returns 8.37 %)
* If the number is zero or grater than 1, it works the same way as the Percentage
* function above, if is between 0 and 1, it looks for the first non-zero
* decimal number, up to 10 decimal points, otherwise shows ~0.0%
*/
export function PercentageCustom(
numerator: number,
denominator: number,
precision?: number,
): string {
if (denominator === 0) {
return "--%";
}
const pct = (numerator / denominator) * 100;
if (pct <= 0 || pct >= 1) {
return Percentage(numerator, denominator, precision);
}
const pctString = pct.toFixed(10);
let finalPct = "0.";
let found = false;
for (let index = 2; index < pctString.length && !found; index++) {
finalPct = `${finalPct}${pctString[index]}`;
if (pctString[index] != "0") {
found = true;
}
}
if (found) {
return finalPct + " %";
}
return "~0.0 %";
}

/**
* ComputeDurationScale calculates an appropriate scale factor and unit to use
* to display a given duration value, without actually converting the value.
Expand Down

0 comments on commit 8055184

Please sign in to comment.