-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat(prod-queries): Implement backend for prod queries #4398
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6e0567e
Add api endpoint
davidtsuk 38df7bf
Add backend
davidtsuk cd44984
Remove accidental code
davidtsuk 55eda8a
Add prod queries backend
davidtsuk 7a336b5
Fix merge issues
davidtsuk 6b56677
Add frontend stuff
davidtsuk a8a35ea
Add frontend stuff
davidtsuk 6e7ac53
Fix issues
davidtsuk 050a128
Add tests
davidtsuk 6fc749b
Merge branch 'master' into david/feat/prod-query-api
davidtsuk 8b6349d
Fix test issues
davidtsuk cb95d35
Add test
davidtsuk 9a5da48
Fix error message
davidtsuk e7b0e2b
Fix tests
davidtsuk 4a785de
Update failing test
davidtsuk 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 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 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 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
Empty file.
This file contains 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,51 @@ | ||
from typing import Any, Dict | ||
|
||
from flask import Response | ||
|
||
from snuba import settings | ||
from snuba.admin.audit_log.query import audit_log | ||
from snuba.clickhouse.query_dsl.accessors import get_object_ids_in_query_ast | ||
from snuba.datasets.dataset import Dataset | ||
from snuba.datasets.factory import get_dataset | ||
from snuba.query.exceptions import InvalidQueryException | ||
from snuba.query.query_settings import HTTPQuerySettings | ||
from snuba.query.snql.parser import parse_snql_query | ||
from snuba.request.schema import RequestSchema | ||
from snuba.utils.metrics.timer import Timer | ||
from snuba.web.views import dataset_query | ||
|
||
|
||
def run_snql_query(body: Dict[str, Any], user: str) -> Response: | ||
""" | ||
Validates, audit logs, and executes given query. | ||
""" | ||
|
||
@audit_log | ||
def run_query_with_audit(query: str, user: str) -> Response: | ||
dataset = get_dataset(body.pop("dataset")) | ||
body["dry_run"] = True | ||
response = dataset_query(dataset, body, Timer("admin")) | ||
if response.status_code != 200: | ||
return response | ||
|
||
body["dry_run"] = False | ||
_validate_projects_in_query(body, dataset) | ||
return dataset_query(dataset, body, Timer("admin")) | ||
|
||
return run_query_with_audit(body["query"], user) | ||
|
||
|
||
def _validate_projects_in_query(body: Dict[str, Any], dataset: Dataset) -> None: | ||
request_parts = RequestSchema.build(HTTPQuerySettings).validate(body) | ||
query = parse_snql_query(request_parts.query["query"], dataset)[0] | ||
project_ids = get_object_ids_in_query_ast(query, "project_id") | ||
if project_ids is None: | ||
raise InvalidQueryException("Missing project ID") | ||
|
||
disallowed_project_ids = project_ids.difference( | ||
set(settings.ADMIN_ALLOWED_PROD_PROJECTS) | ||
) | ||
if len(disallowed_project_ids) > 0: | ||
raise InvalidQueryException( | ||
f"Cannot access the following project ids: {disallowed_project_ids}" | ||
) |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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,25 @@ | ||
const executeActionsStyle = { | ||
display: "flex", | ||
justifyContent: "space-between", | ||
marginTop: 8, | ||
}; | ||
|
||
const executeButtonStyle = { | ||
height: 30, | ||
border: 0, | ||
padding: "4px 20px", | ||
}; | ||
|
||
const selectStyle = { | ||
marginRight: 8, | ||
height: 30, | ||
}; | ||
|
||
let collapsibleStyle = { listStyleType: "none", fontFamily: "Monaco" }; | ||
|
||
export { | ||
executeActionsStyle, | ||
executeButtonStyle, | ||
selectStyle, | ||
collapsibleStyle, | ||
}; |
This file contains 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,17 @@ | ||
type SnQLRequest = { | ||
dataset: string; | ||
query: string; | ||
}; | ||
|
||
type QueryResult = { | ||
input_query?: string; | ||
columns: [string]; | ||
rows: [[string]]; | ||
}; | ||
|
||
type QueryResultColumnMeta = { | ||
name: string; | ||
type: string; | ||
}; | ||
|
||
export { SnQLRequest, QueryResult, QueryResultColumnMeta }; |
This file contains 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
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.
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.
"Could not convert SnQL" is probably not the right error message.
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.
lol good catch