CloudWatch: move QueryData input parsing types to separate package (#57165)
* CloudWatch: move parse request types separate package * Move metric query constants, unexport metricDataQuery json decoding type * Unexport isSearchExpression
This commit is contained in:
@@ -11,12 +11,13 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
)
|
||||
|
||||
func (e *cloudWatchExecutor) parseResponse(startTime time.Time, endTime time.Time, metricDataOutputs []*cloudwatch.GetMetricDataOutput,
|
||||
queries []*cloudWatchQuery) ([]*responseWrapper, error) {
|
||||
queries []*models.CloudWatchQuery) ([]*responseWrapper, error) {
|
||||
aggregatedResponse := aggregateResponse(metricDataOutputs)
|
||||
queriesById := map[string]*cloudWatchQuery{}
|
||||
queriesById := map[string]*models.CloudWatchQuery{}
|
||||
for _, query := range queries {
|
||||
queriesById[query.Id] = query
|
||||
}
|
||||
@@ -87,7 +88,7 @@ func aggregateResponse(getMetricDataOutputs []*cloudwatch.GetMetricDataOutput) m
|
||||
return responseByID
|
||||
}
|
||||
|
||||
func getLabels(cloudwatchLabel string, query *cloudWatchQuery) data.Labels {
|
||||
func getLabels(cloudwatchLabel string, query *models.CloudWatchQuery) data.Labels {
|
||||
dims := make([]string, 0, len(query.Dimensions))
|
||||
for k := range query.Dimensions {
|
||||
dims = append(dims, k)
|
||||
@@ -112,19 +113,19 @@ func getLabels(cloudwatchLabel string, query *cloudWatchQuery) data.Labels {
|
||||
}
|
||||
|
||||
func buildDataFrames(startTime time.Time, endTime time.Time, aggregatedResponse queryRowResponse,
|
||||
query *cloudWatchQuery, dynamicLabelEnabled bool) (data.Frames, error) {
|
||||
query *models.CloudWatchQuery, dynamicLabelEnabled bool) (data.Frames, error) {
|
||||
frames := data.Frames{}
|
||||
for _, metric := range aggregatedResponse.Metrics {
|
||||
label := *metric.Label
|
||||
|
||||
deepLink, err := query.buildDeepLink(startTime, endTime, dynamicLabelEnabled)
|
||||
deepLink, err := query.BuildDeepLink(startTime, endTime, dynamicLabelEnabled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// In case a multi-valued dimension is used and the cloudwatch query yields no values, create one empty time
|
||||
// series for each dimension value. Use that dimension value to expand the alias field
|
||||
if len(metric.Values) == 0 && query.isMultiValuedDimensionExpression() {
|
||||
if len(metric.Values) == 0 && query.IsMultiValuedDimensionExpression() {
|
||||
series := 0
|
||||
multiValuedDimension := ""
|
||||
for key, values := range query.Dimensions {
|
||||
@@ -221,45 +222,45 @@ func buildDataFrames(startTime time.Time, endTime time.Time, aggregatedResponse
|
||||
return frames, nil
|
||||
}
|
||||
|
||||
func formatAlias(query *cloudWatchQuery, stat string, dimensions map[string]string, label string) string {
|
||||
func formatAlias(query *models.CloudWatchQuery, stat string, dimensions map[string]string, label string) string {
|
||||
region := query.Region
|
||||
namespace := query.Namespace
|
||||
metricName := query.MetricName
|
||||
period := strconv.Itoa(query.Period)
|
||||
|
||||
if query.isUserDefinedSearchExpression() {
|
||||
if query.IsUserDefinedSearchExpression() {
|
||||
pIndex := strings.LastIndex(query.Expression, ",")
|
||||
period = strings.Trim(query.Expression[pIndex+1:], " )")
|
||||
sIndex := strings.LastIndex(query.Expression[:pIndex], ",")
|
||||
stat = strings.Trim(query.Expression[sIndex+1:pIndex], " '")
|
||||
}
|
||||
|
||||
if len(query.Alias) == 0 && query.isMathExpression() {
|
||||
if len(query.Alias) == 0 && query.IsMathExpression() {
|
||||
return query.Id
|
||||
}
|
||||
if len(query.Alias) == 0 && query.isInferredSearchExpression() && !query.isMultiValuedDimensionExpression() {
|
||||
if len(query.Alias) == 0 && query.IsInferredSearchExpression() && !query.IsMultiValuedDimensionExpression() {
|
||||
return label
|
||||
}
|
||||
if len(query.Alias) == 0 && query.MetricQueryType == MetricQueryTypeQuery {
|
||||
if len(query.Alias) == 0 && query.MetricQueryType == models.MetricQueryTypeQuery {
|
||||
return label
|
||||
}
|
||||
|
||||
// common fields
|
||||
data := map[string]string{
|
||||
commonFields := map[string]string{
|
||||
"region": region,
|
||||
"period": period,
|
||||
}
|
||||
if len(label) != 0 {
|
||||
data["label"] = label
|
||||
commonFields["label"] = label
|
||||
}
|
||||
|
||||
// since the SQL query string is not (yet) parsed, we don't know what namespace, metric, statistic and labels it's using at this point
|
||||
if query.MetricQueryType != MetricQueryTypeQuery {
|
||||
data["namespace"] = namespace
|
||||
data["metric"] = metricName
|
||||
data["stat"] = stat
|
||||
if query.MetricQueryType != models.MetricQueryTypeQuery {
|
||||
commonFields["namespace"] = namespace
|
||||
commonFields["metric"] = metricName
|
||||
commonFields["stat"] = stat
|
||||
for k, v := range dimensions {
|
||||
data[k] = v
|
||||
commonFields[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +268,7 @@ func formatAlias(query *cloudWatchQuery, stat string, dimensions map[string]stri
|
||||
labelName := strings.Replace(string(in), "{{", "", 1)
|
||||
labelName = strings.Replace(labelName, "}}", "", 1)
|
||||
labelName = strings.TrimSpace(labelName)
|
||||
if val, exists := data[labelName]; exists {
|
||||
if val, exists := commonFields[labelName]; exists {
|
||||
return []byte(val)
|
||||
}
|
||||
|
||||
@@ -293,7 +294,7 @@ func createDataLinks(link string) []data.DataLink {
|
||||
return dataLinks
|
||||
}
|
||||
|
||||
func createMeta(query *cloudWatchQuery) *data.FrameMeta {
|
||||
func createMeta(query *models.CloudWatchQuery) *data.FrameMeta {
|
||||
return &data.FrameMeta{
|
||||
ExecutedQueryString: query.UsedExpression,
|
||||
Custom: fmt.Sprintf(`{
|
||||
|
||||
Reference in New Issue
Block a user