From 6000d6a1adfc131a3880531dc7d16df887fd8b8b Mon Sep 17 00:00:00 2001 From: Isabella Siu Date: Fri, 16 Aug 2024 09:42:39 -0400 Subject: [PATCH] ElasticSearch: Neaten up vars/consts (#91989) ElasticSearch: neaten up vars/consts --- pkg/tsdb/elasticsearch/client/client.go | 2 +- .../elasticsearch/client/index_pattern.go | 102 +++++++++--------- pkg/tsdb/elasticsearch/client/models.go | 6 +- .../elasticsearch/client/search_request.go | 18 ++-- pkg/tsdb/elasticsearch/response_parser.go | 3 +- 5 files changed, 65 insertions(+), 66 deletions(-) diff --git a/pkg/tsdb/elasticsearch/client/client.go b/pkg/tsdb/elasticsearch/client/client.go index b74e3416c8c..a9de26e65c9 100644 --- a/pkg/tsdb/elasticsearch/client/client.go +++ b/pkg/tsdb/elasticsearch/client/client.go @@ -23,7 +23,7 @@ import ( ) // Used in logging to mark a stage -var ( +const ( StagePrepareRequest = "prepareRequest" StageDatabaseRequest = "databaseRequest" StageParseResponse = "parseResponse" diff --git a/pkg/tsdb/elasticsearch/client/index_pattern.go b/pkg/tsdb/elasticsearch/client/index_pattern.go index aae58ec04ee..a7a7a6096f8 100644 --- a/pkg/tsdb/elasticsearch/client/index_pattern.go +++ b/pkg/tsdb/elasticsearch/client/index_pattern.go @@ -18,6 +18,57 @@ const ( intervalYearly = "yearly" ) +var datePatternRegex = regexp.MustCompile("(LT|LL?L?L?|l{1,4}|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|Q)") + +var datePatternReplacements = map[string]string{ + "M": "1", // stdNumMonth 1 2 ... 11 12 + "MM": "01", // stdZeroMonth 01 02 ... 11 12 + "MMM": "Jan", // stdMonth Jan Feb ... Nov Dec + "MMMM": "January", // stdLongMonth January February ... November December + "D": "2", // stdDay 1 2 ... 30 30 + "DD": "02", // stdZeroDay 01 02 ... 30 31 + "DDD": "", // Day of the year 1 2 ... 364 365 + "DDDD": "", // Day of the year 001 002 ... 364 365 @todo**** + "d": "", // Numeric representation of day of the week 0 1 ... 5 6 + "dd": "Mon", // ***Su Mo ... Fr Sa @todo + "ddd": "Mon", // Sun Mon ... Fri Sat + "dddd": "Monday", // stdLongWeekDay Sunday Monday ... Friday Saturday + "e": "", // Numeric representation of day of the week 0 1 ... 5 6 @todo + "E": "", // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 2 ... 6 7 @todo + "w": "", // 1 2 ... 52 53 + "ww": "", // ***01 02 ... 52 53 @todo + "W": "", // 1 2 ... 52 53 + "WW": "", // ***01 02 ... 52 53 @todo + "YY": "06", // stdYear 70 71 ... 29 30 + "YYYY": "2006", // stdLongYear 1970 1971 ... 2029 2030 + "gg": "", // ISO-8601 year number 70 71 ... 29 30 + "gggg": "", // ***1970 1971 ... 2029 2030 + "GG": "", // 70 71 ... 29 30 + "GGGG": "", // ***1970 1971 ... 2029 2030 + "Q": "", // 1, 2, 3, 4 + "A": "PM", // stdPM AM PM + "a": "pm", // stdpm am pm + "H": "", // stdHour 0 1 ... 22 23 + "HH": "15", // 00 01 ... 22 23 + "h": "3", // stdHour12 1 2 ... 11 12 + "hh": "03", // stdZeroHour12 01 02 ... 11 12 + "m": "4", // stdZeroMinute 0 1 ... 58 59 + "mm": "04", // stdZeroMinute 00 01 ... 58 59 + "s": "5", // stdSecond 0 1 ... 58 59 + "ss": "05", // stdZeroSecond ***00 01 ... 58 59 + "z": "MST", // EST CST ... MST PST + "zz": "MST", // EST CST ... MST PST + "Z": "Z07:00", // stdNumColonTZ -07:00 -06:00 ... +06:00 +07:00 + "ZZ": "-0700", // stdNumTZ -0700 -0600 ... +0600 +0700 + "X": "", // Seconds since unix epoch 1360013296 + "LT": "3:04 PM", // 8:30 PM + "L": "01/02/2006", // 09/04/1986 + "l": "1/2/2006", // 9/4/1986 + "ll": "Jan 2 2006", // Sep 4 1986 + "lll": "Jan 2 2006 3:04 PM", // Sep 4 1986 8:30 PM + "llll": "Mon, Jan 2 2006 3:04 PM", // Thu, Sep 4 1986 8:30 PM +} + type IndexPattern interface { GetIndices(timeRange backend.TimeRange) ([]string, error) } @@ -200,57 +251,6 @@ func (i *yearlyInterval) Generate(from, to time.Time) []time.Time { return intervals } -var datePatternRegex = regexp.MustCompile("(LT|LL?L?L?|l{1,4}|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|Q)") - -var datePatternReplacements = map[string]string{ - "M": "1", // stdNumMonth 1 2 ... 11 12 - "MM": "01", // stdZeroMonth 01 02 ... 11 12 - "MMM": "Jan", // stdMonth Jan Feb ... Nov Dec - "MMMM": "January", // stdLongMonth January February ... November December - "D": "2", // stdDay 1 2 ... 30 30 - "DD": "02", // stdZeroDay 01 02 ... 30 31 - "DDD": "", // Day of the year 1 2 ... 364 365 - "DDDD": "", // Day of the year 001 002 ... 364 365 @todo**** - "d": "", // Numeric representation of day of the week 0 1 ... 5 6 - "dd": "Mon", // ***Su Mo ... Fr Sa @todo - "ddd": "Mon", // Sun Mon ... Fri Sat - "dddd": "Monday", // stdLongWeekDay Sunday Monday ... Friday Saturday - "e": "", // Numeric representation of day of the week 0 1 ... 5 6 @todo - "E": "", // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 2 ... 6 7 @todo - "w": "", // 1 2 ... 52 53 - "ww": "", // ***01 02 ... 52 53 @todo - "W": "", // 1 2 ... 52 53 - "WW": "", // ***01 02 ... 52 53 @todo - "YY": "06", // stdYear 70 71 ... 29 30 - "YYYY": "2006", // stdLongYear 1970 1971 ... 2029 2030 - "gg": "", // ISO-8601 year number 70 71 ... 29 30 - "gggg": "", // ***1970 1971 ... 2029 2030 - "GG": "", // 70 71 ... 29 30 - "GGGG": "", // ***1970 1971 ... 2029 2030 - "Q": "", // 1, 2, 3, 4 - "A": "PM", // stdPM AM PM - "a": "pm", // stdpm am pm - "H": "", // stdHour 0 1 ... 22 23 - "HH": "15", // 00 01 ... 22 23 - "h": "3", // stdHour12 1 2 ... 11 12 - "hh": "03", // stdZeroHour12 01 02 ... 11 12 - "m": "4", // stdZeroMinute 0 1 ... 58 59 - "mm": "04", // stdZeroMinute 00 01 ... 58 59 - "s": "5", // stdSecond 0 1 ... 58 59 - "ss": "05", // stdZeroSecond ***00 01 ... 58 59 - "z": "MST", // EST CST ... MST PST - "zz": "MST", // EST CST ... MST PST - "Z": "Z07:00", // stdNumColonTZ -07:00 -06:00 ... +06:00 +07:00 - "ZZ": "-0700", // stdNumTZ -0700 -0600 ... +0600 +0700 - "X": "", // Seconds since unix epoch 1360013296 - "LT": "3:04 PM", // 8:30 PM - "L": "01/02/2006", // 09/04/1986 - "l": "1/2/2006", // 9/4/1986 - "ll": "Jan 2 2006", // Sep 4 1986 - "lll": "Jan 2 2006 3:04 PM", // Sep 4 1986 8:30 PM - "llll": "Mon, Jan 2 2006 3:04 PM", // Thu, Sep 4 1986 8:30 PM -} - func formatDate(t time.Time, pattern string) string { var formattedDatePatterns []string var bases []string diff --git a/pkg/tsdb/elasticsearch/client/models.go b/pkg/tsdb/elasticsearch/client/models.go index fd0d5b45d9e..69bdf2ea911 100644 --- a/pkg/tsdb/elasticsearch/client/models.go +++ b/pkg/tsdb/elasticsearch/client/models.go @@ -7,6 +7,9 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" ) +// DateFormatEpochMS represents a date format of epoch milliseconds (epoch_millis) +const DateFormatEpochMS = "epoch_millis" + // SearchRequest represents a search request type SearchRequest struct { Index string @@ -119,9 +122,6 @@ type RangeFilter struct { Format string } -// DateFormatEpochMS represents a date format of epoch milliseconds (epoch_millis) -const DateFormatEpochMS = "epoch_millis" - // MarshalJSON returns the JSON encoding of the query string filter. func (f *RangeFilter) MarshalJSON() ([]byte, error) { root := map[string]map[string]map[string]interface{}{ diff --git a/pkg/tsdb/elasticsearch/client/search_request.go b/pkg/tsdb/elasticsearch/client/search_request.go index 35414712252..cb0cf004c11 100644 --- a/pkg/tsdb/elasticsearch/client/search_request.go +++ b/pkg/tsdb/elasticsearch/client/search_request.go @@ -12,6 +12,15 @@ const ( HighlightPostTagsString = "@/HIGHLIGHT@" HighlightFragmentSize = 2147483647 DefaultGeoHashPrecision = 3 + + termsOrderTerm = "_term" +) + +type SortOrder string + +const ( + SortOrderAsc SortOrder = "asc" + SortOrderDesc SortOrder = "desc" ) // SearchRequestBuilder represents a builder which can build a search request @@ -79,13 +88,6 @@ func (b *SearchRequestBuilder) Size(size int) *SearchRequestBuilder { return b } -type SortOrder string - -const ( - SortOrderAsc SortOrder = "asc" - SortOrderDesc SortOrder = "desc" -) - // Sort adds a "asc" | "desc" sort to the search request func (b *SearchRequestBuilder) Sort(order SortOrder, field string, unmappedType string) *SearchRequestBuilder { if order != SortOrderAsc && order != SortOrderDesc { @@ -386,8 +388,6 @@ func (b *aggBuilderImpl) DateHistogram(key, field string, fn func(a *DateHistogr return b } -const termsOrderTerm = "_term" - func (b *aggBuilderImpl) Terms(key, field string, fn func(a *TermsAggregation, b AggBuilder)) AggBuilder { innerAgg := &TermsAggregation{ Field: field, diff --git a/pkg/tsdb/elasticsearch/response_parser.go b/pkg/tsdb/elasticsearch/response_parser.go index d7fae4473c2..ef9d15025cd 100644 --- a/pkg/tsdb/elasticsearch/response_parser.go +++ b/pkg/tsdb/elasticsearch/response_parser.go @@ -46,6 +46,7 @@ const ( ) var searchWordsRegex = regexp.MustCompile(regexp.QuoteMeta(es.HighlightPreTagsString) + `(.*?)` + regexp.QuoteMeta(es.HighlightPostTagsString)) +var aliasPatternRegex = regexp.MustCompile(`\{\{([\s\S]+?)\}\}`) func parseResponse(ctx context.Context, responses []*es.SearchResponse, targets []*Query, configuredFields es.ConfiguredFields, keepLabelsInResponse bool, logger log.Logger) (*backend.QueryDataResponse, error) { result := backend.QueryDataResponse{ @@ -923,8 +924,6 @@ func nameFields(queryResult backend.DataResponse, target *Query, keepLabelsInRes } } -var aliasPatternRegex = regexp.MustCompile(`\{\{([\s\S]+?)\}\}`) - func getFieldName(dataField data.Field, target *Query, metricTypeCount int) string { metricType := dataField.Labels["metric"] metricName := getMetricName(metricType)