Skip to content

Commit

Permalink
fix: change time range to start and end query args (#27)
Browse files Browse the repository at this point in the history
* fix: change time range to start and end query args #22

* fix tests check and check zero time

---------

Co-authored-by: dmitryk-dk <[email protected]>
  • Loading branch information
Loori-R and dmitryk-dk authored Jun 25, 2024
1 parent 542fee9 commit acb116b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## tip

* BUGFIX: fix bug with displaying responses with a custom set of fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/23).
* BUGFIX: change time range limitation from `_time` in the expression to `start` and `end` query args. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/22).

## v0.2.1

Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (d *Datasource) query(ctx context.Context, _ backend.PluginContext, query b
settings.HTTPMethod = http.MethodPost
}

q.TimeRange = TimeRange(query.TimeRange)
reqURL, err := q.getQueryURL(d.settings.URL, settings.QueryParams)
if err != nil {
err = fmt.Errorf("failed to create request URL: %w", err)
Expand Down
18 changes: 18 additions & 0 deletions pkg/plugin/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"path"
"strconv"
"time"
)

const (
Expand All @@ -18,9 +19,16 @@ type Query struct {
Expr string `json:"expr"`
LegendFormat string `json:"legendFormat"`
MaxLines int `json:"maxLines"`
TimeRange TimeRange
url *url.URL
}

// TimeRange represents time range backend object
type TimeRange struct {
From time.Time
To time.Time
}

// GetQueryURL calculates step and clear expression from template variables,
// and after builds query url depends on query type
func (q *Query) getQueryURL(rawURL string, queryParams string) (string, error) {
Expand Down Expand Up @@ -56,8 +64,18 @@ func (q *Query) queryInstantURL(queryParams url.Values) string {
q.MaxLines = defaultMaxLines
}

now := time.Now()
if q.TimeRange.From.IsZero() {
q.TimeRange.From = now.Add(-time.Minute * 5)
}
if q.TimeRange.To.IsZero() {
q.TimeRange.To = now
}

values.Set("query", q.Expr)
values.Set("limit", strconv.Itoa(q.MaxLines))
values.Set("start", strconv.FormatInt(q.TimeRange.From.Unix(), 10))
values.Set("end", strconv.FormatInt(q.TimeRange.To.Unix(), 10))

q.url.RawQuery = values.Encode()
return q.url.String()
Expand Down
40 changes: 28 additions & 12 deletions pkg/plugin/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package plugin

import (
"testing"
"time"
)

func TestQuery_getQueryURL(t *testing.T) {
type fields struct {
RefID string
Expr string
MaxLines int
RefID string
Expr string
MaxLines int
TimeRange TimeRange
}
type args struct {
rawURL string
Expand All @@ -24,9 +26,10 @@ func TestQuery_getQueryURL(t *testing.T) {
{
name: "empty values",
fields: fields{
RefID: "1",
Expr: "",
MaxLines: 0,
RefID: "1",
Expr: "",
MaxLines: 0,
TimeRange: TimeRange{},
},
args: args{
rawURL: "",
Expand All @@ -41,12 +44,16 @@ func TestQuery_getQueryURL(t *testing.T) {
RefID: "1",
Expr: "",
MaxLines: 0,
TimeRange: TimeRange{
From: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
To: time.Date(2021, 1, 1, 1, 0, 0, 0, time.UTC),
},
},
args: args{
rawURL: "http://127.0.0.1:9428",
queryParams: "",
},
want: "http://127.0.0.1:9428/select/logsql/query?limit=1000&query=",
want: "http://127.0.0.1:9428/select/logsql/query?end=1609462800&limit=1000&query=&start=1609459200",
wantErr: false,
},
{
Expand All @@ -55,12 +62,16 @@ func TestQuery_getQueryURL(t *testing.T) {
RefID: "1",
Expr: "_time:1s",
MaxLines: 10,
TimeRange: TimeRange{
From: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
To: time.Date(2021, 1, 1, 1, 0, 0, 0, time.UTC),
},
},
args: args{
rawURL: "http://127.0.0.1:9428",
queryParams: "",
},
want: "http://127.0.0.1:9428/select/logsql/query?limit=10&query=_time%3A1s",
want: "http://127.0.0.1:9428/select/logsql/query?end=1609462800&limit=10&query=_time%3A1s&start=1609459200",
wantErr: false,
},
{
Expand All @@ -69,21 +80,26 @@ func TestQuery_getQueryURL(t *testing.T) {
RefID: "1",
Expr: "_time:1s and syslog",
MaxLines: 10,
TimeRange: TimeRange{
From: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
To: time.Date(2021, 1, 1, 1, 0, 0, 0, time.UTC),
},
},
args: args{
rawURL: "http://127.0.0.1:9428",
queryParams: "a=1&b=2",
},
want: "http://127.0.0.1:9428/select/logsql/query?a=1&b=2&limit=10&query=_time%3A1s+and+syslog",
want: "http://127.0.0.1:9428/select/logsql/query?a=1&b=2&end=1609462800&limit=10&query=_time%3A1s+and+syslog&start=1609459200",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := &Query{
RefID: tt.fields.RefID,
Expr: tt.fields.Expr,
MaxLines: tt.fields.MaxLines,
RefID: tt.fields.RefID,
Expr: tt.fields.Expr,
MaxLines: tt.fields.MaxLines,
TimeRange: tt.fields.TimeRange,
}
got, err := q.getQueryURL(tt.args.rawURL, tt.args.queryParams)
if (err != nil) != tt.wantErr {
Expand Down
5 changes: 0 additions & 5 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export class VictoriaLogsDatasource

query(request: DataQueryRequest<Query>): Observable<DataQueryResponse> {
const queries = request.targets.filter(q => q.expr).map((q) => {
// include time range in query if not already present
if (!/_time/.test(q.expr)) {
const timerange = `_time:[${request.range.from.toISOString()}, ${request.range.to.toISOString()}]`
q.expr = `${timerange} AND (${q.expr})`;
}
return { ...q, maxLines: q.maxLines ?? this.maxLines }
});

Expand Down

0 comments on commit acb116b

Please sign in to comment.