From 2df5a9d72db851e7bde445c3a4e19987880cf4c3 Mon Sep 17 00:00:00 2001 From: Nityananda Gohain Date: Tue, 22 Aug 2023 16:48:44 +0530 Subject: [PATCH] fix: ts issue fixed with live tail (#3397) --- .../app/clickhouseReader/reader.go | 2 ++ .../app/logs/v3/query_builder.go | 17 +---------- .../app/logs/v3/query_builder_test.go | 30 ------------------- pkg/query-service/utils/format.go | 16 ++++++++++ pkg/query-service/utils/format_test.go | 29 ++++++++++++++++++ 5 files changed, 48 insertions(+), 46 deletions(-) diff --git a/pkg/query-service/app/clickhouseReader/reader.go b/pkg/query-service/app/clickhouseReader/reader.go index 1224e2838a..1d02035719 100644 --- a/pkg/query-service/app/clickhouseReader/reader.go +++ b/pkg/query-service/app/clickhouseReader/reader.go @@ -4600,6 +4600,8 @@ func (r *ClickHouseReader) GetSpanAttributeKeys(ctx context.Context) (map[string func (r *ClickHouseReader) LiveTailLogsV3(ctx context.Context, query string, timestampStart uint64, idStart string, client *v3.LogsLiveTailClient) { if timestampStart == 0 { timestampStart = uint64(time.Now().UnixNano()) + } else { + timestampStart = uint64(utils.GetEpochNanoSecs(int64(timestampStart))) } ticker := time.NewTicker(time.Duration(r.liveTailRefreshSeconds) * time.Second) diff --git a/pkg/query-service/app/logs/v3/query_builder.go b/pkg/query-service/app/logs/v3/query_builder.go index 8f00f5fbcd..48265745b4 100644 --- a/pkg/query-service/app/logs/v3/query_builder.go +++ b/pkg/query-service/app/logs/v3/query_builder.go @@ -2,7 +2,6 @@ package v3 import ( "fmt" - "math" "strings" "go.signoz.io/signoz/pkg/query-service/constants" @@ -165,20 +164,6 @@ func buildLogsTimeSeriesFilterQuery(fs *v3.FilterSet, groupBy []v3.AttributeKey) return queryString, nil } -// getZerosForEpochNano returns the number of zeros to be appended to the epoch time for converting it to nanoseconds -func getZerosForEpochNano(epoch int64) int64 { - count := 0 - if epoch == 0 { - count = 1 - } else { - for epoch != 0 { - epoch /= 10 - count++ - } - } - return int64(math.Pow(10, float64(19-count))) -} - func buildLogsQuery(panelType v3.PanelType, start, end, step int64, mq *v3.BuilderQuery, graphLimitQtype string, preferRPM bool) (string, error) { filterSubQuery, err := buildLogsTimeSeriesFilterQuery(mq.Filters, mq.GroupBy) @@ -187,7 +172,7 @@ func buildLogsQuery(panelType v3.PanelType, start, end, step int64, mq *v3.Build } // timerange will be sent in epoch millisecond - timeFilter := fmt.Sprintf("(timestamp >= %d AND timestamp <= %d)", start*getZerosForEpochNano(start), end*getZerosForEpochNano(end)) + timeFilter := fmt.Sprintf("(timestamp >= %d AND timestamp <= %d)", utils.GetEpochNanoSecs(start), utils.GetEpochNanoSecs(end)) selectLabels := getSelectLabels(mq.AggregateOperator, mq.GroupBy) diff --git a/pkg/query-service/app/logs/v3/query_builder_test.go b/pkg/query-service/app/logs/v3/query_builder_test.go index bf36b559d0..ebc62ec5e5 100644 --- a/pkg/query-service/app/logs/v3/query_builder_test.go +++ b/pkg/query-service/app/logs/v3/query_builder_test.go @@ -828,36 +828,6 @@ func TestBuildLogsQuery(t *testing.T) { } } -var testGetZerosForEpochNanoData = []struct { - Name string - Epoch int64 - Multiplier int64 - Result int64 -}{ - { - Name: "Test 1", - Epoch: 1680712080000, - Multiplier: 1000000, - Result: 1680712080000000000, - }, - { - Name: "Test 1", - Epoch: 1680712080000000000, - Multiplier: 1, - Result: 1680712080000000000, - }, -} - -func TestGetZerosForEpochNano(t *testing.T) { - for _, tt := range testGetZerosForEpochNanoData { - Convey("testGetZerosForEpochNanoData", t, func() { - multiplier := getZerosForEpochNano(tt.Epoch) - So(multiplier, ShouldEqual, tt.Multiplier) - So(tt.Epoch*multiplier, ShouldEqual, tt.Result) - }) - } -} - var testOrderBy = []struct { Name string PanelType v3.PanelType diff --git a/pkg/query-service/utils/format.go b/pkg/query-service/utils/format.go index 5d2ef618d4..1bfccd6858 100644 --- a/pkg/query-service/utils/format.go +++ b/pkg/query-service/utils/format.go @@ -2,6 +2,7 @@ package utils import ( "fmt" + "math" "reflect" "strconv" "strings" @@ -228,3 +229,18 @@ func getPointerValue(v interface{}) interface{} { return v } } + +// GetEpochNanoSecs takes epoch and returns it in ns +func GetEpochNanoSecs(epoch int64) int64 { + temp := epoch + count := 0 + if epoch == 0 { + count = 1 + } else { + for epoch != 0 { + epoch /= 10 + count++ + } + } + return temp * int64(math.Pow(10, float64(19-count))) +} diff --git a/pkg/query-service/utils/format_test.go b/pkg/query-service/utils/format_test.go index 594e544ce1..bd3c0eaf35 100644 --- a/pkg/query-service/utils/format_test.go +++ b/pkg/query-service/utils/format_test.go @@ -387,3 +387,32 @@ func TestClickHouseFormattedValue(t *testing.T) { }) } } + +var testGetEpochNanoSecsData = []struct { + Name string + Epoch int64 + Multiplier int64 + Result int64 +}{ + { + Name: "Test 1", + Epoch: 1680712080000, + Result: 1680712080000000000, + }, + { + Name: "Test 1", + Epoch: 1680712080000000000, + Result: 1680712080000000000, + }, +} + +func TestGetEpochNanoSecs(t *testing.T) { + for _, tt := range testGetEpochNanoSecsData { + t.Run(tt.Name, func(t *testing.T) { + got := GetEpochNanoSecs(tt.Epoch) + if !reflect.DeepEqual(got, tt.Result) { + t.Errorf("ClickHouseFormattedValue() = %v, want %v", got, tt.Result) + } + }) + } +}