elasticsearch: support reversed index patterns

Now both [index-]pattern and pattern[-index] are supported
This commit is contained in:
Marcus Efraimsson 2018-07-26 21:39:02 +02:00
parent 7699451d94
commit ab8fa0de74
No known key found for this signature in database
GPG Key ID: EBFE0FB04612DD4A
2 changed files with 53 additions and 9 deletions

View File

@ -248,14 +248,29 @@ var datePatternReplacements = map[string]string{
func formatDate(t time.Time, pattern string) string { func formatDate(t time.Time, pattern string) string {
var datePattern string var datePattern string
base := ""
ltr := false
if strings.HasPrefix(pattern, "[") {
parts := strings.Split(strings.TrimLeft(pattern, "["), "]") parts := strings.Split(strings.TrimLeft(pattern, "["), "]")
base := parts[0] base = parts[0]
if len(parts) == 2 { if len(parts) == 2 {
datePattern = parts[1] datePattern = parts[1]
} else { } else {
datePattern = base datePattern = base
base = "" base = ""
} }
ltr = true
} else if strings.HasSuffix(pattern, "]") {
parts := strings.Split(strings.TrimRight(pattern, "]"), "[")
datePattern = parts[0]
if len(parts) == 2 {
base = parts[1]
} else {
base = ""
}
ltr = false
}
formatted := t.Format(patternToLayout(datePattern)) formatted := t.Format(patternToLayout(datePattern))
@ -293,9 +308,13 @@ func formatDate(t time.Time, pattern string) string {
formatted = strings.Replace(formatted, "<stdHourNoZero>", fmt.Sprintf("%d", t.Hour()), -1) formatted = strings.Replace(formatted, "<stdHourNoZero>", fmt.Sprintf("%d", t.Hour()), -1)
} }
if ltr {
return base + formatted return base + formatted
} }
return formatted + base
}
func patternToLayout(pattern string) string { func patternToLayout(pattern string) string {
var match [][]string var match [][]string
if match = datePatternRegex.FindAllStringSubmatch(pattern, -1); match == nil { if match = datePatternRegex.FindAllStringSubmatch(pattern, -1); match == nil {

View File

@ -28,29 +28,54 @@ func TestIndexPattern(t *testing.T) {
to := fmt.Sprintf("%d", time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond)) to := fmt.Sprintf("%d", time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
indexPatternScenario(intervalHourly, "[data-]YYYY.MM.DD.HH", tsdb.NewTimeRange(from, to), func(indices []string) { indexPatternScenario(intervalHourly, "[data-]YYYY.MM.DD.HH", tsdb.NewTimeRange(from, to), func(indices []string) {
//So(indices, ShouldHaveLength, 1) So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "data-2018.05.15.17") So(indices[0], ShouldEqual, "data-2018.05.15.17")
}) })
indexPatternScenario(intervalHourly, "YYYY.MM.DD.HH[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "2018.05.15.17-data")
})
indexPatternScenario(intervalDaily, "[data-]YYYY.MM.DD", tsdb.NewTimeRange(from, to), func(indices []string) { indexPatternScenario(intervalDaily, "[data-]YYYY.MM.DD", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1) So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "data-2018.05.15") So(indices[0], ShouldEqual, "data-2018.05.15")
}) })
indexPatternScenario(intervalDaily, "YYYY.MM.DD[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "2018.05.15-data")
})
indexPatternScenario(intervalWeekly, "[data-]GGGG.WW", tsdb.NewTimeRange(from, to), func(indices []string) { indexPatternScenario(intervalWeekly, "[data-]GGGG.WW", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1) So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "data-2018.20") So(indices[0], ShouldEqual, "data-2018.20")
}) })
indexPatternScenario(intervalWeekly, "GGGG.WW[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "2018.20-data")
})
indexPatternScenario(intervalMonthly, "[data-]YYYY.MM", tsdb.NewTimeRange(from, to), func(indices []string) { indexPatternScenario(intervalMonthly, "[data-]YYYY.MM", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1) So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "data-2018.05") So(indices[0], ShouldEqual, "data-2018.05")
}) })
indexPatternScenario(intervalMonthly, "YYYY.MM[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "2018.05-data")
})
indexPatternScenario(intervalYearly, "[data-]YYYY", tsdb.NewTimeRange(from, to), func(indices []string) { indexPatternScenario(intervalYearly, "[data-]YYYY", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1) So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "data-2018") So(indices[0], ShouldEqual, "data-2018")
}) })
indexPatternScenario(intervalYearly, "YYYY[-data]", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "2018-data")
})
}) })
Convey("Hourly interval", t, func() { Convey("Hourly interval", t, func() {