Skip to content

Commit

Permalink
Move print function to printer module
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtemple committed Sep 17, 2021
1 parent f7cc2ba commit 2c9c9a9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
2 changes: 0 additions & 2 deletions spectacles/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,6 @@ def run_query(self, query_id: int) -> str:

result = response.text

# TODO: Handle the error that comes back when the query is unrunnable

return result

def get_query_task_multi_results(self, query_task_ids: List[str]) -> JsonDict:
Expand Down
31 changes: 31 additions & 0 deletions spectacles/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import textwrap
from typing import List, Optional
import colorama # type: ignore
from tabulate import tabulate
from spectacles.validators.sql import SqlTest
from spectacles.logger import GLOBAL_LOGGER as logger, log_sql_error, COLORS

LINE_WIDTH = 80
Expand Down Expand Up @@ -127,3 +129,32 @@ def extract_sql_context(sql: str, line_number: int, window_size: int = 2) -> str
marked = mark_line(selected_lines, line_number=line_number - line_start + 1)
context = "\n".join(marked)
return context


def print_profile_results(tests: List[SqlTest], runtime_threshold: int) -> None:
HEADER_CHAR = "."
print_header("Query profiler results", char=HEADER_CHAR, leading_newline=False)
if tests:
tests_by_runtime = sorted(
tests,
key=lambda x: x.runtime if x.runtime is not None else -1,
reverse=True,
)
profile_results = [test.get_profile_result() for test in tests_by_runtime]
output = tabulate(
profile_results,
headers=[
"Type",
"Name",
"Runtime (s)",
"Query ID",
"Explore From Here",
],
tablefmt="github",
numalign="left",
floatfmt=".1f",
)
else:
output = f"All queries completed in less than {runtime_threshold} " "seconds."
logger.info(output)
print_header(HEADER_CHAR, char=HEADER_CHAR)
6 changes: 5 additions & 1 deletion spectacles/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from spectacles.validators import SqlValidator, DataTestValidator, ContentValidator
from spectacles.utils import time_hash
from spectacles.logger import GLOBAL_LOGGER as logger
from spectacles.printer import print_header
from spectacles.printer import print_header, LINE_WIDTH
from spectacles.lookml import Project, Explore, build_project


Expand Down Expand Up @@ -48,6 +48,10 @@ def __init__(self, client: LookerClient, project: str, remote_reset: bool = Fals
self.import_managers: List[LookerBranchManager] = []

def __call__(self, ref: Optional[str] = None, ephemeral: Optional[bool] = None):
logger.debug(
f"\nSetting Git state for project '{self.project}' "
f"@ {ref or 'production'}\n" + "-" * LINE_WIDTH
)
self.branch = None
self.commit = None

Expand Down
36 changes: 2 additions & 34 deletions spectacles/validators/sql.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dataclasses import dataclass
from typing import Union, Dict, Any, List, Optional
import time
from tabulate import tabulate
from spectacles.validators.validator import SpectaclesTest
from spectacles.client import LookerClient
from spectacles.lookml import Dimension, Explore
from spectacles.exceptions import SpectaclesException, SqlError
from spectacles.logger import GLOBAL_LOGGER as logger
from spectacles.printer import print_header
from spectacles.printer import print_profile_results

# TODO: Assign Tests to their corresponding LookMlObject

Expand Down Expand Up @@ -143,7 +142,7 @@ def run_tests(self, tests: List[SqlTest], profile: bool = False):
)

if profile:
self._print_profile_results()
print_profile_results(self._long_running_tests, self.runtime_threshold)

def _get_running_query_tasks(self) -> List[str]:
return [
Expand Down Expand Up @@ -303,34 +302,3 @@ def _cancel_queries(self, query_task_ids: List[str]) -> None:
"""Asks the Looker API to cancel specified queries"""
for query_task_id in query_task_ids:
self.client.cancel_query_task(query_task_id)

def _print_profile_results(self) -> None:
HEADER_CHAR = "."
print_header("Query profiler results", char=HEADER_CHAR, leading_newline=False)
if self._long_running_tests:
tests_by_runtime = sorted(
self._long_running_tests,
key=lambda x: x.runtime if x.runtime is not None else -1,
reverse=True,
)
profile_results = [test.get_profile_result() for test in tests_by_runtime]
output = tabulate(
profile_results,
headers=[
"Type",
"Name",
"Runtime (s)",
"Query ID",
"Explore From Here",
],
tablefmt="github",
numalign="left",
floatfmt=".1f",
)
else:
output = (
f"All queries completed in less than {self.runtime_threshold} "
"seconds."
)
logger.info(output)
print_header(HEADER_CHAR, char=HEADER_CHAR)

0 comments on commit 2c9c9a9

Please sign in to comment.