Commit ee3475d9 by Tom Daly Committed by GitHub

Elasticsearch: Fix index pattern not working with multiple base sections (#28348)

* Add logic to allow for multiple [] sections in pattern

* Small refactor

* Fix goimport error

* Refactor formateDate
parent c22a39ab
......@@ -247,54 +247,51 @@ var datePatternReplacements = map[string]string{
}
func formatDate(t time.Time, pattern string) string {
var datePattern string
var formattedDatePatterns []string
var bases []string
base := ""
ltr := false
isBaseFirst := false
if strings.HasPrefix(pattern, "[") {
parts := strings.Split(strings.TrimLeft(pattern, "["), "]")
base = parts[0]
if len(parts) == 2 {
datePattern = parts[1]
} else {
datePattern = 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]
baseStart := strings.Index(pattern, "[")
for baseStart != -1 {
var datePattern string
baseEnd := strings.Index(pattern, "]")
base = pattern[baseStart+1 : baseEnd]
bases = append(bases, base)
if baseStart == 0 {
isBaseFirst = true
} else {
base = ""
datePattern = pattern[:baseStart]
formatted := t.Format(patternToLayout(datePattern))
formattedDatePatterns = append(formattedDatePatterns, formatted)
}
ltr = false
if len(pattern) <= baseEnd+1 {
break
}
pattern = pattern[baseEnd+1:]
baseStart = strings.Index(pattern, "[")
if baseStart == -1 {
datePattern = pattern
} else {
datePattern = pattern[:baseStart]
pattern = pattern[baseStart:]
baseStart = 0
}
formatted := t.Format(patternToLayout(datePattern))
formattedDatePatterns = append(formattedDatePatterns, formatted)
}
if strings.Contains(formatted, "<std") {
isoYear, isoWeek := t.ISOWeek()
isoYearShort := fmt.Sprintf("%d", isoYear)[2:4]
formatted = strings.ReplaceAll(formatted, "<stdIsoYear>", fmt.Sprintf("%d", isoYear))
formatted = strings.ReplaceAll(formatted, "<stdIsoYearShort>", isoYearShort)
formatted = strings.ReplaceAll(formatted, "<stdWeekOfYear>", fmt.Sprintf("%02d", isoWeek))
formatted = strings.ReplaceAll(formatted, "<stdUnix>", fmt.Sprintf("%d", t.Unix()))
day := t.Weekday()
dayOfWeekIso := int(day)
if day == time.Sunday {
dayOfWeekIso = 7
}
formatted = strings.ReplaceAll(formatted, "<stdDayOfWeek>", fmt.Sprintf("%d", day))
formatted = strings.ReplaceAll(formatted, "<stdDayOfWeekISO>", fmt.Sprintf("%d", dayOfWeekIso))
formatted = strings.ReplaceAll(formatted, "<stdDayOfYear>", fmt.Sprintf("%d", t.YearDay()))
quarter := 4
switch t.Month() {
case time.January, time.February, time.March:
quarter = 1
......@@ -304,15 +301,43 @@ func formatDate(t time.Time, pattern string) string {
quarter = 3
}
for i, formatted := range formattedDatePatterns {
if !strings.Contains(formatted, "<std") {
continue
}
formatted = strings.ReplaceAll(formatted, "<stdIsoYear>", fmt.Sprintf("%d", isoYear))
formatted = strings.ReplaceAll(formatted, "<stdIsoYearShort>", isoYearShort)
formatted = strings.ReplaceAll(formatted, "<stdWeekOfYear>", fmt.Sprintf("%02d", isoWeek))
formatted = strings.ReplaceAll(formatted, "<stdUnix>", fmt.Sprintf("%d", t.Unix()))
formatted = strings.ReplaceAll(formatted, "<stdDayOfWeek>", fmt.Sprintf("%d", day))
formatted = strings.ReplaceAll(formatted, "<stdDayOfWeekISO>", fmt.Sprintf("%d", dayOfWeekIso))
formatted = strings.ReplaceAll(formatted, "<stdDayOfYear>", fmt.Sprintf("%d", t.YearDay()))
formatted = strings.ReplaceAll(formatted, "<stdQuarter>", fmt.Sprintf("%d", quarter))
formatted = strings.ReplaceAll(formatted, "<stdHourNoZero>", fmt.Sprintf("%d", t.Hour()))
formattedDatePatterns[i] = formatted
}
if ltr {
return base + formatted
var fullPattern []string
var i int
minLen := min(len(formattedDatePatterns), len(bases))
for i = 0; i < minLen; i++ {
if isBaseFirst {
fullPattern = append(fullPattern, bases[i], formattedDatePatterns[i])
} else {
fullPattern = append(fullPattern, formattedDatePatterns[i], bases[i])
}
}
formattedDatePatterns = formattedDatePatterns[i:]
bases = bases[i:]
if len(bases) == 0 {
fullPattern = append(fullPattern, formattedDatePatterns...)
} else {
fullPattern = append(fullPattern, bases...)
}
return formatted + base
return strings.Join(fullPattern, "")
}
func patternToLayout(pattern string) string {
......@@ -329,3 +354,10 @@ func patternToLayout(pattern string) string {
return pattern
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
......@@ -77,6 +77,16 @@ func TestIndexPattern(t *testing.T) {
So(indices[0], ShouldEqual, "2018-data")
})
indexPatternScenario(intervalDaily, "YYYY[-data-]MM.DD", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "2018-data-05.15")
})
indexPatternScenario(intervalDaily, "[data-]YYYY[-moredata-]MM.DD", tsdb.NewTimeRange(from, to), func(indices []string) {
So(indices, ShouldHaveLength, 1)
So(indices[0], ShouldEqual, "data-2018-moredata-05.15")
})
Convey("Should return 01 week", func() {
from = fmt.Sprintf("%d", time.Date(2018, 1, 15, 17, 50, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
to = fmt.Sprintf("%d", time.Date(2018, 1, 15, 17, 55, 0, 0, time.UTC).UnixNano()/int64(time.Millisecond))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment