mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-10-14 13:51:29 +08:00

* feat: dynamic step size for the data for graphs * fix: remove console.log * chore: add jest globals * feat: add step size for dashboard * chore: undo .eslintignore
47 lines
1.0 KiB
TypeScript
47 lines
1.0 KiB
TypeScript
import dayjs from 'dayjs';
|
|
|
|
type DateType = number | string;
|
|
type DateInputFormatType = 's' | 'ms' | 'ns';
|
|
|
|
interface GetStepInput {
|
|
start: DateType;
|
|
end: DateType;
|
|
inputFormat: DateInputFormatType;
|
|
}
|
|
|
|
/**
|
|
* Converts given timestamp to ms.
|
|
*/
|
|
const convertToMs = (
|
|
timestamp: number,
|
|
inputFormat: DateInputFormatType,
|
|
): number => {
|
|
switch (inputFormat) {
|
|
case 's':
|
|
return timestamp * 1e3;
|
|
case 'ms':
|
|
return timestamp * 1;
|
|
case 'ns':
|
|
return timestamp / 1e6;
|
|
default: {
|
|
throw new Error('invalid format');
|
|
}
|
|
}
|
|
};
|
|
|
|
export const DefaultStepSize = 60;
|
|
export const MaxDataPoints = 200;
|
|
|
|
/**
|
|
* Returns relevant step size based on given start and end date.
|
|
*/
|
|
const getStep = ({ start, end, inputFormat = 'ms' }: GetStepInput): number => {
|
|
const startDate = dayjs(convertToMs(Number(start), inputFormat));
|
|
const endDate = dayjs(convertToMs(Number(end), inputFormat));
|
|
const diffSec = Math.abs(endDate.diff(startDate, 's'));
|
|
|
|
return Math.max(Math.floor(diffSec / MaxDataPoints), DefaultStepSize);
|
|
};
|
|
|
|
export default getStep;
|