-
Notifications
You must be signed in to change notification settings - Fork 12
Sync API client from cli@40cd697 #338
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package planetscale | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "path" | ||
| ) | ||
|
|
||
| // QueryTag is a tag key observed on branch queries (sqlcommenter / system). | ||
| // ID is the dimension key used by the API (e.g. "Sapp", "Busername"). | ||
| // Name is the friendly key users see in the app (e.g. "app", "username"). | ||
| // Source is "sql" or "system". | ||
| type QueryTag struct { | ||
| ID string `json:"id"` | ||
| Name string `json:"name"` | ||
| Source string `json:"source"` | ||
| QueryCount int64 `json:"query_count"` | ||
| Values []QueryTagValue `json:"values"` | ||
| } | ||
|
|
||
| // QueryTagValue is one observed value for a tag key. | ||
| type QueryTagValue struct { | ||
| Name string `json:"name"` | ||
| QueryCount int64 `json:"query_count"` | ||
| Kind string `json:"kind"` | ||
| } | ||
|
|
||
| // TagSummary is query statistics grouped by one or more tag dimensions. | ||
| type TagSummary struct { | ||
| Dimensions map[string]string `json:"dimensions"` | ||
| QueryCount int64 `json:"query_count"` | ||
| ErrorCount int64 `json:"error_count"` | ||
| Tables []string `json:"tables"` | ||
| SumRowsRead int64 `json:"sum_rows_read"` | ||
| SumRowsReturned int64 `json:"sum_rows_returned"` | ||
| SumRowsAffected int64 `json:"sum_rows_affected"` | ||
| RowsReadPerReturned float64 `json:"rows_read_per_returned"` | ||
| SumTotalDurationMillis float64 `json:"sum_total_duration_millis"` | ||
| SumTotalDurationPct float64 `json:"sum_total_duration_percent"` | ||
| SumCPUDurationMillis float64 `json:"sum_cpu_duration_millis"` | ||
| SumIODurationMillis float64 `json:"sum_io_duration_millis"` | ||
| LastRunAt string `json:"last_run_at"` | ||
| TimePerQuery float64 `json:"time_per_query"` | ||
| P50Latency float64 `json:"p50_latency"` | ||
| P99Latency float64 `json:"p99_latency"` | ||
| MaxLatency float64 `json:"max_latency"` | ||
| } | ||
|
|
||
| // ListQueryTagsRequest lists tag keys for a branch. | ||
| type ListQueryTagsRequest struct { | ||
| Organization string | ||
| Database string | ||
| Branch string | ||
| } | ||
|
|
||
| // GetQueryTagRequest retrieves one tag key and its values. | ||
| // Tag is the dimension id (e.g. "Sapp"), not the display name. | ||
| type GetQueryTagRequest struct { | ||
| Organization string | ||
| Database string | ||
| Branch string | ||
| Tag string | ||
| } | ||
|
|
||
| // ListTagSummariesRequest lists query stats grouped by tag dimensions. | ||
| // Tags are dimension ids from the tags list endpoint (e.g. "Sapp"). | ||
| type ListTagSummariesRequest struct { | ||
| Organization string | ||
| Database string | ||
| Branch string | ||
| Tags []string | ||
| } | ||
|
|
||
| type queryTagsResponse struct { | ||
| Data []*QueryTag `json:"data"` | ||
| } | ||
|
|
||
| type tagSummariesResponse struct { | ||
| Data []*TagSummary `json:"data"` | ||
| } | ||
|
|
||
| // WithTags sets the tags[] query parameters (dimension ids for summaries). | ||
| func WithTags(tags []string) ListOption { | ||
| return func(opt *ListOptions) error { | ||
| for _, tag := range tags { | ||
| if tag != "" { | ||
| opt.URLValues.Add("tags[]", tag) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithFingerprint sets the fingerprint query parameter. | ||
| func WithFingerprint(fingerprint string) ListOption { | ||
| return func(opt *ListOptions) error { | ||
| if fingerprint != "" { | ||
| opt.URLValues.Set("fingerprint", fingerprint) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithKeyspace sets the keyspace query parameter. | ||
| func WithKeyspace(keyspace string) ListOption { | ||
| return func(opt *ListOptions) error { | ||
| if keyspace != "" { | ||
| opt.URLValues.Set("keyspace", keyspace) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func (s *queryInsightsService) ListTags(ctx context.Context, request *ListQueryTagsRequest, opts ...ListOption) ([]*QueryTag, error) { | ||
| listOpts := defaultListOptions(opts...) | ||
|
|
||
| req, err := s.client.newRequest(http.MethodGet, insightsTagsAPIPath(request.Organization, request.Database, request.Branch), nil, WithQueryParams(*listOpts.URLValues)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resp := &queryTagsResponse{} | ||
| if err := s.client.do(ctx, req, &resp); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp.Data, nil | ||
| } | ||
|
|
||
| func (s *queryInsightsService) GetTag(ctx context.Context, request *GetQueryTagRequest, opts ...ListOption) (*QueryTag, error) { | ||
| listOpts := defaultListOptions(opts...) | ||
|
|
||
| req, err := s.client.newRequest(http.MethodGet, insightsTagAPIPath(request.Organization, request.Database, request.Branch, request.Tag), nil, WithQueryParams(*listOpts.URLValues)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| tag := &QueryTag{} | ||
| if err := s.client.do(ctx, req, &tag); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return tag, nil | ||
| } | ||
|
|
||
| func (s *queryInsightsService) ListTagSummaries(ctx context.Context, request *ListTagSummariesRequest, opts ...ListOption) ([]*TagSummary, error) { | ||
| opts = append([]ListOption{WithTags(request.Tags)}, opts...) | ||
| listOpts := defaultListOptions(opts...) | ||
|
|
||
| req, err := s.client.newRequest(http.MethodGet, insightsTagSummariesAPIPath(request.Organization, request.Database, request.Branch), nil, WithQueryParams(*listOpts.URLValues)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resp := &tagSummariesResponse{} | ||
| if err := s.client.do(ctx, req, &resp); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp.Data, nil | ||
| } | ||
|
|
||
| func insightsTagsAPIPath(org, db, branch string) string { | ||
| return path.Join(insightsAPIPath(org, db, branch), "tags") | ||
| } | ||
|
|
||
| func insightsTagAPIPath(org, db, branch, tag string) string { | ||
| return path.Join(insightsTagsAPIPath(org, db, branch), tag) | ||
| } | ||
|
|
||
| func insightsTagSummariesAPIPath(org, db, branch string) string { | ||
| return path.Join(insightsTagsAPIPath(org, db, branch), "summaries") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.