def results(key, update=False): cache_key = AdminQuery.cache_key_template.format(**{"id": key}) results = cache.get(cache_key) if results is None or update: engine = db.get_engine(current_app) connection = engine.connect() aqs = connection.execute( select([AdminQuery]).where(AdminQuery.id == key)) if aqs.returns_rows: aqs = [{key: value for (key, value) in o.items()} for o in aqs] connection.close() if len(aqs): query_text = f"{aqs[0]['query_text']};".replace( '\r\n', ' ').replace('\n', ' ').replace("%", "%%") query_name = f"{aqs[0]['name']}" headings, rows = AdminQuery.execute(query_text) results = headings, rows, query_name cache.set(cache_key, results, timeout=current_app. config['ADMIN_QUERY_CACHE_TIMEOUT']) else: results = [], [], "unknown" else: results = [], [], "unknown" return results
def schedule_queries(): # tasks called by celery beat are unfortunatelly outside of app context, so it needs to be added manually with create_app_celery().app_context(): from flask import current_app engine = db.get_engine(current_app) connection = engine.connect() aqs = connection.execute(select([models.AdminQuery]).where(models.AdminQuery.enable_autorun == True)) if aqs.returns_rows: aqs = [{key: value for (key, value) in o.items()} for o in aqs] for aq in aqs: run_admin_query.delay(aq['id'])
def admin_query_list_diagnostic(): engine = db.get_engine(current_app, models.AdminQuery.__bind_key__) connection = engine.connect() aqs = connection.execute( select([models.AdminQuery]).where( models.AdminQuery.type == models.AdminQueryTypeEnum.diagnostic ) ) if aqs.returns_rows: aqs = [{key: value for (key, value) in o.items()} for o in aqs] else: aqs = [] context = { 'data': aqs, 'title': 'Diagnostics' } return render_template('tracker_admin/admin-query-list.html', **context)
def execute(query): try: AdminQuery().validate_query_text(None, query) except ValueError: return [], [] else: engine = db.get_engine(current_app) connection = engine.connect() aqs = connection.execute(query) headings = aqs.keys() if aqs.returns_rows: rows = [{key: value for (key, value) in o.items()} for o in aqs] connection.close() else: rows = [] return headings, rows
def admin_query_list_statistic(): engine = db.get_engine(current_app) connection = engine.connect() aqs = connection.execute( select([models.AdminQuery]).where( models.AdminQuery.type == models.AdminQueryTypeEnum.statistic)) if aqs.returns_rows: aqs = [{key: value for (key, value) in o.items()} for o in aqs] else: aqs = [] context = { 'data': aqs, 'title': 'Statistics', 'keycloak': KeycloakServiceClient } return render_template('tracker_admin/admin-query-list.html', **context)