Skip to content

Commit

Permalink
fix: change time range to start and end query args #22
Browse files Browse the repository at this point in the history
  • Loading branch information
Loori-R committed Jun 18, 2024
1 parent 2f87098 commit 63424af
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## tip

* 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

* BUGFIX: change the `metrics` flag from `false` to `true` in `plugin.json` to ensure the plugin appears in the Grafana datasource selection list.
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
10 changes: 10 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 @@ -58,6 +66,8 @@ func (q *Query) queryInstantURL(queryParams url.Values) string {

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
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 63424af

Please sign in to comment.