Elasticsearch: revert to isoWeek when resolving weekly indices (#31709)
* Elasticsearch: revert to isoWeek when resolving weekly indices * Add type assertion explainer
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { TimeZone } from '../types/time';
|
import { TimeZone } from '../types/time';
|
||||||
/* eslint-disable id-blacklist, no-restricted-imports, @typescript-eslint/ban-types */
|
/* eslint-disable id-blacklist, no-restricted-imports, @typescript-eslint/ban-types */
|
||||||
import moment, { Moment, MomentInput, DurationInputArg1 } from 'moment';
|
import moment, { Moment, MomentInput, DurationInputArg1, DurationInputArg2 } from 'moment';
|
||||||
export interface DateTimeBuiltinFormat {
|
export interface DateTimeBuiltinFormat {
|
||||||
__momentBuiltinFormatBrand: any;
|
__momentBuiltinFormatBrand: any;
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ export type DurationUnit =
|
|||||||
| 'M'
|
| 'M'
|
||||||
| 'week'
|
| 'week'
|
||||||
| 'weeks'
|
| 'weeks'
|
||||||
|
| 'isoWeek'
|
||||||
| 'w'
|
| 'w'
|
||||||
| 'day'
|
| 'day'
|
||||||
| 'days'
|
| 'days'
|
||||||
@@ -96,7 +97,8 @@ export const toUtc = (input?: DateTimeInput, formatInput?: FormatInput): DateTim
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const toDuration = (input?: DurationInput, unit?: DurationUnit): DateTimeDuration => {
|
export const toDuration = (input?: DurationInput, unit?: DurationUnit): DateTimeDuration => {
|
||||||
return moment.duration(input as DurationInputArg1, unit) as DateTimeDuration;
|
// moment built-in types are a bit flaky, for example `isoWeek` is not in the type definition but it's present in the js source.
|
||||||
|
return moment.duration(input as DurationInputArg1, unit as DurationInputArg2) as DateTimeDuration;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const dateTime = (input?: DateTimeInput, formatInput?: FormatInput): DateTime => {
|
export const dateTime = (input?: DateTimeInput, formatInput?: FormatInput): DateTime => {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ type IntervalMap = Record<
|
|||||||
const intervalMap: IntervalMap = {
|
const intervalMap: IntervalMap = {
|
||||||
Hourly: { startOf: 'hour', amount: 'hours' },
|
Hourly: { startOf: 'hour', amount: 'hours' },
|
||||||
Daily: { startOf: 'day', amount: 'days' },
|
Daily: { startOf: 'day', amount: 'days' },
|
||||||
Weekly: { startOf: 'week', amount: 'weeks' },
|
Weekly: { startOf: 'isoWeek', amount: 'weeks' },
|
||||||
Monthly: { startOf: 'month', amount: 'months' },
|
Monthly: { startOf: 'month', amount: 'months' },
|
||||||
Yearly: { startOf: 'year', amount: 'years' },
|
Yearly: { startOf: 'year', amount: 'years' },
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,6 +55,20 @@ describe('IndexPattern', () => {
|
|||||||
expect(pattern.getIndexList(from, to)).toEqual(expected);
|
expect(pattern.getIndexList(from, to)).toEqual(expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('weekly', () => {
|
||||||
|
it('should return correct index list', () => {
|
||||||
|
const pattern = new IndexPattern('[asd-]YYYY.WW', 'Weekly');
|
||||||
|
// Sunday, February 21, 2021 1:00:00 AM
|
||||||
|
const from = dateTime(new Date(1613869200000));
|
||||||
|
// Friday, March 5, 2021 1:00:00 AM
|
||||||
|
const to = dateTime(new Date(1614906000000));
|
||||||
|
|
||||||
|
const expected = ['asd-2021.07', 'asd-2021.08', 'asd-2021.09'];
|
||||||
|
|
||||||
|
expect(pattern.getIndexList(from, to)).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when getting index list from single date', () => {
|
describe('when getting index list from single date', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user