2023-12-12 07:24:33 +05:30
package v4
import (
"fmt"
2024-01-09 22:19:03 +05:30
"time"
2023-12-12 07:24:33 +05:30
2024-01-09 22:19:03 +05:30
metricsV3 "go.signoz.io/signoz/pkg/query-service/app/metrics/v3"
2024-01-16 16:56:20 +05:30
"go.signoz.io/signoz/pkg/query-service/app/metrics/v4/cumulative"
"go.signoz.io/signoz/pkg/query-service/app/metrics/v4/delta"
"go.signoz.io/signoz/pkg/query-service/app/metrics/v4/helpers"
"go.signoz.io/signoz/pkg/query-service/common"
2024-01-09 22:19:03 +05:30
"go.signoz.io/signoz/pkg/query-service/model"
2023-12-12 07:24:33 +05:30
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)
2024-01-16 16:56:20 +05:30
// PrepareMetricQuery prepares the query to be used for fetching metrics
// from the database
// start and end are in milliseconds
// step is in seconds
func PrepareMetricQuery ( start , end int64 , queryType v3 . QueryType , panelType v3 . PanelType , mq * v3 . BuilderQuery , options metricsV3 . Options ) ( string , error ) {
2023-12-12 07:24:33 +05:30
2024-01-16 16:56:20 +05:30
start , end = common . AdjustedMetricTimeRange ( start , end , mq . StepInterval , mq . TimeAggregation )
2023-12-12 07:24:33 +05:30
2024-01-26 17:07:23 +05:30
var quantile float64
2024-03-01 14:51:50 +05:30
if v3 . IsPercentileOperator ( mq . SpaceAggregation ) &&
mq . AggregateAttribute . Type != v3 . AttributeKeyType ( v3 . MetricTypeExponentialHistogram ) {
2024-01-26 17:07:23 +05:30
quantile = v3 . GetPercentileFromOperator ( mq . SpaceAggregation )
2024-01-16 16:56:20 +05:30
// If quantile is set, we need to group by le
// and set the space aggregation to sum
// and time aggregation to rate
mq . TimeAggregation = v3 . TimeAggregationRate
mq . SpaceAggregation = v3 . SpaceAggregationSum
2024-02-11 00:31:47 +05:30
// If le is not present in group by for quantile, add it
leFound := false
for _ , groupBy := range mq . GroupBy {
if groupBy . Key == "le" {
leFound = true
break
}
}
if ! leFound {
mq . GroupBy = append ( mq . GroupBy , v3 . AttributeKey {
Key : "le" ,
Type : v3 . AttributeKeyTypeTag ,
DataType : v3 . AttributeKeyDataTypeString ,
} )
}
2023-12-12 07:24:33 +05:30
}
2024-01-16 16:56:20 +05:30
var query string
var err error
if mq . Temporality == v3 . Delta {
if panelType == v3 . PanelTypeTable {
query , err = delta . PrepareMetricQueryDeltaTable ( start , end , mq . StepInterval , mq )
} else {
query , err = delta . PrepareMetricQueryDeltaTimeSeries ( start , end , mq . StepInterval , mq )
}
} else {
if panelType == v3 . PanelTypeTable {
query , err = cumulative . PrepareMetricQueryCumulativeTable ( start , end , mq . StepInterval , mq )
} else {
query , err = cumulative . PrepareMetricQueryCumulativeTimeSeries ( start , end , mq . StepInterval , mq )
}
}
2023-12-12 07:24:33 +05:30
2024-01-16 16:56:20 +05:30
if err != nil {
return "" , err
}
2024-01-09 22:19:03 +05:30
2024-02-11 00:31:47 +05:30
groupByWithoutLe := [ ] v3 . AttributeKey { }
for _ , groupBy := range mq . GroupBy {
if groupBy . Key != "le" {
groupByWithoutLe = append ( groupByWithoutLe , groupBy )
}
}
groupBy := helpers . GroupByAttributeKeyTags ( groupByWithoutLe ... )
orderBy := helpers . OrderByAttributeKeyTags ( mq . OrderBy , groupByWithoutLe )
2024-03-01 14:51:50 +05:30
// fixed-bucket histogram quantiles are calculated with UDF
if quantile != 0 && mq . AggregateAttribute . Type != v3 . AttributeKeyType ( v3 . MetricTypeExponentialHistogram ) {
2024-01-26 17:07:23 +05:30
query = fmt . Sprintf ( ` SELECT %s, histogramQuantile(arrayMap(x -> toFloat64(x), groupArray(le)), groupArray(value), %.3f) as value FROM (%s) GROUP BY %s ORDER BY %s ` , groupBy , quantile , query , groupBy , orderBy )
2024-01-16 16:56:20 +05:30
}
2024-01-09 22:19:03 +05:30
2024-01-16 16:56:20 +05:30
return query , nil
2024-01-09 22:19:03 +05:30
}
func BuildPromQuery ( promQuery * v3 . PromQuery , step , start , end int64 ) * model . QueryRangeParams {
return & model . QueryRangeParams {
Query : promQuery . Query ,
Start : time . UnixMilli ( start ) ,
End : time . UnixMilli ( end ) ,
Step : time . Duration ( step * int64 ( time . Second ) ) ,
}
}