[Logs UI] Limit extendDatemath
to valid ranges (#71113)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
cb4020f3fc
commit
c00359ec96
|
@ -196,6 +196,15 @@ describe('extendDatemath()', () => {
|
||||||
diffUnit: 'y',
|
diffUnit: 'y',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Returns no difference if the next value would result in an epoch smaller than 0', () => {
|
||||||
|
// FIXME: Test will fail in ~551 years
|
||||||
|
expect(extendDatemath('now-500y', 'before')).toBeUndefined();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
extendDatemath('1970-01-01T00:00:00.000Z', 'before', '1970-01-01T00:00:00.001Z')
|
||||||
|
).toBeUndefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with a positive operator', () => {
|
describe('with a positive operator', () => {
|
||||||
|
@ -573,6 +582,13 @@ describe('extendDatemath()', () => {
|
||||||
diffUnit: 'y',
|
diffUnit: 'y',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Returns no difference if the next value would result in an epoch bigger than the max JS date', () => {
|
||||||
|
expect(extendDatemath('now+275760y', 'after')).toBeUndefined();
|
||||||
|
expect(
|
||||||
|
extendDatemath('+275760-09-13T00:00:00.000Z', 'after', '+275760-09-12T23:59:59.999Z')
|
||||||
|
).toBeUndefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
import dateMath, { Unit } from '@elastic/datemath';
|
import dateMath, { Unit } from '@elastic/datemath';
|
||||||
|
|
||||||
|
const JS_MAX_DATE = 8640000000000000;
|
||||||
|
|
||||||
export function isValidDatemath(value: string): boolean {
|
export function isValidDatemath(value: string): boolean {
|
||||||
const parsedValue = dateMath.parse(value);
|
const parsedValue = dateMath.parse(value);
|
||||||
return !!(parsedValue && parsedValue.isValid());
|
return !!(parsedValue && parsedValue.isValid());
|
||||||
|
@ -136,18 +138,24 @@ function extendRelativeDatemath(
|
||||||
// if `diffAmount` is not an integer after normalization, express the difference in the original unit
|
// if `diffAmount` is not an integer after normalization, express the difference in the original unit
|
||||||
const shouldKeepDiffUnit = diffAmount % 1 !== 0;
|
const shouldKeepDiffUnit = diffAmount % 1 !== 0;
|
||||||
|
|
||||||
return {
|
const nextValue = `now${operator}${normalizedAmount}${normalizedUnit}`;
|
||||||
value: `now${operator}${normalizedAmount}${normalizedUnit}`,
|
|
||||||
diffUnit: shouldKeepDiffUnit ? unit : newUnit,
|
if (isDateInRange(nextValue)) {
|
||||||
diffAmount: shouldKeepDiffUnit ? Math.abs(newAmount - parsedAmount) : diffAmount,
|
return {
|
||||||
};
|
value: nextValue,
|
||||||
|
diffUnit: shouldKeepDiffUnit ? unit : newUnit,
|
||||||
|
diffAmount: shouldKeepDiffUnit ? Math.abs(newAmount - parsedAmount) : diffAmount,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function extendAbsoluteDatemath(
|
function extendAbsoluteDatemath(
|
||||||
value: string,
|
value: string,
|
||||||
direction: 'before' | 'after',
|
direction: 'before' | 'after',
|
||||||
oppositeEdge: string
|
oppositeEdge: string
|
||||||
): DatemathExtension {
|
): DatemathExtension | undefined {
|
||||||
const valueTimestamp = datemathToEpochMillis(value)!;
|
const valueTimestamp = datemathToEpochMillis(value)!;
|
||||||
const oppositeEdgeTimestamp = datemathToEpochMillis(oppositeEdge)!;
|
const oppositeEdgeTimestamp = datemathToEpochMillis(oppositeEdge)!;
|
||||||
const actualTimestampDiff = Math.abs(valueTimestamp - oppositeEdgeTimestamp);
|
const actualTimestampDiff = Math.abs(valueTimestamp - oppositeEdgeTimestamp);
|
||||||
|
@ -159,11 +167,15 @@ function extendAbsoluteDatemath(
|
||||||
? valueTimestamp - normalizedTimestampDiff
|
? valueTimestamp - normalizedTimestampDiff
|
||||||
: valueTimestamp + normalizedTimestampDiff;
|
: valueTimestamp + normalizedTimestampDiff;
|
||||||
|
|
||||||
return {
|
if (isDateInRange(newValue)) {
|
||||||
value: new Date(newValue).toISOString(),
|
return {
|
||||||
diffUnit: normalizedDiff.unit,
|
value: new Date(newValue).toISOString(),
|
||||||
diffAmount: normalizedDiff.amount,
|
diffUnit: normalizedDiff.unit,
|
||||||
};
|
diffAmount: normalizedDiff.amount,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const CONVERSION_RATIOS: Record<string, Array<[Unit, number]>> = {
|
const CONVERSION_RATIOS: Record<string, Array<[Unit, number]>> = {
|
||||||
|
@ -265,3 +277,12 @@ export function normalizeDate(amount: number, unit: Unit): { amount: number; uni
|
||||||
// Cannot go one one unit above. Return as it is
|
// Cannot go one one unit above. Return as it is
|
||||||
return { amount, unit };
|
return { amount, unit };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isDateInRange(date: string | number): boolean {
|
||||||
|
try {
|
||||||
|
const epoch = typeof date === 'string' ? datemathToEpochMillis(date) ?? -1 : date;
|
||||||
|
return epoch >= 0 && epoch <= JS_MAX_DATE;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue