signoz/frontend/src/lib/getStep.ts
Pranshu Chittora 566c2becdf
feat: dynamic step size for the data for graphs (#929)
* 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
2022-04-05 16:09:57 +05:30

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;