Skip to content

Commit

Permalink
feat: added series aggregation for group by with value type panel | 5949
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketio-ctrl committed Jan 7, 2025
1 parent 3d21b6a commit 421a6c2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 58 deletions.
10 changes: 5 additions & 5 deletions pkg/query-service/app/metrics/v4/helpers/series_agg_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)

func AddSeriesAggregation(seriesAggregator v3.SeriesAggregation, query string) string {
func AddSecondaryAggregation(seriesAggregator v3.SecondaryAggregation, query string) string {
queryImpl := "SELECT %s as aggregated_value, ts" +
" FROM (%s)" +
" GROUP BY ts" +
" ORDER BY ts"

var op string
switch seriesAggregator {
case v3.SeriesAggregationAvg:
case v3.SecondaryAggregationAvg:
op = "avg(value)"
query = fmt.Sprintf(queryImpl, op, query)
case v3.SeriesAggregationSum:
case v3.SecondaryAggregationSum:
op = "sum(value)"
query = fmt.Sprintf(queryImpl, op, query)
case v3.SeriesAggregationMin:
case v3.SecondaryAggregationMin:
op = "min(value)"
query = fmt.Sprintf(queryImpl, op, query)
case v3.SeriesAggregationMax:
case v3.SecondaryAggregationMax:
op = "max(value)"
query = fmt.Sprintf(queryImpl, op, query)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/query-service/app/metrics/v4/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func PrepareMetricQuery(start, end int64, queryType v3.QueryType, panelType v3.P
}

if panelType == v3.PanelTypeValue && len(mq.GroupBy) > 0 {
query = helpers.AddSeriesAggregation(mq.SeriesAggregation, query)
query = helpers.AddSecondaryAggregation(mq.SecondaryAggregation, query)
}

return query, nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/query-service/app/metrics/v4/query_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,10 @@ func TestPrepareMetricQueryValueTypePanelWithGroupBY(t *testing.T) {
Type: v3.AttributeKeyType("Gauge"),
IsColumn: true,
},
Temporality: v3.Delta,
TimeAggregation: v3.TimeAggregationAnyLast,
SpaceAggregation: v3.SpaceAggregationAvg,
SeriesAggregation: v3.SeriesAggregationMax,
Temporality: v3.Delta,
TimeAggregation: v3.TimeAggregationAnyLast,
SpaceAggregation: v3.SpaceAggregationAvg,
SecondaryAggregation: v3.SecondaryAggregationMax,
Filters: &v3.FilterSet{
Operator: "AND",
Items: []v3.FilterItem{
Expand Down Expand Up @@ -694,10 +694,10 @@ func TestPrepareMetricQueryValueTypePanelWithGroupBY(t *testing.T) {
Type: v3.AttributeKeyType("Gauge"),
IsColumn: true,
},
Temporality: v3.Cumulative,
TimeAggregation: v3.TimeAggregationAnyLast,
SpaceAggregation: v3.SpaceAggregationAvg,
SeriesAggregation: v3.SeriesAggregationMax,
Temporality: v3.Cumulative,
TimeAggregation: v3.TimeAggregationAnyLast,
SpaceAggregation: v3.SpaceAggregationAvg,
SecondaryAggregation: v3.SecondaryAggregationMax,
Filters: &v3.FilterSet{
Operator: "AND",
Items: []v3.FilterItem{
Expand Down
2 changes: 1 addition & 1 deletion pkg/query-service/app/queryBuilder/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (c *cacheKeyGenerator) GenerateKeys(params *v3.QueryRangeParamsV3) map[stri
parts = append(parts, fmt.Sprintf("groupBy-%d=%s", idx, groupBy.CacheKey()))
}
if params.CompositeQuery.PanelType == v3.PanelTypeValue {
parts = append(parts, fmt.Sprintf("seriesAggregation=%s", query.SeriesAggregation))
parts = append(parts, fmt.Sprintf("secondaryAggregation=%s", query.SecondaryAggregation))
}
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/query-service/app/queryBuilder/query_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1300,14 +1300,14 @@ func TestGenerateCacheKeysMetricsBuilder(t *testing.T) {
QueryType: v3.QueryTypeBuilder,
BuilderQueries: map[string]*v3.BuilderQuery{
"A": {
QueryName: "A",
StepInterval: 60,
DataSource: v3.DataSourceMetrics,
AggregateOperator: v3.AggregateOperatorSumRate,
SeriesAggregation: v3.SeriesAggregationMax,
Expression: "A",
AggregateAttribute: v3.AttributeKey{Key: "signoz_latency_bucket"},
Temporality: v3.Delta,
QueryName: "A",
StepInterval: 60,
DataSource: v3.DataSourceMetrics,
AggregateOperator: v3.AggregateOperatorSumRate,
SecondaryAggregation: v3.SecondaryAggregationMax,
Expression: "A",
AggregateAttribute: v3.AttributeKey{Key: "signoz_latency_bucket"},
Temporality: v3.Delta,
Filters: &v3.FilterSet{
Operator: "AND",
Items: []v3.FilterItem{
Expand All @@ -1334,7 +1334,7 @@ func TestGenerateCacheKeysMetricsBuilder(t *testing.T) {
},
},
expectedCacheKeys: map[string]string{
"A": "source=metrics&step=60&aggregate=sum_rate&timeAggregation=&spaceAggregation=&aggregateAttribute=signoz_latency_bucket---false&filter-0=key:service_name---false,op:=,value:A&groupBy-0=service_name---false&groupBy-1=le---false&seriesAggregation=max&having-0=column:value,op:>,value:100",
"A": "source=metrics&step=60&aggregate=sum_rate&timeAggregation=&spaceAggregation=&aggregateAttribute=signoz_latency_bucket---false&filter-0=key:service_name---false,op:=,value:A&groupBy-0=service_name---false&groupBy-1=le---false&secondaryAggregation=max&having-0=column:value,op:>,value:100",
},
},
{
Expand Down
68 changes: 34 additions & 34 deletions pkg/query-service/model/v3/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,22 +712,22 @@ func GetPercentileFromOperator(operator SpaceAggregation) float64 {
}
}

type SeriesAggregation string
type SecondaryAggregation string

const (
SeriesAggregationUnspecified SeriesAggregation = ""
SeriesAggregationSum SeriesAggregation = "sum"
SeriesAggregationAvg SeriesAggregation = "avg"
SeriesAggregationMin SeriesAggregation = "min"
SeriesAggregationMax SeriesAggregation = "max"
SecondaryAggregationUnspecified SecondaryAggregation = ""
SecondaryAggregationSum SecondaryAggregation = "sum"
SecondaryAggregationAvg SecondaryAggregation = "avg"
SecondaryAggregationMin SecondaryAggregation = "min"
SecondaryAggregationMax SecondaryAggregation = "max"
)

func (s SeriesAggregation) Validate() error {
func (s SecondaryAggregation) Validate() error {
switch s {
case SeriesAggregationSum,
SeriesAggregationAvg,
SeriesAggregationMin,
SeriesAggregationMax:
case SecondaryAggregationSum,
SecondaryAggregationAvg,
SecondaryAggregationMin,
SecondaryAggregationMax:
return nil
default:
return fmt.Errorf("invalid series aggregation: %s", s)
Expand Down Expand Up @@ -806,28 +806,28 @@ func (m *MetricValueFilter) Clone() *MetricValueFilter {
}

type BuilderQuery struct {
QueryName string `json:"queryName"`
StepInterval int64 `json:"stepInterval"`
DataSource DataSource `json:"dataSource"`
AggregateOperator AggregateOperator `json:"aggregateOperator"`
AggregateAttribute AttributeKey `json:"aggregateAttribute,omitempty"`
Temporality Temporality `json:"temporality,omitempty"`
Filters *FilterSet `json:"filters,omitempty"`
GroupBy []AttributeKey `json:"groupBy,omitempty"`
Expression string `json:"expression"`
Disabled bool `json:"disabled"`
Having []Having `json:"having,omitempty"`
Legend string `json:"legend,omitempty"`
Limit uint64 `json:"limit"`
Offset uint64 `json:"offset"`
PageSize uint64 `json:"pageSize"`
OrderBy []OrderBy `json:"orderBy,omitempty"`
ReduceTo ReduceToOperator `json:"reduceTo,omitempty"`
SelectColumns []AttributeKey `json:"selectColumns,omitempty"`
TimeAggregation TimeAggregation `json:"timeAggregation,omitempty"`
SpaceAggregation SpaceAggregation `json:"spaceAggregation,omitempty"`
SeriesAggregation SeriesAggregation `json:"seriesAggregation,omitempty"`
Functions []Function `json:"functions,omitempty"`
QueryName string `json:"queryName"`
StepInterval int64 `json:"stepInterval"`
DataSource DataSource `json:"dataSource"`
AggregateOperator AggregateOperator `json:"aggregateOperator"`
AggregateAttribute AttributeKey `json:"aggregateAttribute,omitempty"`
Temporality Temporality `json:"temporality,omitempty"`
Filters *FilterSet `json:"filters,omitempty"`
GroupBy []AttributeKey `json:"groupBy,omitempty"`
Expression string `json:"expression"`
Disabled bool `json:"disabled"`
Having []Having `json:"having,omitempty"`
Legend string `json:"legend,omitempty"`
Limit uint64 `json:"limit"`
Offset uint64 `json:"offset"`
PageSize uint64 `json:"pageSize"`
OrderBy []OrderBy `json:"orderBy,omitempty"`
ReduceTo ReduceToOperator `json:"reduceTo,omitempty"`
SelectColumns []AttributeKey `json:"selectColumns,omitempty"`
TimeAggregation TimeAggregation `json:"timeAggregation,omitempty"`
SpaceAggregation SpaceAggregation `json:"spaceAggregation,omitempty"`
SecondaryAggregation SecondaryAggregation `json:"seriesAggregation,omitempty"`
Functions []Function `json:"functions,omitempty"`
ShiftBy int64
IsAnomaly bool
QueriesUsedInFormula []string
Expand Down Expand Up @@ -982,7 +982,7 @@ func (b *BuilderQuery) Validate(panelType PanelType) error {
// }

if panelType == PanelTypeValue {
if err := b.SeriesAggregation.Validate(); err != nil {
if err := b.SecondaryAggregation.Validate(); err != nil {
return fmt.Errorf("series aggregation is required for value type panel with group by: %w", err)
}
}
Expand Down

0 comments on commit 421a6c2

Please sign in to comment.