Skip to content

Commit

Permalink
Refactor XAxis precision methods for improved date rounding and remov…
Browse files Browse the repository at this point in the history
…e random sorting method from QueryHelper
  • Loading branch information
simlarsen committed Nov 26, 2024
1 parent 002c23b commit ab5acde
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 24 deletions.
5 changes: 0 additions & 5 deletions Common/Server/Types/Database/QueryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import { FindWhereProperty } from "../../../Types/BaseDatabase/Query";

export default class QueryHelper {

public static sortByRandom(): FindWhereProperty<any> {
return Raw(() => {
return "RANDOM()";
});
}

public static findWithSameText(
text: string | number,
Expand Down
184 changes: 166 additions & 18 deletions Common/UI/Components/Charts/Utils/XAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ export default class XAxisUtil {
if (totalMinutes <= 3000) {
return XAxisPrecision.EVERY_THIRTY_MINUTES;
}
if (totalHours <= 100) {
if (totalHours <= 80) {
return XAxisPrecision.EVERY_HOUR;
}
if (totalHours <= 200) {
if (totalHours <= 100) {
return XAxisPrecision.EVERY_TWO_HOURS;
}
if (totalHours <= 300) {
if (totalHours <= 150) {
return XAxisPrecision.EVERY_THREE_HOURS;
}
if (totalHours <= 600) {
if (totalHours <= 300) {
return XAxisPrecision.EVERY_SIX_HOURS;
}
if (totalHours <= 1200) {
Expand Down Expand Up @@ -196,70 +196,218 @@ export default class XAxisUtil {

switch (precision) {
case XAxisPrecision.EVERY_SECOND:
return (value: Date) => {
// round down to nearest second
return value.toISOString().substring(11, 19);
};
case XAxisPrecision.EVERY_FIVE_SECONDS:
// round down to nearest 5 seconds
return (value: Date) => {
const seconds: number = value.getSeconds();
const roundedSeconds: number = Math.floor(seconds / 5) * 5;
value.setSeconds(roundedSeconds);
return value.toISOString().substring(11, 19);
};
case XAxisPrecision.EVERY_TEN_SECONDS:
// round down to nearest 10 seconds
return (value: Date) => {
const seconds: number = value.getSeconds();
const roundedSeconds: number = Math.floor(seconds / 10) * 10;
value.setSeconds(roundedSeconds);
return value.toISOString().substring(11, 19);
};
case XAxisPrecision.EVERY_THIRTY_SECONDS:
// round down to nearest 30 seconds
return (value: Date) => {
const seconds: number = value.getSeconds();
const roundedSeconds: number = Math.floor(seconds / 30) * 30;
value.setSeconds(roundedSeconds);
return value.toISOString().substring(11, 19);
}; // HH:mm:ss
};
case XAxisPrecision.EVERY_MINUTE:
// round down to nearest minute
return (value: Date) => {
return value.toISOString().substring(11, 16);
};
case XAxisPrecision.EVERY_FIVE_MINUTES:
// round down to nearest 5 minutes
return (value: Date) => {
const minutes: number = value.getMinutes();
const roundedMinutes: number = Math.floor(minutes / 5) * 5;
value.setMinutes(roundedMinutes);
return value.toISOString().substring(11, 16);
};
case XAxisPrecision.EVERY_TEN_MINUTES:
// round down to nearest 10 minutes
return (value: Date) => {
const minutes: number = value.getMinutes();
const roundedMinutes: number = Math.floor(minutes / 10) * 10;
value.setMinutes(roundedMinutes);
return value.toISOString().substring(11, 16);
};
case XAxisPrecision.EVERY_THIRTY_MINUTES:
// round down to nearest 30 minutes
return (value: Date) => {
const minutes: number = value.getMinutes();
const roundedMinutes: number = Math.floor(minutes / 30) * 30;
value.setMinutes(roundedMinutes);
return value.toISOString().substring(11, 16);
}; // HH:mm
};
case XAxisPrecision.EVERY_HOUR:
return (value: Date) => {
return value.toISOString().substring(11, 13);
}; // HH:00
case XAxisPrecision.EVERY_TWO_HOURS:
return (value: Date) => {
const hours: number = value.getHours();
const roundedHours: number = Math.floor(hours / 2) * 2;
value.setHours(roundedHours);

const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}, ${value.toISOString().substring(11, 13)}:00`;
};
case XAxisPrecision.EVERY_THREE_HOURS:
// round down to nearest 3 hours
return (value: Date) => {
const hours: number = value.getHours();
const roundedHours: number = Math.floor(hours / 3) * 3;
value.setHours(roundedHours);

const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}, ${value.toISOString().substring(11, 13)}:00`;
};
case XAxisPrecision.EVERY_SIX_HOURS:
// round down to nearest 6 hours // HH:00 DD MMM
return (value: Date) => {
const hours: number = value.getHours();
const roundedHours: number = Math.floor(hours / 6) * 6;
value.setHours(roundedHours);

const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}, ${value.toISOString().substring(11, 13)}:00`;
};
case XAxisPrecision.EVERY_TWELVE_HOURS:
// round down to nearest 12 hours // DD MMM, HH:00
return (value: Date) => {
const hours: number = value.getHours();
const roundedHours: number = Math.floor(hours / 12) * 12;
value.setHours(roundedHours);

const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const month: string = value.toLocaleString("default", {
month: "short",
});
const hour: string = dateString.substring(11, 13);
return `${day} ${month}, ${hour}:00`;
}; // DD MMM, HH:00
};
case XAxisPrecision.EVERY_DAY:
// round down to nearest day
return (value: Date) => {
const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}`;
};
case XAxisPrecision.EVERY_TWO_DAYS:
// round down to nearest 2 days
return (value: Date) => {
const days: number = value.getDate();
const roundedDays: number = Math.floor(days / 2) * 2;
value.setDate(roundedDays);
const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}`;
}; // DD MMM
};
case XAxisPrecision.EVERY_WEEK:
// round down to nearest week
return (value: Date) => {
const day: string = value.getDate().toString();
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}`;
};
case XAxisPrecision.EVERY_TWO_WEEKS:
// round down to nearest 2 weeks. // DD MMM
return (value: Date) => {
const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const days: number = value.getDate();
const roundedDays: number = Math.floor(days / 2) * 2;
value.setDate(roundedDays);
const day: string = value.getDate().toString();
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month}`;
}; // DD MMM
};
case XAxisPrecision.EVERY_MONTH:
// round down to nearest month // MM YYYY
return (value: Date) => {
const month: string = value.toLocaleString("default", {
month: "short",
});
const year: string = value.getFullYear().toString();
return `${month} ${year}`;
};

case XAxisPrecision.EVERY_TWO_MONTHS:
// round down to nearest 2 months // MM YYYY
return (value: Date) => {
const months: number = value.getMonth();
const roundedMonths: number = Math.floor(months / 2) * 2;
value.setMonth(roundedMonths);
const month: string = value.toLocaleString("default", {
month: "short",
});
const year: string = value.getFullYear().toString();
return `${month} ${year}`;
};
case XAxisPrecision.EVERY_THREE_MONTHS:
// round down to nearest 3 months // MM YYYY
return (value: Date) => {
const months: number = value.getMonth();
const roundedMonths: number = Math.floor(months / 3) * 3;
value.setMonth(roundedMonths);
const month: string = value.toLocaleString("default", {
month: "short",
});
const year: string = value.getFullYear().toString();
return `${month} ${year}`;
};
case XAxisPrecision.EVERY_SIX_MONTHS:
// round down to nearest 6 months // MM YYYY
return (value: Date) => {
const dateString: string = value.toISOString();
const day: string = dateString.substring(8, 10);
const year: string = dateString.substring(0, 4);
const months: number = value.getMonth();
const roundedMonths: number = Math.floor(months / 6) * 6;
value.setMonth(roundedMonths);
const month: string = value.toLocaleString("default", {
month: "short",
});
return `${day} ${month} ${year}`;
}; // DD MMM
const year: string = value.getFullYear().toString();
return `${month} ${year}`;
};
case XAxisPrecision.EVERY_YEAR:
// round down to nearest year // YYYY
return (value: Date) => {
return value.toISOString().substring(0, 4);
}; // YYYY
return value.getFullYear().toString();
};
default:
throw new Error("Unsupported precision");
}
Expand Down
2 changes: 1 addition & 1 deletion ProbeIngest/API/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ router.post(
await MonitorProbeService.findBy({
query: getMonitorFetchQuery((req as OneUptimeRequest).probe!.id!),
sort: {
nextPingAt: QueryHelper.sortByRandom(), // randomize the order of the monitors
nextPingAt: SortOrder.Ascending,
} as any,
skip: 0,
limit: limit,
Expand Down

0 comments on commit ab5acde

Please sign in to comment.