Moved normalization to BE
This commit is contained in:
1 parent
7928bd7822
commit
51058b6d2b
8 files changed
+28
-18
No files matched your search
@@ -1,6 +1,5 @@
|
|||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import { AreaChart, Area, XAxis, Tooltip, ResponsiveContainer } from "recharts";
|
import { AreaChart, Area, XAxis, Tooltip, ResponsiveContainer } from "recharts";
|
||||||
import { NormalizeData } from "../ChartUtils";
|
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
const CustomToolTip = ({ active, payload, label }) => {
|
const CustomToolTip = ({ active, payload, label }) => {
|
||||||
@@ -47,17 +46,12 @@ const MonitorDetailsAreaChart = ({ checks, filter }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let normalizedChecks = [];
|
|
||||||
if (checks && checks.length > 0) {
|
|
||||||
normalizedChecks = NormalizeData(checks, 10, 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ResponsiveContainer width="100%" height="100%">
|
<ResponsiveContainer width="100%" height="100%">
|
||||||
<AreaChart
|
<AreaChart
|
||||||
width={500}
|
width={500}
|
||||||
height={400}
|
height={400}
|
||||||
data={normalizedChecks}
|
data={checks}
|
||||||
margin={{
|
margin={{
|
||||||
top: 10,
|
top: 10,
|
||||||
right: 0,
|
right: 0,
|
||||||
|
|||||||
@@ -1,21 +1,19 @@
|
|||||||
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 ResponseTimeChart = ({ checks = [] }) => {
|
const ResponseTimeChart = ({ checks = [] }) => {
|
||||||
const normalizedChecks = NormalizeData(checks, 1, 100);
|
|
||||||
return (
|
return (
|
||||||
<div className="chart-container">
|
<div className="chart-container">
|
||||||
<ResponsiveContainer width="100%" height="100%">
|
<ResponsiveContainer width="100%" height="100%">
|
||||||
<BarChart
|
<BarChart
|
||||||
width={150}
|
width={150}
|
||||||
height={40}
|
height={40}
|
||||||
data={normalizedChecks}
|
data={checks}
|
||||||
style={{ cursor: "pointer" }}
|
style={{ cursor: "pointer" }}
|
||||||
>
|
>
|
||||||
<Bar maxBarSize={10} dataKey="responseTime">
|
<Bar maxBarSize={10} dataKey="responseTime">
|
||||||
{normalizedChecks.map((check, index) => (
|
{checks.map((check, index) => (
|
||||||
<Cell
|
<Cell
|
||||||
key={`cell-${index}`}
|
key={`cell-${index}`}
|
||||||
fill={
|
fill={
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ export const getUptimeMonitorsByUserId = createAsyncThunk(
|
|||||||
const user = jwtDecode(token);
|
const user = jwtDecode(token);
|
||||||
try {
|
try {
|
||||||
const res = await axiosInstance.get(
|
const res = await axiosInstance.get(
|
||||||
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc`,
|
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc&normalize=true`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ const DetailsPage = () => {
|
|||||||
const fetchMonitor = useCallback(async () => {
|
const fetchMonitor = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const res = await axiosInstance.get(
|
const res = await axiosInstance.get(
|
||||||
`/monitors/${monitorId}?sortOrder=asc&filter=${filter}&numToDisplay=50`,
|
`/monitors/${monitorId}?sortOrder=asc&filter=${filter}&numToDisplay=50&normalize=true`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${authToken}`,
|
Authorization: `Bearer ${authToken}`,
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ const Monitors = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch(getUptimeMonitorsByUserId(authState.authToken));
|
dispatch(getUptimeMonitorsByUserId(authState.authToken));
|
||||||
}, []);
|
}, [authState.authToken, dispatch]);
|
||||||
|
|
||||||
const up = monitorState.monitors.reduce((acc, cur) => {
|
const up = monitorState.monitors.reduce((acc, cur) => {
|
||||||
return cur.status === true ? acc + 1 : acc;
|
return cur.status === true ? acc + 1 : acc;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ const Check = require("../../../models/Check");
|
|||||||
const PageSpeedCheck = require("../../../models/PageSpeedCheck");
|
const PageSpeedCheck = require("../../../models/PageSpeedCheck");
|
||||||
const { errorMessages } = require("../../../utils/messages");
|
const { errorMessages } = require("../../../utils/messages");
|
||||||
const Notification = require("../../../models/Notification");
|
const Notification = require("../../../models/Notification");
|
||||||
|
const { NormalizeData } = require("../../../utils/dataUtils");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all monitors
|
* Get all monitors
|
||||||
@@ -32,7 +33,8 @@ const getAllMonitors = async (req, res) => {
|
|||||||
const getMonitorById = async (req, res) => {
|
const getMonitorById = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { monitorId } = req.params;
|
const { monitorId } = req.params;
|
||||||
let { status, limit, sortOrder, filter, numToDisplay } = req.query;
|
let { status, limit, sortOrder, filter, numToDisplay, normalize } =
|
||||||
|
req.query;
|
||||||
|
|
||||||
const filterLookup = {
|
const filterLookup = {
|
||||||
day: new Date(new Date().setDate(new Date().getDate() - 1)),
|
day: new Date(new Date().setDate(new Date().getDate() - 1)),
|
||||||
@@ -82,6 +84,11 @@ const getMonitorById = async (req, res) => {
|
|||||||
checks = checks.filter((_, index) => index % n === 0);
|
checks = checks.filter((_, index) => index % n === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize checks if requested
|
||||||
|
if (normalize) {
|
||||||
|
checks = NormalizeData(checks, 10, 100);
|
||||||
|
}
|
||||||
|
|
||||||
const notifications = await Notification.find({ monitorId: monitor._id });
|
const notifications = await Notification.find({ monitorId: monitor._id });
|
||||||
const monitorWithChecks = { ...monitor.toObject(), checks, notifications };
|
const monitorWithChecks = { ...monitor.toObject(), checks, notifications };
|
||||||
return monitorWithChecks;
|
return monitorWithChecks;
|
||||||
@@ -100,7 +107,7 @@ const getMonitorById = async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
const getMonitorsByUserId = async (req, res) => {
|
const getMonitorsByUserId = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
let { limit, type, status, sortOrder } = req.query;
|
let { limit, type, status, sortOrder, normalize } = req.query;
|
||||||
const monitorQuery = { userId: req.params.userId };
|
const monitorQuery = { userId: req.params.userId };
|
||||||
|
|
||||||
if (type !== undefined) {
|
if (type !== undefined) {
|
||||||
@@ -133,13 +140,18 @@ const getMonitorsByUserId = async (req, res) => {
|
|||||||
: PageSpeedCheck;
|
: PageSpeedCheck;
|
||||||
|
|
||||||
// Checks are order newest -> oldest
|
// Checks are order newest -> oldest
|
||||||
const checks = await model
|
let checks = await model
|
||||||
.find(checksQuery)
|
.find(checksQuery)
|
||||||
.sort({
|
.sort({
|
||||||
createdAt: sortOrder,
|
createdAt: sortOrder,
|
||||||
})
|
})
|
||||||
.limit(limit);
|
.limit(limit);
|
||||||
|
|
||||||
|
//Normalize checks if requested
|
||||||
|
if (normalize === true) {
|
||||||
|
checks = NormalizeData(checks, 10, 100);
|
||||||
|
}
|
||||||
|
|
||||||
// Get notifications
|
// Get notifications
|
||||||
const notifications = await Notification.find({
|
const notifications = await Notification.find({
|
||||||
monitorId: monitor._id,
|
monitorId: monitor._id,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const calculatePercentile = (arr, percentile) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NormalizeData = (checks, rangeMin, rangeMax) => {
|
const NormalizeData = (checks, rangeMin, rangeMax) => {
|
||||||
if (checks.length > 1) {
|
if (checks.length > 1) {
|
||||||
// Get the 5th and 95th percentile
|
// Get the 5th and 95th percentile
|
||||||
const min = calculatePercentile(checks, 0);
|
const min = calculatePercentile(checks, 0);
|
||||||
@@ -44,3 +44,7 @@ export const NormalizeData = (checks, rangeMin, rangeMax) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
NormalizeData,
|
||||||
|
};
|
||||||
@@ -128,6 +128,7 @@ const getMonitorByIdQueryValidation = joi.object({
|
|||||||
limit: joi.number(),
|
limit: joi.number(),
|
||||||
filter: joi.string().valid("day", "week", "month"),
|
filter: joi.string().valid("day", "week", "month"),
|
||||||
numToDisplay: joi.number(),
|
numToDisplay: joi.number(),
|
||||||
|
normalize: joi.boolean(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const getMonitorsByUserIdValidation = joi.object({
|
const getMonitorsByUserIdValidation = joi.object({
|
||||||
@@ -138,6 +139,7 @@ const getMonitorsByUserIdQueryValidation = joi.object({
|
|||||||
status: joi.boolean(),
|
status: joi.boolean(),
|
||||||
sortOrder: joi.string().valid("asc", "desc"),
|
sortOrder: joi.string().valid("asc", "desc"),
|
||||||
limit: joi.number(),
|
limit: joi.number(),
|
||||||
|
normalize: joi.boolean(),
|
||||||
type: joi
|
type: joi
|
||||||
.alternatives()
|
.alternatives()
|
||||||
.try(
|
.try(
|
||||||
|
|||||||
Reference in new issue
Block a user