2019-05-08 13:51:44 +02:00
|
|
|
import { toUtc, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
|
2017-09-28 12:52:39 +02:00
|
|
|
|
|
|
|
|
const intervalMap = {
|
2017-12-20 12:33:33 +01:00
|
|
|
Hourly: { startOf: 'hour', amount: 'hours' },
|
|
|
|
|
Daily: { startOf: 'day', amount: 'days' },
|
|
|
|
|
Weekly: { startOf: 'isoWeek', amount: 'weeks' },
|
|
|
|
|
Monthly: { startOf: 'month', amount: 'months' },
|
|
|
|
|
Yearly: { startOf: 'year', amount: 'years' },
|
2017-09-28 12:52:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export class IndexPattern {
|
2017-12-19 16:06:54 +01:00
|
|
|
constructor(private pattern, private interval: string | null) {}
|
2017-09-28 12:52:39 +02:00
|
|
|
|
|
|
|
|
getIndexForToday() {
|
|
|
|
|
if (this.interval) {
|
2019-05-08 13:51:44 +02:00
|
|
|
return toUtc().format(this.pattern);
|
2017-09-28 12:52:39 +02:00
|
|
|
} else {
|
|
|
|
|
return this.pattern;
|
|
|
|
|
}
|
2017-10-07 10:31:39 +02:00
|
|
|
}
|
2017-09-28 12:52:39 +02:00
|
|
|
|
|
|
|
|
getIndexList(from, to) {
|
|
|
|
|
if (!this.interval) {
|
|
|
|
|
return this.pattern;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 14:27:29 +02:00
|
|
|
const intervalInfo = intervalMap[this.interval];
|
2019-05-08 13:51:44 +02:00
|
|
|
const start = dateTime(from)
|
2017-12-19 16:06:54 +01:00
|
|
|
.utc()
|
|
|
|
|
.startOf(intervalInfo.startOf);
|
2019-05-08 13:51:44 +02:00
|
|
|
const endEpoch = dateTime(to)
|
2017-12-19 16:06:54 +01:00
|
|
|
.utc()
|
|
|
|
|
.startOf(intervalInfo.startOf)
|
|
|
|
|
.valueOf();
|
2018-08-29 14:27:29 +02:00
|
|
|
const indexList = [];
|
2017-09-28 12:52:39 +02:00
|
|
|
|
2017-10-03 19:38:52 +02:00
|
|
|
while (start.valueOf() <= endEpoch) {
|
2017-09-28 12:52:39 +02:00
|
|
|
indexList.push(start.format(this.pattern));
|
|
|
|
|
start.add(1, intervalInfo.amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return indexList;
|
|
|
|
|
}
|
|
|
|
|
}
|