-
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.
Some example charts: <img width="1725" alt="Screenshot 2024-05-16 at 16 42 15" src="https://github.com/QuesmaOrg/quesma/assets/5407146/4a2d4731-deb6-4370-84af-399463a04c3c"> <img width="1720" alt="Screenshot 2024-05-16 at 16 40 43" src="https://github.com/QuesmaOrg/quesma/assets/5407146/89d01eb3-9ba1-4e66-9e19-efea6384c87d">
- Loading branch information
Showing
11 changed files
with
1,051 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package pipeline_aggregations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"mitmproxy/quesma/logger" | ||
"mitmproxy/quesma/model" | ||
"mitmproxy/quesma/queryprocessor" | ||
"mitmproxy/quesma/util" | ||
"strings" | ||
) | ||
|
||
type AverageBucket struct { | ||
ctx context.Context | ||
Parent string | ||
} | ||
|
||
func NewAverageBucket(ctx context.Context, bucketsPath string) AverageBucket { | ||
const delimiter = ">" | ||
withoutUnnecessarySuffix, _ := strings.CutSuffix(bucketsPath, delimiter+BucketsPathCount) | ||
lastDelimiterIdx := strings.LastIndex(withoutUnnecessarySuffix, delimiter) | ||
var parent string | ||
if lastDelimiterIdx+1 < len(withoutUnnecessarySuffix) { | ||
parent = withoutUnnecessarySuffix[lastDelimiterIdx+1:] | ||
} else { | ||
logger.WarnWithCtx(ctx).Msgf("invalid bucketsPath: %s, withoutUnnecessarySuffix: %s. Using empty string as parent.", bucketsPath, withoutUnnecessarySuffix) | ||
parent = "" | ||
} | ||
return AverageBucket{ctx: ctx, Parent: parent} | ||
} | ||
|
||
func (query AverageBucket) IsBucketAggregation() bool { | ||
return false | ||
} | ||
|
||
func (query AverageBucket) TranslateSqlResponseToJson(rows []model.QueryResultRow, level int) []model.JsonMap { | ||
if len(rows) == 0 { | ||
logger.WarnWithCtx(query.ctx).Msg("no rows returned for average bucket aggregation") | ||
return []model.JsonMap{{}} | ||
} | ||
var response []model.JsonMap | ||
for _, row := range rows { | ||
response = append(response, model.JsonMap{"value": row.Cols[len(row.Cols)-1].Value}) | ||
} | ||
return response | ||
} | ||
|
||
func (query AverageBucket) CalculateResultWhenMissing(qwa *model.Query, parentRows []model.QueryResultRow) []model.QueryResultRow { | ||
resultRows := make([]model.QueryResultRow, 0) | ||
if len(parentRows) == 0 { | ||
return resultRows // maybe null? | ||
} | ||
qp := queryprocessor.NewQueryProcessor(query.ctx) | ||
parentFieldsCnt := len(parentRows[0].Cols) - 2 // -2, because row is [parent_cols..., current_key, current_value] | ||
// in calculateSingleAvgBucket we calculate avg all current_keys with the same parent_cols | ||
// so we need to split into buckets based on parent_cols | ||
for _, parentRowsOneBucket := range qp.SplitResultSetIntoBuckets(parentRows, parentFieldsCnt) { | ||
resultRows = append(resultRows, query.calculateSingleAvgBucket(parentRowsOneBucket)) | ||
} | ||
return resultRows | ||
} | ||
|
||
// we're sure len(parentRows) > 0 | ||
func (query AverageBucket) calculateSingleAvgBucket(parentRows []model.QueryResultRow) model.QueryResultRow { | ||
if len(parentRows) == 0 { | ||
logger.WarnWithCtx(query.ctx).Msg("no parent rows, should NEVER happen") | ||
return model.QueryResultRow{} | ||
} | ||
|
||
var resultValue float64 | ||
rowsCnt := 0 | ||
if _, firstRowValueIsFloat := util.ExtractFloat64Maybe(parentRows[0].LastColValue()); firstRowValueIsFloat { | ||
sum := 0.0 | ||
for _, parentRow := range parentRows { | ||
value, ok := util.ExtractFloat64Maybe(parentRow.LastColValue()) | ||
if ok { | ||
sum += value | ||
rowsCnt++ | ||
} else { | ||
logger.WarnWithCtx(query.ctx).Msgf("could not convert value to float: %v, type: %T. Skipping", parentRow.LastColValue(), parentRow.LastColValue()) | ||
} | ||
} | ||
resultValue = sum / float64(rowsCnt) | ||
} else { | ||
var sum int64 | ||
for _, parentRow := range parentRows { | ||
value, ok := util.ExtractInt64Maybe(parentRow.LastColValue()) | ||
if ok { | ||
sum += value | ||
rowsCnt++ | ||
} else { | ||
logger.WarnWithCtx(query.ctx).Msgf("could not convert value to int: %v, type: %T. Skipping", parentRow.LastColValue(), parentRow.LastColValue()) | ||
} | ||
} | ||
resultValue = float64(sum) / float64(rowsCnt) | ||
} | ||
|
||
resultRow := parentRows[0].Copy() | ||
resultRow.Cols[len(resultRow.Cols)-1].Value = resultValue | ||
return resultRow | ||
} | ||
|
||
func (query AverageBucket) PostprocessResults(rowsFromDB []model.QueryResultRow) []model.QueryResultRow { | ||
return rowsFromDB | ||
} | ||
|
||
func (query AverageBucket) String() string { | ||
return fmt.Sprintf("avg_bucket(%s)", query.Parent) | ||
} |
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
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
Oops, something went wrong.