mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 04:59:15 -06:00
3f15170914
* moving to data WIP * more refactoring * add missing test * mock full path * remove sinon from grafana-ui
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { TimeRange, toUtc, AbsoluteTimeRange } from '@grafana/data';
|
|
|
|
export const getShiftedTimeRange = (direction: number, origRange: TimeRange): AbsoluteTimeRange => {
|
|
const range = {
|
|
from: toUtc(origRange.from),
|
|
to: toUtc(origRange.to),
|
|
};
|
|
|
|
const timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
|
|
let to: number, from: number;
|
|
|
|
if (direction === -1) {
|
|
to = range.to.valueOf() - timespan;
|
|
from = range.from.valueOf() - timespan;
|
|
} else if (direction === 1) {
|
|
to = range.to.valueOf() + timespan;
|
|
from = range.from.valueOf() + timespan;
|
|
if (to > Date.now() && range.to.valueOf() < Date.now()) {
|
|
to = Date.now();
|
|
from = range.from.valueOf();
|
|
}
|
|
} else {
|
|
to = range.to.valueOf();
|
|
from = range.from.valueOf();
|
|
}
|
|
|
|
return { from, to };
|
|
};
|
|
|
|
export const getZoomedTimeRange = (range: TimeRange, factor: number): AbsoluteTimeRange => {
|
|
const timespan = range.to.valueOf() - range.from.valueOf();
|
|
const center = range.to.valueOf() - timespan / 2;
|
|
|
|
const to = center + (timespan * factor) / 2;
|
|
const from = center - (timespan * factor) / 2;
|
|
|
|
return { from, to };
|
|
};
|