-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Grafana]
geohash_grid
: Support the only new aggregration from Graf…
…ana (#1327) Doesn't support `bounds` parameter, but will warn to logs if someone uses it. Only `precision` is visible in Grafana, so doesn't really matter there. Let's add that in another PR if needed.
- Loading branch information
Showing
7 changed files
with
204 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package bucket_aggregations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/QuesmaOrg/quesma/platform/logger" | ||
"github.com/QuesmaOrg/quesma/platform/model" | ||
"reflect" | ||
) | ||
|
||
type GeoHashGrid struct { | ||
ctx context.Context | ||
} | ||
|
||
func NewGeoHashGrid(ctx context.Context) GeoHashGrid { | ||
return GeoHashGrid{ctx: ctx} | ||
} | ||
|
||
func (query GeoHashGrid) AggregationType() model.AggregationType { | ||
return model.BucketAggregation | ||
} | ||
|
||
func (query GeoHashGrid) TranslateSqlResponseToJson(rows []model.QueryResultRow) model.JsonMap { | ||
buckets := make([]model.JsonMap, 0, len(rows)) | ||
for _, row := range rows { | ||
if len(row.Cols) < 2 { | ||
logger.ErrorWithCtx(query.ctx).Msgf( | ||
"unexpected number of columns in geohash_grid aggregation response, len(rows[0].Cols): %d", | ||
len(row.Cols), | ||
) | ||
return model.JsonMap{"buckets": []model.JsonMap{}} | ||
} | ||
|
||
buckets = append(buckets, model.JsonMap{ | ||
"key": row.SecondLastColValue(), | ||
"doc_count": row.LastColValue(), | ||
}) | ||
} | ||
|
||
return model.JsonMap{ | ||
"buckets": buckets, | ||
} | ||
} | ||
|
||
func (query GeoHashGrid) String() string { | ||
return "geohash_grid" | ||
} | ||
|
||
// TODO make part of QueryType interface and implement for all aggregations | ||
// TODO add bad requests to tests | ||
// Doing so will ensure we see 100% of what we're interested in in our logs (now we see ~95%) | ||
func CheckParamsGeohashGrid(ctx context.Context, paramsRaw any) error { | ||
requiredParams := map[string]string{ | ||
"field": "string", | ||
} | ||
optionalParams := map[string]string{ | ||
"bounds": "map", | ||
"precision": "float64", // TODO should be int, low priority for fixing | ||
"shard_size": "float64", // TODO should be int, low priority for fixing | ||
"size": "float64", // TODO should be int, low priority for fixing | ||
} | ||
logIfYouSeeThemParams := []string{"bounds", "shard_size"} | ||
|
||
params, ok := paramsRaw.(model.JsonMap) | ||
if !ok { | ||
return fmt.Errorf("params is not a map, but %+v", paramsRaw) | ||
} | ||
|
||
// check if required are present | ||
for paramName, paramType := range requiredParams { | ||
paramVal, exists := params[paramName] | ||
if !exists { | ||
return fmt.Errorf("required parameter %s not found in params", paramName) | ||
} | ||
if reflect.TypeOf(paramVal).Name() != paramType { // TODO I'll make a small rewrite to not use reflect here | ||
return fmt.Errorf("required parameter %s is not of type %s, but %T", paramName, paramType, paramVal) | ||
} | ||
} | ||
|
||
// check if only required/optional are present | ||
for paramName := range params { | ||
if _, isRequired := requiredParams[paramName]; !isRequired { | ||
wantedType, isOptional := optionalParams[paramName] | ||
if !isOptional { | ||
return fmt.Errorf("unexpected parameter %s found in GeoHash Grid params %v", paramName, params) | ||
} | ||
if reflect.TypeOf(params[paramName]).Name() != wantedType { // TODO I'll make a small rewrite to not use reflect here | ||
return fmt.Errorf("optional parameter %s is not of type %s, but %T", paramName, wantedType, params[paramName]) | ||
} | ||
} | ||
} | ||
|
||
// log if you see them | ||
for _, warnParam := range logIfYouSeeThemParams { | ||
if _, exists := params[warnParam]; exists { | ||
logger.WarnWithCtxAndThrottling(ctx, "geohash_grid", warnParam, "we didn't expect %s in GeoHash Grid params %v", warnParam, params) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters