From 6a8103d10e431a5ef9fe7b0f058765a8a9629682 Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Thu, 9 May 2024 13:56:19 +0800 Subject: [PATCH] fix: fix parsing timestamp --- controller/rest/restful.go | 4 ++-- controller/ws/tmq/tmq_test.go | 18 ++++++++++++++---- plugin/prometheus/process.go | 4 ++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/controller/rest/restful.go b/controller/rest/restful.go index c0cd48f7..5a683df3 100644 --- a/controller/rest/restful.go +++ b/controller/rest/restful.go @@ -119,9 +119,9 @@ func (ctl *Restful) sql(c *gin.Context) { timeBuffer = timeBuffer[:0] switch precision { case common.PrecisionMilliSecond: // milli-second - timeBuffer = time.Unix(0, ts*1e6).In(location).AppendFormat(timeBuffer, layout.LayoutMillSecond) + timeBuffer = time.Unix(ts/1e3, (ts%1e3)*1e6).In(location).AppendFormat(timeBuffer, layout.LayoutMillSecond) case common.PrecisionMicroSecond: // micro-second - timeBuffer = time.Unix(0, ts*1e3).In(location).AppendFormat(timeBuffer, layout.LayoutMicroSecond) + timeBuffer = time.Unix(ts/1e6, (ts%1e6)*1e3).In(location).AppendFormat(timeBuffer, layout.LayoutMicroSecond) case common.PrecisionNanoSecond: // nano-second timeBuffer = time.Unix(0, ts).In(location).AppendFormat(timeBuffer, layout.LayoutNanoSecond) default: diff --git a/controller/ws/tmq/tmq_test.go b/controller/ws/tmq/tmq_test.go index 83b4d632..42a3db88 100644 --- a/controller/ws/tmq/tmq_test.go +++ b/controller/ws/tmq/tmq_test.go @@ -667,7 +667,7 @@ func TestMeta(t *testing.T) { var resp wstool.TDEngineRestfulResp err = jsoniter.Unmarshal(w.Body.Bytes(), &resp) assert.NoError(t, err) - assert.Equal(t, [][]driver.Value{ + expect := [][]driver.Value{ {"ts", "TIMESTAMP", float64(8), ""}, {"c1", "BOOL", float64(1), ""}, {"c2", "TINYINT", float64(1), ""}, @@ -696,7 +696,12 @@ func TestMeta(t *testing.T) { {"tc11", "DOUBLE", float64(8), "TAG"}, {"tc12", "VARCHAR", float64(20), "TAG"}, {"tc13", "NCHAR", float64(20), "TAG"}, - }, resp.Data) + } + for index, values := range expect { + for i := 0; i < 4; i++ { + assert.Equal(t, values[i], resp.Data[index][i]) + } + } status = AfterTMQCommit b, _ := json.Marshal(&TMQCommitReq{ ReqID: 3, @@ -850,7 +855,7 @@ func TestMeta(t *testing.T) { var resp wstool.TDEngineRestfulResp err = jsoniter.Unmarshal(w.Body.Bytes(), &resp) assert.NoError(t, err) - assert.Equal(t, [][]driver.Value{ + expect := [][]driver.Value{ {"ts", "TIMESTAMP", float64(8), ""}, {"c1", "BOOL", float64(1), ""}, {"c2", "TINYINT", float64(1), ""}, @@ -879,7 +884,12 @@ func TestMeta(t *testing.T) { {"tc11", "DOUBLE", float64(8), "TAG"}, {"tc12", "VARCHAR", float64(20), "TAG"}, {"tc13", "NCHAR", float64(20), "TAG"}, - }, resp.Data) + } + for index, values := range expect { + for i := 0; i < 4; i++ { + assert.Equal(t, values[i], resp.Data[index][i]) + } + } time.Sleep(time.Second * 3) w = httptest.NewRecorder() body = strings.NewReader("drop topic if exists test_tmq_meta_ws_topic") diff --git a/plugin/prometheus/process.go b/plugin/prometheus/process.go index 2fe25a55..f78db45a 100644 --- a/plugin/prometheus/process.go +++ b/plugin/prometheus/process.go @@ -123,7 +123,7 @@ func generateWriteSql(timeseries []prompbWrite.TimeSeries, sql *bytes.Buffer, tt for _, sample := range timeseries[i].Samples { sql.WriteString("('") timeBuffer.Reset() - timeBuffer.B = time.Unix(0, sample.Timestamp*1e6).UTC().AppendFormat(timeBuffer.B, time.RFC3339Nano) + timeBuffer.B = time.Unix(sample.Timestamp/1e3, (sample.Timestamp%1e3)*1e6).UTC().AppendFormat(timeBuffer.B, time.RFC3339Nano) sql.WriteString(bytesutil.ToUnsafeString(timeBuffer.B)) sql.WriteString("',") if math.IsNaN(sample.Value) { @@ -262,7 +262,7 @@ func generateReadSql(query *prompb.Query) (string, error) { } func ms2Time(ts int64) string { - return time.Unix(0, ts*1e6).UTC().Format(time.RFC3339Nano) + return time.Unix(ts/1e3, (ts%1e3)*1e6).UTC().Format(time.RFC3339Nano) } func escapeString(s string) string {