diff --git a/spectacles/client.py b/spectacles/client.py index 62fc0868..0414ff31 100644 --- a/spectacles/client.py +++ b/spectacles/client.py @@ -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: diff --git a/spectacles/printer.py b/spectacles/printer.py index b6e89cca..e2d225ca 100644 --- a/spectacles/printer.py +++ b/spectacles/printer.py @@ -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 @@ -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) diff --git a/spectacles/runner.py b/spectacles/runner.py index 48b4cc09..18d5c771 100644 --- a/spectacles/runner.py +++ b/spectacles/runner.py @@ -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 @@ -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 diff --git a/spectacles/validators/sql.py b/spectacles/validators/sql.py index 29dc892f..a91f2663 100644 --- a/spectacles/validators/sql.py +++ b/spectacles/validators/sql.py @@ -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 @@ -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 [ @@ -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)