|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Shared helper functions for tqdm progress bar.""" |
| 16 | + |
| 17 | +import concurrent.futures |
| 18 | +import time |
| 19 | +import warnings |
| 20 | + |
| 21 | +try: |
| 22 | + import tqdm |
| 23 | +except ImportError: # pragma: NO COVER |
| 24 | + tqdm = None |
| 25 | + |
| 26 | +_NO_TQDM_ERROR = ( |
| 27 | + "A progress bar was requested, but there was an error loading the tqdm " |
| 28 | + "library. Please install tqdm to use the progress bar functionality." |
| 29 | +) |
| 30 | + |
| 31 | +_PROGRESS_BAR_UPDATE_INTERVAL = 0.5 |
| 32 | + |
| 33 | + |
| 34 | +def get_progress_bar(progress_bar_type, description, total, unit): |
| 35 | + """Construct a tqdm progress bar object, if tqdm is .""" |
| 36 | + if tqdm is None: |
| 37 | + if progress_bar_type is not None: |
| 38 | + warnings.warn(_NO_TQDM_ERROR, UserWarning, stacklevel=3) |
| 39 | + return None |
| 40 | + |
| 41 | + try: |
| 42 | + if progress_bar_type == "tqdm": |
| 43 | + return tqdm.tqdm(desc=description, total=total, unit=unit) |
| 44 | + elif progress_bar_type == "tqdm_notebook": |
| 45 | + return tqdm.tqdm_notebook(desc=description, total=total, unit=unit) |
| 46 | + elif progress_bar_type == "tqdm_gui": |
| 47 | + return tqdm.tqdm_gui(desc=description, total=total, unit=unit) |
| 48 | + except (KeyError, TypeError): |
| 49 | + # Protect ourselves from any tqdm errors. In case of |
| 50 | + # unexpected tqdm behavior, just fall back to showing |
| 51 | + # no progress bar. |
| 52 | + warnings.warn(_NO_TQDM_ERROR, UserWarning, stacklevel=3) |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def wait_for_query(query_job, progress_bar_type=None): |
| 57 | + """Return query result and display a progress bar while the query running, if tqdm is installed.""" |
| 58 | + if progress_bar_type is None: |
| 59 | + return query_job.result() |
| 60 | + |
| 61 | + default_total = 1 |
| 62 | + current_stage = None |
| 63 | + start_time = time.time() |
| 64 | + progress_bar = get_progress_bar( |
| 65 | + progress_bar_type, "Query is running", default_total, "query" |
| 66 | + ) |
| 67 | + i = 0 |
| 68 | + while True: |
| 69 | + if query_job.query_plan: |
| 70 | + default_total = len(query_job.query_plan) |
| 71 | + current_stage = query_job.query_plan[i] |
| 72 | + progress_bar.total = len(query_job.query_plan) |
| 73 | + progress_bar.set_description( |
| 74 | + "Query executing stage {} and status {} : {:0.2f}s".format( |
| 75 | + current_stage.name, current_stage.status, time.time() - start_time, |
| 76 | + ), |
| 77 | + ) |
| 78 | + try: |
| 79 | + query_result = query_job.result(timeout=_PROGRESS_BAR_UPDATE_INTERVAL) |
| 80 | + progress_bar.update(default_total) |
| 81 | + progress_bar.set_description( |
| 82 | + "Query complete after {:0.2f}s".format(time.time() - start_time), |
| 83 | + ) |
| 84 | + break |
| 85 | + except concurrent.futures.TimeoutError: |
| 86 | + query_job.reload() # Refreshes the state via a GET request. |
| 87 | + if current_stage: |
| 88 | + if current_stage.status == "COMPLETE": |
| 89 | + if i < default_total - 1: |
| 90 | + progress_bar.update(i + 1) |
| 91 | + i += 1 |
| 92 | + continue |
| 93 | + progress_bar.close() |
| 94 | + return query_result |
0 commit comments