-
Notifications
You must be signed in to change notification settings - Fork 73
Experimental: Add query type definition and schemas #897
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 1 commit
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
8781f2f
add query type support
ryantxu 0ec8629
move values
ryantxu 556b816
no refs
ryantxu fa45a90
add k8s placeholder
ryantxu 645173e
parallel k8s
ryantxu ff55c53
Merge remote-tracking branch 'origin/main' into query-type-handler
ryantxu f6bb323
now with examples
ryantxu e2341d6
ok with dataframe now
ryantxu 992463d
ok with dataframe now
ryantxu 58b32ee
no versions
ryantxu d791fc9
add apiVersion
ryantxu 0d526d8
Merge remote-tracking branch 'origin/main' into query-type-handler
ryantxu 4ae1f0c
add settings example
ryantxu b9382d8
Merge remote-tracking branch 'origin/main' into query-type-handler
ryantxu 238f7c9
fix build
ryantxu 7ed252c
Merge remote-tracking branch 'origin/main' into query-type-handler
ryantxu 1fe487a
multiple discriminators
ryantxu f5a35b3
difference between save and post
ryantxu dc41e8c
rename to spec
ryantxu 1202776
rename to spec
ryantxu 10b145d
more reference stuff
ryantxu e854402
more reference stuff
ryantxu 074b8f8
more reference stuff
ryantxu c673243
with definitions
ryantxu 940bc8e
with definitions
ryantxu 5c2de27
cleanup
ryantxu 64c06c2
cleanup
ryantxu 36b841c
use kube-openapi
ryantxu 2c8c4ad
fix error state
ryantxu 2b78392
refactor (again)
ryantxu e717941
refactor (again)
ryantxu d9f700e
now with panel schema
ryantxu ee12be2
now with panel schema
ryantxu 77f2055
now with dataFrame type
ryantxu f8eb48f
now with dataFrame type
ryantxu 8f647ba
lint
ryantxu 9e14771
refactor
ryantxu 29beda6
refactor
ryantxu a09ab12
another constructor
ryantxu 81d51b3
merge main
ryantxu 2151c09
update query parser
ryantxu 5af1ec4
update query parser
ryantxu 1e64757
merge main
ryantxu f18d1cb
use real type for JSONSchema object
ryantxu ff38e88
use real type for JSONSchema object
ryantxu 34c635a
use real type for JSONSchema object
ryantxu 0847e1d
add spec definition
ryantxu 8482109
more specs
ryantxu 8dbdba0
more specs
ryantxu bb0cd98
lint
ryantxu 2b3cc45
now with unstructured
ryantxu 0b0e1ea
less verbose
ryantxu a804614
now with unstructured
ryantxu 424be9a
now with unstructured
ryantxu 3a97c12
more copy methods
ryantxu f8336e6
fix lint remove parser
ryantxu 2a7ba03
fix lint
ryantxu 2ec1471
panel cleanup
ryantxu 0237a40
panel cleanup
ryantxu d35e870
inline timerange
ryantxu a7986a7
more deepcopy
ryantxu 71374e7
hopefully last big package rename
ryantxu d2c2e90
loading frame types
ryantxu 0e568d6
loading frame types
ryantxu e7e1b68
fix test
ryantxu 81d7990
remove unused settings configs
ryantxu 0fe8c9c
move panel to schema builder
ryantxu bd58a3b
Merge remote-tracking branch 'origin/main' into query-type-handler
ryantxu e9b643e
use k8s style paths
ryantxu c52d81c
use codegen
ryantxu 91df7c5
use codegen
ryantxu 66438c4
lint
ryantxu 557e208
rename to data
ryantxu 283c5df
update readme
ryantxu 782e1f5
update headers
ryantxu 342bf4e
update headers
ryantxu 0fd38ec
Merge remote-tracking branch 'origin/main' into query-type-handler
ryantxu 86b43fa
move package
ryantxu 95b4050
move package
ryantxu 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package v0alpha1 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/backend" | ||
"github.com/grafana/grafana-plugin-sdk-go/data/utils/jsoniter" | ||
) | ||
|
||
type QueryDataClient interface { | ||
QueryData(ctx context.Context, req QueryDataRequest, headers ...string) (int, *backend.QueryDataResponse, error) | ||
} | ||
|
||
type simpleHTTPClient struct { | ||
url string | ||
client *http.Client | ||
headers []string | ||
} | ||
|
||
func NewQueryDataClient(url string, client *http.Client, headers ...string) QueryDataClient { | ||
if client == nil { | ||
client = http.DefaultClient | ||
} | ||
return &simpleHTTPClient{ | ||
url: url, | ||
client: client, | ||
headers: headers, | ||
} | ||
} | ||
|
||
func (c *simpleHTTPClient) QueryData(ctx context.Context, query QueryDataRequest, headers ...string) (int, *backend.QueryDataResponse, error) { | ||
body, err := json.Marshal(query) | ||
if err != nil { | ||
return http.StatusBadRequest, nil, err | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return http.StatusBadRequest, nil, err | ||
} | ||
headers = append(c.headers, headers...) | ||
if (len(headers) % 2) != 0 { | ||
return http.StatusBadRequest, nil, fmt.Errorf("headers must be in pairs of two") | ||
} | ||
for i := 0; i < len(headers); i += 2 { | ||
req.Header.Set(headers[i], headers[i+1]) | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
rsp, err := c.client.Do(req) | ||
if err != nil { | ||
return rsp.StatusCode, nil, err | ||
} | ||
defer rsp.Body.Close() | ||
|
||
qdr := &backend.QueryDataResponse{} | ||
iter, err := jsoniter.Parse(jsoniter.ConfigCompatibleWithStandardLibrary, rsp.Body, 1024*10) | ||
if err == nil { | ||
err = iter.ReadVal(qdr) | ||
} | ||
return rsp.StatusCode, qdr, err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package v0alpha1_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/apis/data/v0alpha1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestQueryClient(t *testing.T) { | ||
t.Skip() | ||
|
||
client := v0alpha1.NewQueryDataClient("http://localhost:3000/api/ds/query", nil, | ||
"Authorization", "Bearer YOURKEYHERE", | ||
) | ||
body := `{ | ||
"from": "", | ||
"to": "", | ||
"queries": [ | ||
{ | ||
"refId": "X", | ||
"scenarioId": "csv_content", | ||
"datasource": { | ||
"type": "grafana-testdata-datasource", | ||
"uid": "PD8C576611E62080A" | ||
}, | ||
"csvContent": "a,b,c\n1,hello,true", | ||
"hide": true | ||
} | ||
] | ||
}` | ||
qdr := v0alpha1.QueryDataRequest{} | ||
err := json.Unmarshal([]byte(body), &qdr) | ||
require.NoError(t, err) | ||
|
||
code, rsp, err := client.QueryData(context.Background(), qdr) | ||
require.NoError(t, err) | ||
require.Equal(t, http.StatusOK, code) | ||
|
||
r, ok := rsp.Responses["X"] | ||
require.True(t, ok) | ||
|
||
for _, frame := range r.Frames { | ||
txt, err := frame.StringTable(20, 10) | ||
require.NoError(t, err) | ||
fmt.Printf("%s\n", txt) | ||
} | ||
} |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions
8
.../sdkapi/v0alpha1/zz_generated.deepcopy.go → apis/data/v0alpha1/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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.
I am not sure I understand the purpose of this new client? Is it an example?
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 is what alert ruler would call (currently we say "call /ds/query" but there is no typed client).
It is also the abstraction used inside the query service that will execute query requests. We have a few implementations:
We don't necessarily need the implementation here (could be internal) but then not sure how to best have alert ruler use the client.
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 may make sense in its own package (and perhaps with a generic for the query type) but given this is all in experimental/v0alpha I don't think it matters too much yet