Skip to content

Commit

Permalink
Timestamps: Fix timestamp handling and release 1.2.1 (#64)
Browse files Browse the repository at this point in the history
* fixed timestamp parsing

* removed unnecessary mutliplier

* adapted tests

* queries should be made in UTC

* fixed tests not using utc

* added version upgrade

* update changelog
  • Loading branch information
svennergr authored Sep 12, 2022
1 parent a350aeb commit a0b105b
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## v1.2.1
### Bug fixes
- Fixed timestamps in the backend beeing handled wrong (#31)
- Fixed timestamps in the frontend being assumed as local, whereas they should be UTC (#21, #67)
## v1.2.0
### Features and enhancements
- Upgrade of `@grafana/data`, `@grafana/ui`, `@grafana/runtime`, `@grafana/toolkit` to 8.5.5 (#35, #41)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grafana-opensearch-datasource",
"version": "1.2.0",
"version": "1.2.1",
"description": "",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
2 changes: 1 addition & 1 deletion pkg/opensearch/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ const (

// PPL date time type formats
const (
pplTSFormat = "2006-01-02 15:04:05.000000"
pplTSFormat = "2006-01-02 15:04:05.999999"
pplDateFormat = "2006-01-02"
)
4 changes: 2 additions & 2 deletions pkg/opensearch/ppl_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var newPPLHandler = func(client es.Client, req *backend.QueryDataRequest) *pplHa
}

func (h *pplHandler) processQuery(q *Query) error {
from := h.req.Queries[0].TimeRange.From.Local().Format("2006-01-02 15:04:05")
to := h.req.Queries[0].TimeRange.To.Local().Format("2006-01-02 15:04:05")
from := h.req.Queries[0].TimeRange.From.UTC().Format("2006-01-02 15:04:05")
to := h.req.Queries[0].TimeRange.To.UTC().Format("2006-01-02 15:04:05")

builder := h.client.PPL()
builder.AddPPLQueryString(h.client.GetTimeField(), to, from, q.RawQuery)
Expand Down
3 changes: 2 additions & 1 deletion pkg/opensearch/ppl_response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (rp *pplResponseParser) addDatarow(frame *data.Frame, i int, datarow es.Dat
if err != nil {
return err
}
frame.Set(0, i, nullFloatToNullableTime(timestamp))

frame.Set(0, i, utils.NullFloatToNullableTime(timestamp))
if value.Valid {
frame.Set(1, i, &value.Float64)
} else {
Expand Down
12 changes: 6 additions & 6 deletions pkg/opensearch/ppl_response_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func TestPPLResponseParser(t *testing.T) {
frame := queryRes.Frames[0]
So(frame.Name, ShouldEqual, "testMetric")
So(frame.Rows(), ShouldEqual, 2)
So(floatAt(frame, 0, 0), ShouldEqual, 100000)
So(floatAt(frame, 0, 1), ShouldEqual, 200000)
So(floatAt(frame, 0, 0), ShouldEqual, 100)
So(floatAt(frame, 0, 1), ShouldEqual, 200)
So(floatAt(frame, 1, 0), ShouldEqual, 10)
So(floatAt(frame, 1, 1), ShouldEqual, 15)
})
Expand Down Expand Up @@ -77,8 +77,8 @@ func TestPPLResponseParser(t *testing.T) {
frame := queryRes.Frames[0]
So(frame.Name, ShouldEqual, "testMetric")
So(frame.Rows(), ShouldEqual, 2)
So(floatAt(frame, 0, 0), ShouldEqual, 100000)
So(floatAt(frame, 0, 1), ShouldEqual, 200000)
So(floatAt(frame, 0, 0), ShouldEqual, 100)
So(floatAt(frame, 0, 1), ShouldEqual, 200)
So(floatAt(frame, 1, 0), ShouldEqual, 20)
So(floatAt(frame, 1, 1), ShouldEqual, 25)
})
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestPPLResponseParser(t *testing.T) {
frame := queryRes.Frames[0]
So(frame.Name, ShouldEqual, "testMetric")
So(frame.Rows(), ShouldEqual, 1)
So(floatAt(frame, 0, 0), ShouldEqual, 100000)
So(floatAt(frame, 0, 0), ShouldEqual, 100)
So(floatAt(frame, 1, 0), ShouldEqual, 10)
})

Expand All @@ -156,7 +156,7 @@ func TestPPLResponseParser(t *testing.T) {
frame := queryRes.Frames[0]
So(frame.Name, ShouldEqual, "testMetric")
So(frame.Rows(), ShouldEqual, 1)
So(floatAt(frame, 0, 0), ShouldEqual, 100000)
So(floatAt(frame, 0, 0), ShouldEqual, 100)
So(floatAt(frame, 1, 0), ShouldEqual, 10)
})

Expand Down
17 changes: 2 additions & 15 deletions pkg/opensearch/response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package opensearch

import (
"errors"
"math"
"regexp"
"sort"
"strconv"
"strings"
"time"

simplejson "github.com/bitly/go-simplejson"
"github.com/grafana/grafana-plugin-sdk-go/backend"
Expand Down Expand Up @@ -636,23 +634,12 @@ func getErrorFromOpenSearchResponse(response *es.SearchResponse) error {
return err
}

func setFrameRow(frame *data.Frame, i int, key, value null.Float) {
frame.Set(0, i, nullFloatToNullableTime(key))
func setFrameRow(frame *data.Frame, i int, ntime null.Float, value null.Float) {
frame.Set(0, i, utils.NullFloatToNullableTime(ntime))

if value.Valid {
frame.Set(1, i, &value.Float64)
} else {
frame.Set(1, i, nil)
}
}

func nullFloatToNullableTime(ts null.Float) *time.Time {
if !ts.Valid {
return nil
}

sec, fract := math.Modf(ts.Float64)
nsec := int64(fract * float64(time.Second))
timestamp := time.Unix(int64(sec), nsec)
return &timestamp
}
11 changes: 11 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package utils

import (
"encoding/json"
"time"

"github.com/bitly/go-simplejson"
"github.com/grafana/opensearch-datasource/pkg/null"
)

func NewJsonFromAny(data interface{}) *simplejson.Json {
Expand All @@ -18,3 +20,12 @@ func NewRawJsonFromAny(data interface{}) []byte {
dataJsonRaw, _ := dataJson.MarshalJSON()
return dataJsonRaw
}

func NullFloatToNullableTime(ts null.Float) *time.Time {
if !ts.Valid {
return nil
}

timestamp := time.UnixMilli(int64(ts.Float64)).UTC()
return &timestamp
}
3 changes: 1 addition & 2 deletions src/OpenSearchResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import flatten from './dependencies/flatten';
import * as queryDef from './query_def';
import TableModel from './dependencies/table_model';
import {
dateTime,
DataQueryResponse,
DataFrame,
toDataFrame,
Expand Down Expand Up @@ -755,7 +754,7 @@ const getPPLDatapoints = (response: any): { datapoints: any; targetVal: any; inv
const datapoints = _.map(response.datarows, datarow => {
const newDatarow = _.clone(datarow);
const [timestamp] = newDatarow.splice(timeFieldIndex, 1);
newDatarow.push(dateTime(timestamp).unix() * 1000);
newDatarow.push(toUtc(timestamp).unix() * 1000);
return newDatarow;
});

Expand Down
2 changes: 1 addition & 1 deletion src/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ describe('OpenSearchDatasource', function(this: any) {
timezone: '',
app: CoreApp.Dashboard,
startTime: 0,
range: createTimeRange(dateTime([2015, 4, 30, 10]), dateTime([2015, 5, 1, 10])),
range: createTimeRange(toUtc([2015, 4, 30, 10]), toUtc([2015, 5, 1, 10])),
targets,
};

Expand Down
8 changes: 6 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,12 @@ export class OpenSearchDatasource extends DataSourceApi<OpenSearchQuery, OpenSea
for (const target of targets) {
let payload = this.createPPLQuery(target, options);

const rangeFrom = dateTime(options.range.from.valueOf()).format('YYYY-MM-DD HH:mm:ss');
const rangeTo = dateTime(options.range.to.valueOf()).format('YYYY-MM-DD HH:mm:ss');
const rangeFrom = dateTime(options.range.from.valueOf())
.utc()
.format('YYYY-MM-DD HH:mm:ss');
const rangeTo = dateTime(options.range.to.valueOf())
.utc()
.format('YYYY-MM-DD HH:mm:ss');
// Replace the range here for actual values.
payload = payload.replace(/\$timeTo/g, rangeTo);
payload = payload.replace(/\$timeFrom/g, rangeFrom);
Expand Down

0 comments on commit a0b105b

Please sign in to comment.