-
Notifications
You must be signed in to change notification settings - Fork 12
Bug fixes / context refactor #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48a86ac
add timeout; return no results if no results were found
kminehart 5b91296
remove github.com/pkg/errors in favor of stdlib
kminehart 00d7688
add timeout function to macros_test.go
kminehart b050949
use settings function in interface, let QueryContext handle context c…
kminehart d8ad3af
remove query_test.go
kminehart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
package sqlds | ||
|
||
import "github.com/pkg/errors" | ||
import "errors" | ||
|
||
var ( | ||
// ErrorBadDatasource ... | ||
// ErrorBadDatasource is returned if the data source could not be asserted to the correct type (this should basically never happen?) | ||
ErrorBadDatasource = errors.New("type assertion to datasource failed") | ||
// ErrorJSON is returned when json.Unmarshal fails | ||
ErrorJSON = errors.New("error unmarshaling query JSON the Query Model") | ||
// ErrorQuery is returned when the query could not complete / execute | ||
ErrorQuery = errors.New("error querying the database") | ||
// ErrorTimeout is returned if the query has timed out | ||
ErrorTimeout = errors.New("query timeout exceeded") | ||
// ErrorNoResults is returned if there were no results returned | ||
ErrorNoResults = errors.New("no results returned from query") | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
package sqlds | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
package sqlds | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/data" | ||
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// FormatQueryOption defines how the user has chosen to represent the data | ||
|
@@ -91,21 +92,21 @@ func getErrorFrameFromQuery(query *Query) data.Frames { | |
} | ||
|
||
// query sends the query to the sql.DB and converts the rows to a dataframe. | ||
func query(db *sql.DB, converters []sqlutil.Converter, fillMode *data.FillMissing, query *Query) (data.Frames, error) { | ||
func query(ctx context.Context, db *sql.DB, converters []sqlutil.Converter, fillMode *data.FillMissing, query *Query) (data.Frames, error) { | ||
// Query the rows from the database | ||
rows, err := db.Query(query.RawSQL) | ||
rows, err := db.QueryContext(ctx, query.RawSQL) | ||
if err != nil { | ||
return getErrorFrameFromQuery(query), errors.Wrap(ErrorQuery, err.Error()) | ||
return getErrorFrameFromQuery(query), fmt.Errorf("%w: %s", ErrorQuery, err.Error()) | ||
} | ||
|
||
// Check for an error response | ||
if err := rows.Err(); err != nil { | ||
if err == sql.ErrNoRows { | ||
// Should we even response with an error here? | ||
// The panel will simply show "no data" | ||
return getErrorFrameFromQuery(query), errors.WithMessage(err, "No results from query") | ||
return getErrorFrameFromQuery(query), fmt.Errorf("%s: %w", "No results from query", err) | ||
} | ||
return getErrorFrameFromQuery(query), errors.WithMessage(err, "Error response from database") | ||
return getErrorFrameFromQuery(query), fmt.Errorf("%s: %w", "Error response from database", err) | ||
} | ||
|
||
defer func() { | ||
|
@@ -117,7 +118,7 @@ func query(db *sql.DB, converters []sqlutil.Converter, fillMode *data.FillMissin | |
// Convert the response to frames | ||
res, err := getFrames(rows, -1, converters, fillMode, query) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "Could not process SQL results") | ||
return getErrorFrameFromQuery(query), fmt.Errorf("%w: %s", err, "Could not process SQL results") | ||
} | ||
|
||
return res, nil | ||
|
@@ -139,6 +140,16 @@ func getFrames(rows *sql.Rows, limit int64, converters []sqlutil.Converter, fill | |
return data.Frames{frame}, nil | ||
} | ||
|
||
count, err := frame.RowLen() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if count == 0 { | ||
return nil, ErrorNoResults | ||
} | ||
Comment on lines
+143
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'd be also great to have some test for this, since this is the fix of #16 |
||
|
||
if frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong { | ||
frame, err := data.LongToWide(frame, fillMode) | ||
if err != nil { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it' a bit artificial but can we move lines 93:116 to a
prepareQuery
method? That way we can easily add tests for new/existing code without having to mock the query execution.