Extracted util functions
This commit is contained in:
1 parent
9f95d3d4bf
commit
bb5589a255
1 file changed
+2
-47
@@ -1,55 +1,10 @@
|
|||||||
import "./index.css";
|
import "./index.css";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import { BarChart, Bar, ResponsiveContainer, Cell } from "recharts";
|
import { BarChart, Bar, ResponsiveContainer, Cell } from "recharts";
|
||||||
|
import { NormalizeData } from "../ChartUtils";
|
||||||
const RANGE_MAX = 100;
|
|
||||||
const RANGE_MIN = 1;
|
|
||||||
|
|
||||||
const calculatePercentile = (arr, percentile) => {
|
|
||||||
const sorted = arr.slice().sort((a, b) => a.responseTime - b.responseTime);
|
|
||||||
const index = (percentile / 100) * (sorted.length - 1);
|
|
||||||
const lower = Math.floor(index);
|
|
||||||
const upper = lower + 1;
|
|
||||||
const weight = index % 1;
|
|
||||||
if (upper >= sorted.length) return sorted[lower].responseTime;
|
|
||||||
return (
|
|
||||||
sorted[lower].responseTime * (1 - weight) +
|
|
||||||
sorted[upper].responseTime * weight
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const normalizeData = (checks) => {
|
|
||||||
if (checks.length > 1) {
|
|
||||||
// Get the 5th and 95th percentile
|
|
||||||
const min = calculatePercentile(checks, 5);
|
|
||||||
const max = calculatePercentile(checks, 95);
|
|
||||||
|
|
||||||
const normalizedChecks = checks.map((check) => {
|
|
||||||
// Normalize the response time between 1 and 100
|
|
||||||
let normalizedResponseTime =
|
|
||||||
RANGE_MIN +
|
|
||||||
((check.responseTime - min) * (RANGE_MAX - RANGE_MIN)) / (max - min);
|
|
||||||
|
|
||||||
// Put a floor on the response times so we don't have extreme outliers
|
|
||||||
// Better visuals
|
|
||||||
normalizedResponseTime = Math.max(
|
|
||||||
RANGE_MIN,
|
|
||||||
Math.min(RANGE_MAX, normalizedResponseTime)
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
...check,
|
|
||||||
responseTime: normalizedResponseTime,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return normalizedChecks;
|
|
||||||
} else {
|
|
||||||
return checks;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const ResponseTimeChart = ({ checks = [] }) => {
|
const ResponseTimeChart = ({ checks = [] }) => {
|
||||||
const normalizedChecks = normalizeData(checks);
|
const normalizedChecks = NormalizeData(checks);
|
||||||
return (
|
return (
|
||||||
<div className="chart-container">
|
<div className="chart-container">
|
||||||
<ResponsiveContainer width="100%" height="100%">
|
<ResponsiveContainer width="100%" height="100%">
|
||||||
|
|||||||
Reference in new issue
Block a user