Skip to content

Commit

Permalink
Remove invalid database names from dropdown suggestions
Browse files Browse the repository at this point in the history
This is done by only including dbs against which successful queries have been run in the past.

Also add a Cache-Control header is response so the result is cached in the browser for one hour or till a hard reload is done.

Bug: T289943
  • Loading branch information
siddharthvp committed Oct 7, 2023
1 parent 75a35e6 commit 11b644c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 6 additions & 0 deletions quarry/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ OAUTH_SECRET_TOKEN: "83ed4ac72176286f5c7798459f0a6b16a96853c4"
SECRET_KEY: 'aaarggghhpleaserescuemeiamstuckinarandomnumbergeneratorfactory'
DEBUG: True
DB_HOST: 'db'
DB_PORT: 3306
DB_NAME: 'quarry'
DB_USER: 'quarry'
DB_PASSWORD: 'quarry'
Expand All @@ -31,6 +32,11 @@ QUERY_RESULTS_PER_PAGE: 50
KILLER_LOG_PATH: 'killer.log'
MAINTENANCE_MSG: 'This is your local development environment.'

TOOLS_DB_HOST: 'tools.db.svc.wikimedia.cloud'
TOOLS_DB_PORT: 3306
TOOLS_DB_USER: ''
TOOLS_DB_PASSWORD: ''

# https://flask-caching.readthedocs.io/en/latest/#configuring-flask-caching
CACHE_TYPE: 'RedisCache'
CACHE_DEFAULT_TIMEOUT: 30
Expand Down
21 changes: 14 additions & 7 deletions quarry/web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,28 @@ def pref_set(key, value) -> Union[Tuple[str, int], Tuple[Response, int]]:
@api_blueprint.route("/api/dbs")
def get_dbs() -> Response:
known_dbs = (
g.conn.session.query(QueryRevision.query_database).distinct().all()
g.conn.session.query(QueryRevision.query_database)
.join(QueryRun, QueryRevision.id == QueryRun.query_rev_id)
.filter(QueryRun.extra_info.notlike('{"error%'))
.filter(QueryRevision.query_database.isnot(None))
.distinct()
.all()
)
return Response(
response = Response(
json.dumps(
{
"dbs": list(
set(
db_result[-1].strip().rstrip(";")
db_result[-1].strip()
for db_result in known_dbs
# the db data might be NULL, empty strings or spaces+tabs only so this helps a bit to show only
# likely names
if db_result[-1] and db_result[-1].strip()
if db_result[-1]
)
)
}
},
# Remove spaces to reduce response size
separators=(",", ":"),
),
mimetype="application/json",
)
response.headers["Cache-Control"] = "max-age=3600, public"
return response

0 comments on commit 11b644c

Please sign in to comment.