2023-03-23 19:45:15 +05:30
package v3
import (
2023-05-12 16:25:22 +05:30
"fmt"
2023-03-23 19:45:15 +05:30
"testing"
2025-03-20 21:01:41 +05:30
v3 "github.com/SigNoz/signoz/pkg/query-service/model/v3"
2023-03-23 19:45:15 +05:30
"github.com/stretchr/testify/require"
)
func TestBuildQuery ( t * testing . T ) {
t . Run ( "TestSimpleQueryWithName" , func ( t * testing . T ) {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
AggregateOperator : v3 . AggregateOperatorRateMax ,
Expression : "A" ,
} ,
} ,
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeGraph ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
2023-03-23 19:45:15 +05:30
require . NoError ( t , err )
2025-02-06 17:26:58 +05:30
require . Contains ( t , query , "WHERE metric_name IN ['name']" )
2023-03-23 19:45:15 +05:30
} )
}
func TestBuildQueryWithFilters ( t * testing . T ) {
t . Run ( "TestBuildQueryWithFilters" , func ( t * testing . T ) {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
2023-04-07 09:46:21 +05:30
{ Key : v3 . AttributeKey { Key : "a" } , Value : "b" , Operator : v3 . FilterOperatorNotEqual } ,
{ Key : v3 . AttributeKey { Key : "code" } , Value : "ERROR_*" , Operator : v3 . FilterOperatorNotRegex } ,
2023-03-23 19:45:15 +05:30
} } ,
AggregateOperator : v3 . AggregateOperatorRateMax ,
Expression : "A" ,
2024-05-21 12:01:21 +05:30
Temporality : v3 . Cumulative ,
2023-03-23 19:45:15 +05:30
} ,
} ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
2023-03-23 19:45:15 +05:30
require . NoError ( t , err )
2025-03-03 14:04:16 +05:30
require . Contains ( t , query , "WHERE metric_name IN ['name'] AND temporality = 'Cumulative' AND __normalized = true AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000 AND JSONExtractString(labels, 'a') != 'b'" )
2023-03-23 19:45:15 +05:30
require . Contains ( t , query , rateWithoutNegative )
require . Contains ( t , query , "not match(JSONExtractString(labels, 'code'), 'ERROR_*')" )
} )
}
func TestBuildQueryWithMultipleQueries ( t * testing . T ) {
t . Run ( "TestBuildQueryWithFilters" , func ( t * testing . T ) {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
2023-04-07 09:46:21 +05:30
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
2023-03-23 19:45:15 +05:30
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
2024-05-21 12:01:21 +05:30
Temporality : v3 . Cumulative ,
2023-03-23 19:45:15 +05:30
Expression : "A" ,
} ,
"B" : {
QueryName : "B" ,
2023-09-17 10:40:45 +05:30
StepInterval : 60 ,
2023-03-23 19:45:15 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name2" } ,
AggregateOperator : v3 . AggregateOperatorRateMax ,
2024-05-21 12:01:21 +05:30
Temporality : v3 . Cumulative ,
2023-03-23 19:45:15 +05:30
Expression : "B" ,
} ,
} ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
2023-03-23 19:45:15 +05:30
require . NoError ( t , err )
2025-03-03 14:04:16 +05:30
require . Contains ( t , query , "WHERE metric_name IN ['name'] AND temporality = 'Cumulative' AND __normalized = true AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000 AND JSONExtractString(labels, 'in') IN ['a','b','c']" )
2023-03-23 19:45:15 +05:30
require . Contains ( t , query , rateWithoutNegative )
} )
}
2023-05-12 16:25:22 +05:30
2023-07-03 13:30:37 +05:30
func TestBuildQueryXRate ( t * testing . T ) {
t . Run ( "TestBuildQueryXRate" , func ( t * testing . T ) {
2025-03-03 14:04:16 +05:30
tmpl := ` SELECT ts, %s(rate_value) as value FROM (SELECT ts, If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) as rate_value FROM(SELECT fingerprint, toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), INTERVAL 60 SECOND) as ts, max(value) as value FROM signoz_metrics.distributed_samples_v4 INNER JOIN (SELECT DISTINCT fingerprint FROM signoz_metrics.time_series_v4_1day WHERE metric_name IN ['name'] AND temporality = '' AND __normalized = true AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000) as filtered_time_series USING fingerprint WHERE metric_name IN ['name'] AND unix_milli >= 1650991920000 AND unix_milli < 1651078380000 GROUP BY fingerprint, ts ORDER BY fingerprint, ts) WINDOW rate_window as (PARTITION BY fingerprint ORDER BY fingerprint, ts) ) WHERE isNaN(rate_value) = 0 GROUP BY ts ORDER BY ts `
2023-07-03 13:30:37 +05:30
cases := [ ] struct {
aggregateOperator v3 . AggregateOperator
expectedQuery string
} {
{
aggregateOperator : v3 . AggregateOperatorAvgRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorAvgRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMaxRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMaxRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMinRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMinRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorSumRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorSumRate ] ) ,
} ,
}
for _ , c := range cases {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-07-03 13:30:37 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
AggregateOperator : c . aggregateOperator ,
Expression : "A" ,
} ,
} ,
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeGraph ,
} ,
}
2023-08-18 07:32:05 +05:30
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
require . NoError ( t , err )
require . Equal ( t , query , c . expectedQuery )
}
} )
}
func TestBuildQueryRPM ( t * testing . T ) {
t . Run ( "TestBuildQueryXRate" , func ( t * testing . T ) {
2025-03-03 14:04:16 +05:30
tmpl := ` SELECT ts, ceil(value * 60) as value FROM (SELECT ts, %s(rate_value) as value FROM (SELECT ts, If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) as rate_value FROM(SELECT fingerprint, toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), INTERVAL 60 SECOND) as ts, max(value) as value FROM signoz_metrics.distributed_samples_v4 INNER JOIN (SELECT DISTINCT fingerprint FROM signoz_metrics.time_series_v4_1day WHERE metric_name IN ['name'] AND temporality = '' AND __normalized = true AND unix_milli >= 1650931200000 AND unix_milli < 1651078380000) as filtered_time_series USING fingerprint WHERE metric_name IN ['name'] AND unix_milli >= 1650991920000 AND unix_milli < 1651078380000 GROUP BY fingerprint, ts ORDER BY fingerprint, ts) WINDOW rate_window as (PARTITION BY fingerprint ORDER BY fingerprint, ts) ) WHERE isNaN(rate_value) = 0 GROUP BY ts ORDER BY ts) `
2023-08-18 07:32:05 +05:30
cases := [ ] struct {
aggregateOperator v3 . AggregateOperator
expectedQuery string
} {
{
aggregateOperator : v3 . AggregateOperatorAvgRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorAvgRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMaxRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMaxRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorMinRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorMinRate ] ) ,
} ,
{
aggregateOperator : v3 . AggregateOperatorSumRate ,
expectedQuery : fmt . Sprintf ( tmpl , aggregateOperatorToSQLFunc [ v3 . AggregateOperatorSumRate ] ) ,
} ,
}
for _ , c := range cases {
q := & v3 . QueryRangeParamsV3 {
Start : 1650991982000 ,
End : 1651078382000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
2023-08-22 17:09:58 +05:30
StepInterval : 60 ,
2023-08-18 07:32:05 +05:30
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
AggregateOperator : c . aggregateOperator ,
Expression : "A" ,
} ,
} ,
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeGraph ,
} ,
}
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : true } )
2023-07-03 13:30:37 +05:30
require . NoError ( t , err )
require . Equal ( t , query , c . expectedQuery )
}
} )
}
2023-08-22 17:09:58 +05:30
func TestBuildQueryAdjustedTimes ( t * testing . T ) {
cases := [ ] struct {
name string
params * v3 . QueryRangeParamsV3
expected string
} {
{
name : "TestBuildQueryAdjustedTimes start close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:29
Start : 1686082289000 ,
// 20:41:00
End : 1686084060000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 60 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:10:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082200000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 50 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:52
Start : 1686082312000 ,
// 20:41:00
End : 1686084060000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 60 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:10:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082200000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:00
End : 1686084060000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 30 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:11:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082260000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 30 seconds and end close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:29
End : 1686084089000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 30 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:11:00 - 20:41:00
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686082260000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 300 seconds and end close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:29
End : 1686084089000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 300 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:05:00 - 20:41:00
// 20:10:00 is the nearest 5 minute interval, but we round down to 20:05:00
// as this is a rate query and we want to include the previous value for the first interval
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686081900000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
{
name : "TestBuildQueryAdjustedTimes start close to 42 seconds with step 180 seconds and end close to 30 seconds" ,
params : & v3 . QueryRangeParamsV3 {
// 20:11:42
Start : 1686082302000 ,
// 20:41:29
End : 1686084089000 ,
CompositeQuery : & v3 . CompositeQuery {
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
StepInterval : 180 ,
AggregateAttribute : v3 . AttributeKey { Key : "name" } ,
Filters : & v3 . FilterSet { Operator : "AND" , Items : [ ] v3 . FilterItem {
{ Key : v3 . AttributeKey { Key : "in" } , Value : [ ] interface { } { "a" , "b" , "c" } , Operator : v3 . FilterOperatorIn } ,
} } ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
Expression : "A" ,
} ,
} ,
} ,
} ,
2023-12-29 12:35:22 +05:30
// 20:06:00 - 20:39:00
// 20:09:00 is the nearest 3 minute interval, but we round down to 20:06:00
// as this is a rate query and we want to include the previous value for the first interval
2024-05-21 12:01:21 +05:30
expected : "unix_milli >= 1686081960000 AND unix_milli < 1686084060000" ,
2023-08-22 17:09:58 +05:30
} ,
}
for _ , testCase := range cases {
t . Run ( testCase . name , func ( t * testing . T ) {
q := testCase . params
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
require . NoError ( t , err )
require . Contains ( t , query , testCase . expected )
} )
}
}
2025-01-03 10:52:55 +05:30
func TestBuildQueryWithDotInMetricAndAttributes ( t * testing . T ) {
cases := [ ] struct {
name string
params * v3 . QueryRangeParamsV3
expected string
} {
{
name : "TestBuildQueryWithDotInMetricAndAttributes with dot in metric and attributes" ,
params : & v3 . QueryRangeParamsV3 {
Start : 1735036101000 ,
End : 1735637901000 ,
Step : 60 ,
Variables : map [ string ] interface { } {
"SIGNOZ_START_TIME" : 1735034992000 ,
"SIGNOZ_END_TIME" : 1735036792000 ,
} ,
FormatForWeb : false ,
CompositeQuery : & v3 . CompositeQuery {
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeValue ,
FillGaps : false ,
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
DataSource : v3 . DataSourceMetrics ,
AggregateOperator : v3 . AggregateOperatorAvg ,
AggregateAttribute : v3 . AttributeKey {
Key : "system.memory.usage" ,
DataType : v3 . AttributeKeyDataTypeFloat64 ,
Type : v3 . AttributeKeyType ( "Gauge" ) ,
IsColumn : true ,
} ,
TimeAggregation : v3 . TimeAggregationAvg ,
SpaceAggregation : v3 . SpaceAggregationSum ,
Filters : & v3 . FilterSet {
Operator : "AND" ,
Items : [ ] v3 . FilterItem {
{
Key : v3 . AttributeKey {
Key : "os.type" ,
DataType : v3 . AttributeKeyDataTypeString ,
Type : v3 . AttributeKeyTypeTag ,
IsColumn : false ,
} ,
Operator : v3 . FilterOperatorEqual ,
Value : "linux" ,
} ,
} ,
} ,
Expression : "A" ,
Disabled : false ,
StepInterval : 60 ,
OrderBy : [ ] v3 . OrderBy {
{
ColumnName : "os.type" ,
Order : v3 . DirectionAsc ,
DataType : v3 . AttributeKeyDataTypeString ,
Type : v3 . AttributeKeyTypeTag ,
} ,
} ,
GroupBy : [ ] v3 . AttributeKey {
{
Key : "os.type" ,
DataType : v3 . AttributeKeyDataTypeString ,
Type : v3 . AttributeKeyTypeTag ,
IsColumn : false ,
} ,
} ,
Legend : "" ,
ReduceTo : v3 . ReduceToOperatorAvg ,
Having : [ ] v3 . Having { } ,
} ,
} ,
} ,
} ,
2025-03-03 14:04:16 +05:30
expected : "SELECT *, now() AS ts FROM (SELECT avgIf(value, toUnixTimestamp(ts) != 0) as value, anyIf(ts, toUnixTimestamp(ts) != 0) AS timestamp FROM (SELECT `os.type`, toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), INTERVAL 60 SECOND) as ts, avg(value) as value FROM signoz_metrics.distributed_samples_v4 INNER JOIN (SELECT DISTINCT JSONExtractString(labels, 'os.type') as `os.type`, fingerprint FROM signoz_metrics.time_series_v4_1day WHERE metric_name IN ['system.memory.usage'] AND temporality = '' AND __normalized = true AND unix_milli >= 1734998400000 AND unix_milli < 1735637880000 AND JSONExtractString(labels, 'os.type') = 'linux') as filtered_time_series USING fingerprint WHERE metric_name IN ['system.memory.usage'] AND unix_milli >= 1735036080000 AND unix_milli < 1735637880000 GROUP BY `os.type`, ts ORDER BY `os.type` asc, ts) )" ,
2025-01-03 10:52:55 +05:30
} ,
{
name : "TestBuildQueryWithDotInMetricAndAttributes with dot in metric and attributes with rate_avg aggregation" ,
params : & v3 . QueryRangeParamsV3 {
Start : 1735036101000 ,
End : 1735637901000 ,
Step : 60 ,
Variables : map [ string ] interface { } {
"SIGNOZ_START_TIME" : 1735034992000 ,
"SIGNOZ_END_TIME" : 1735036792000 ,
} ,
FormatForWeb : false ,
CompositeQuery : & v3 . CompositeQuery {
QueryType : v3 . QueryTypeBuilder ,
PanelType : v3 . PanelTypeValue ,
FillGaps : false ,
BuilderQueries : map [ string ] * v3 . BuilderQuery {
"A" : {
QueryName : "A" ,
DataSource : v3 . DataSourceMetrics ,
AggregateOperator : v3 . AggregateOperatorRateAvg ,
AggregateAttribute : v3 . AttributeKey {
Key : "system.memory.usage" ,
DataType : v3 . AttributeKeyDataTypeFloat64 ,
Type : v3 . AttributeKeyType ( "Gauge" ) ,
IsColumn : true ,
} ,
TimeAggregation : v3 . TimeAggregationAvg ,
SpaceAggregation : v3 . SpaceAggregationSum ,
Filters : & v3 . FilterSet {
Operator : "AND" ,
Items : [ ] v3 . FilterItem {
{
Key : v3 . AttributeKey {
Key : "os.type" ,
DataType : v3 . AttributeKeyDataTypeString ,
Type : v3 . AttributeKeyTypeTag ,
IsColumn : false ,
} ,
Operator : v3 . FilterOperatorEqual ,
Value : "linux" ,
} ,
} ,
} ,
Expression : "A" ,
Disabled : false ,
StepInterval : 60 ,
OrderBy : [ ] v3 . OrderBy {
{
ColumnName : "os.type" ,
Order : v3 . DirectionAsc ,
DataType : v3 . AttributeKeyDataTypeString ,
Type : v3 . AttributeKeyTypeTag ,
} ,
} ,
GroupBy : [ ] v3 . AttributeKey {
{
Key : "os.type" ,
DataType : v3 . AttributeKeyDataTypeString ,
Type : v3 . AttributeKeyTypeTag ,
IsColumn : false ,
} ,
} ,
Legend : "" ,
ReduceTo : v3 . ReduceToOperatorAvg ,
Having : [ ] v3 . Having { } ,
} ,
} ,
} ,
} ,
2025-03-03 14:04:16 +05:30
expected : "SELECT *, now() AS ts FROM (SELECT avgIf(value, toUnixTimestamp(ts) != 0) as value, anyIf(ts, toUnixTimestamp(ts) != 0) AS timestamp FROM (SELECT `os.type`, ts, If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) as value FROM(SELECT `os.type`, toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), INTERVAL 60 SECOND) as ts, avg(value) as value FROM signoz_metrics.distributed_samples_v4 INNER JOIN (SELECT DISTINCT JSONExtractString(labels, 'os.type') as `os.type`, fingerprint FROM signoz_metrics.time_series_v4_1day WHERE metric_name IN ['system.memory.usage'] AND temporality = '' AND __normalized = true AND unix_milli >= 1734998400000 AND unix_milli < 1735637880000 AND JSONExtractString(labels, 'os.type') = 'linux') as filtered_time_series USING fingerprint WHERE metric_name IN ['system.memory.usage'] AND unix_milli >= 1735036020000 AND unix_milli < 1735637880000 GROUP BY `os.type`, ts ORDER BY `os.type` asc, ts) WINDOW rate_window as (PARTITION BY `os.type` ORDER BY `os.type`, ts) ) )" ,
2025-01-03 10:52:55 +05:30
} ,
}
for _ , testCase := range cases {
t . Run ( testCase . name , func ( t * testing . T ) {
q := testCase . params
query , err := PrepareMetricQuery ( q . Start , q . End , q . CompositeQuery . QueryType , q . CompositeQuery . PanelType , q . CompositeQuery . BuilderQueries [ "A" ] , Options { PreferRPM : false } )
require . NoError ( t , err )
require . Contains ( t , query , testCase . expected )
} )
}
}