Record query row count

This commit is contained in:
Edward Betts 2019-11-26 20:11:27 +00:00
parent 1badd35362
commit 48fbe6c8f5
3 changed files with 15 additions and 3 deletions

View file

@ -110,6 +110,7 @@ class WikidataQuery(Base):
status_code = Column(Integer) status_code = Column(Integer)
error_text = Column(String) error_text = Column(String)
query_template = Column(String) query_template = Column(String)
row_count = Column(Integer)
@property @property
def duration(self): def duration(self):

View file

@ -35,7 +35,11 @@ def run_from_template_with_cache(template_name, cache_name=None, **context):
query = render_template(template_name, **context) query = render_template(template_name, **context)
return run_query_with_cache(query, name=cache_name, query_template=template_name) return run_query_with_cache(query, name=cache_name, query_template=template_name)
def run_query(query, query_template=None): def run_query(query, **kwargs):
r, db_query = record_query(query, **kwargs)
return r
def record_query(query, query_template=None):
params = {'query': query, 'format': 'json'} params = {'query': query, 'format': 'json'}
start = datetime.utcnow() start = datetime.utcnow()
@ -58,7 +62,7 @@ def run_query(query, query_template=None):
database.session.commit() database.session.commit()
assert r.status_code == 200 assert r.status_code == 200
return r return r, db_query
def md5_query(query): def md5_query(query):
''' generate the md5 hexdigest of a SPARQL query ''' ''' generate the md5 hexdigest of a SPARQL query '''
@ -73,11 +77,13 @@ def run_query_with_cache(q, name=None, query_template=None):
if isinstance(from_cache, dict) and from_cache.get('query') == q: if isinstance(from_cache, dict) and from_cache.get('query') == q:
return from_cache['bindings'] return from_cache['bindings']
r = run_query(q, query_template=query_template) r, db_query = record_query(q, query_template=query_template)
bindings = r.json()['results']['bindings'] bindings = r.json()['results']['bindings']
json.dump({'query': q, 'bindings': bindings}, json.dump({'query': q, 'bindings': bindings},
open(filename, 'w'), indent=2) open(filename, 'w'), indent=2)
db_query.row_count = len(bindings)
database.session.commit()
return bindings return bindings
def format_time(row_time, row_timeprecision): def format_time(row_time, row_timeprecision):

View file

@ -18,6 +18,7 @@
<div class="col-1"></div> <div class="col-1"></div>
<div class="col-2">template</div> <div class="col-2">template</div>
<div class="col-2">when</div> <div class="col-2">when</div>
<div class="col-1">rows</div>
<div class="col-2">time</div> <div class="col-2">time</div>
<div class="col">options</div> <div class="col">options</div>
</div> </div>
@ -33,6 +34,10 @@
{{ obj.start_time.strftime('%Y %b %d %H:%M') }} {{ obj.start_time.strftime('%Y %b %d %H:%M') }}
</div> </div>
<div class="col-1">
{{ obj.row_count if obj.row_count is not none else '' }}
</div>
<div class="col-2"> <div class="col-2">
{% if obj.end_time %} {% if obj.end_time %}
{{ obj.display_seconds }}&nbsp;seconds {{ obj.display_seconds }}&nbsp;seconds