From 11b644c447ae22816e111dcb5557e5c1f33cd52a Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Sat, 7 Oct 2023 15:28:57 +0530 Subject: [PATCH] Remove invalid database names from dropdown suggestions 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 --- quarry/default_config.yaml | 6 ++++++ quarry/web/api.py | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/quarry/default_config.yaml b/quarry/default_config.yaml index e74d3ac..a5030f9 100644 --- a/quarry/default_config.yaml +++ b/quarry/default_config.yaml @@ -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' @@ -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 diff --git a/quarry/web/api.py b/quarry/web/api.py index dd54037..d5425ad 100644 --- a/quarry/web/api.py +++ b/quarry/web/api.py @@ -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