Skip to content

Commit

Permalink
Measure time for query execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Darshan Prajapati committed Jan 30, 2024
1 parent 357f695 commit 13a9777
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
6 changes: 5 additions & 1 deletion xql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

if __name__ == '__main__':
try:
main()
while True:
query = input("xql> ")
if query == ".exit":
break
main(query)
except ImportError as e:
raise ImportError('main function is not imported please try again.') from e

36 changes: 15 additions & 21 deletions xql/src/xql/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from sqlglot import parse_one, exp
from xarray.core.groupby import DatasetGroupBy

from .utils import timing
from .where import apply_where

command_info = {
Expand Down Expand Up @@ -443,31 +444,24 @@ def run_query(query: str) -> None:
result = parse_query(query)
display_result(result)


def main():
@timing
def main(query: str):
"""
Main function for runnning this file.
"""
while True:

query = input("xql> ")

if query == ".exit":
break
if ".help" in query:
display_help(query)

elif ".help" in query:
display_help(query)
elif ".set" in query:
set_dataset_table(query)

elif ".set" in query:
set_dataset_table(query)
elif ".show" in query:
display_table_dataset_map(query)

elif ".show" in query:
display_table_dataset_map(query)

else:
try:
result = parse_query(query)
except Exception as e:
result = f"ERROR: {type(e).__name__}: {e.__str__()}."
else:
try:
result = parse_query(query)
except Exception as e:
result = f"ERROR: {type(e).__name__}: {e.__str__()}."

display_result(result)
display_result(result)
13 changes: 13 additions & 0 deletions xql/src/xql/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from functools import wraps
from time import gmtime, strftime, time

def timing(f):
"""Measure a time for any function execution."""
@wraps(f)
def wrap(*args, **kw):
ts = time()
result = f(*args, **kw)
te = time()
print(f"Query took: { strftime('%H:%M:%S', gmtime(te - ts)) }")
return result
return wrap

0 comments on commit 13a9777

Please sign in to comment.