Skip to content

Commit

Permalink
Feature/limited queries (#45)
Browse files Browse the repository at this point in the history
* Splitted classes, preparing for limited queries
* Added limits to query builder
* Fixed warning

Co-authored-by: Vsevolod <[email protected]>
Co-authored-by: Aleksandr Volochnev <[email protected]>
  • Loading branch information
3 people authored Dec 3, 2020
1 parent f2e8f40 commit 3a1663d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
24 changes: 5 additions & 19 deletions backend/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
type CassandraDatasource struct {
plugin.NetRPCUnsupportedPlugin
logger hclog.Logger
builder *QueryBuilder
session *gocql.Session
}

Expand Down Expand Up @@ -80,6 +81,9 @@ func (ds *CassandraDatasource) Query(ctx context.Context, tsdbReq *datasource.Da
func (ds *CassandraDatasource) MetricQuery(tsdbReq *datasource.DatasourceRequest, jsonQueries []*simplejson.Json, options map[string]string) (*datasource.DatasourceResponse, error) {
ds.logger.Debug(fmt.Sprintf("Query[0]: %v\n", jsonQueries[0]))

ds.logger.Debug(fmt.Sprintf("Timeframe from: %+v\n", tsdbReq.TimeRange.FromRaw))
ds.logger.Debug(fmt.Sprintf("Timeframe to: %+v\n", tsdbReq.TimeRange.ToRaw))

response := &datasource.DatasourceResponse{}

for _, queryData := range jsonQueries {
Expand All @@ -95,27 +99,9 @@ func (ds *CassandraDatasource) MetricQuery(tsdbReq *datasource.DatasourceRequest
var preparedQuery string

if queryData.Get("rawQuery").MustBool() {

preparedQuery = queryData.Get("target").MustString()

} else {

allowFiltering := ""
if queryData.Get("filtering").MustBool() {
ds.logger.Warn("Allow filtering enabled")
allowFiltering = " ALLOW FILTERING"
}
preparedQuery = fmt.Sprintf(
"SELECT %s, CAST(%s as double) FROM %s.%s WHERE %s = %s%s",
queryData.Get("columnTime").MustString(),
queryData.Get("columnValue").MustString(),
queryData.Get("keyspace").MustString(),
queryData.Get("table").MustString(),
queryData.Get("columnId").MustString(),
queryData.Get("valueId").MustString(),
allowFiltering,
)

preparedQuery = ds.builder.MetricQuery(queryData, tsdbReq.TimeRange.FromRaw, tsdbReq.TimeRange.ToRaw)
}

ds.logger.Debug(fmt.Sprintf("Executing CQL query: '%s' ...\n", preparedQuery))
Expand Down
33 changes: 33 additions & 0 deletions backend/query_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"

simplejson "github.com/bitly/go-simplejson"
)

type QueryBuilder struct{}

func (qb *QueryBuilder) MetricQuery(queryData *simplejson.Json, timeRangeFrom string, timeRangeTo string) string {
allowFiltering := ""
if queryData.Get("filtering").MustBool() {
allowFiltering = " ALLOW FILTERING"
}

preparedQuery := fmt.Sprintf(
"SELECT %s, CAST(%s as double) FROM %s.%s WHERE %s = %s AND %s >= '%s' AND %s <= '%s' %s",
queryData.Get("columnTime").MustString(),
queryData.Get("columnValue").MustString(),
queryData.Get("keyspace").MustString(),
queryData.Get("table").MustString(),
queryData.Get("columnId").MustString(),
queryData.Get("valueId").MustString(),
queryData.Get("columnTime").MustString(),
timeRangeFrom,
queryData.Get("columnTime").MustString(),
timeRangeTo,
allowFiltering,
)

return preparedQuery
}

0 comments on commit 3a1663d

Please sign in to comment.