Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit d2dc5f6

Browse files
committed
refactor: update execution history API and tests
1 parent 6557d90 commit d2dc5f6

File tree

4 files changed

+1317
-66
lines changed

4 files changed

+1317
-66
lines changed

bigframes/session/__init__.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,39 @@
109109
logger = logging.getLogger(__name__)
110110

111111

112+
class _ExecutionHistory(pandas.DataFrame):
113+
@property
114+
def _constructor(self):
115+
return _ExecutionHistory
116+
117+
def _repr_html_(self) -> str | None:
118+
try:
119+
import bigframes.formatting_helpers as formatter
120+
121+
if self.empty:
122+
return "<div>No executions found.</div>"
123+
124+
cols = ["job_id", "status", "total_bytes_processed", "job_url"]
125+
df_display = self[cols].copy()
126+
df_display["total_bytes_processed"] = df_display[
127+
"total_bytes_processed"
128+
].apply(formatter.get_formatted_bytes)
129+
130+
def format_url(url):
131+
return f'<a target="_blank" href="{url}">Open Job</a>' if url else ""
132+
133+
df_display["job_url"] = df_display["job_url"].apply(format_url)
134+
135+
# Rename job_id to query_id to match user expectations
136+
df_display = df_display.rename(columns={"job_id": "query_id"})
137+
138+
compact_html = df_display.to_html(escape=False, index=False)
139+
140+
return compact_html
141+
except Exception:
142+
return super()._repr_html_() # type: ignore
143+
144+
112145
@log_adapter.class_logger
113146
class Session(
114147
third_party_pandas_gbq.GBQIOMixin,
@@ -373,7 +406,7 @@ def slot_millis_sum(self):
373406

374407
def execution_history(self) -> pandas.DataFrame:
375408
"""Returns a list of underlying BigQuery executions initiated by BigFrames in the current session."""
376-
return pandas.DataFrame([job.__dict__ for job in self._metrics.jobs])
409+
return _ExecutionHistory([job.__dict__ for job in self._metrics.jobs])
377410

378411
@property
379412
def _allows_ambiguity(self) -> bool:

bigframes/session/metrics.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def from_job(
6060
if query_text and len(query_text) > 1024:
6161
query_text = query_text[:1021] + "..."
6262

63+
job_id = getattr(query_job, "job_id", None)
64+
job_url = None
65+
if job_id:
66+
job_url = f"https://console.cloud.google.com/bigquery?project={query_job.project}&j=bq:{query_job.location}:{job_id}&page=queryresults"
67+
6368
metadata = cls(
6469
job_id=query_job.job_id,
6570
location=query_job.location,
@@ -72,9 +77,7 @@ def from_job(
7277
job_type=query_job.job_type,
7378
error_result=query_job.error_result,
7479
query=query_text,
75-
job_url=f"https://console.cloud.google.com/bigquery?project={query_job.project}&j=bq:{query_job.location}:{query_job.job_id}&page=queryresults"
76-
if getattr(query_job, "job_id", None)
77-
else None,
80+
job_url=job_url,
7881
)
7982
if isinstance(query_job, QueryJob):
8083
metadata.cached = getattr(query_job, "cache_hit", None)
@@ -111,8 +114,15 @@ def from_row_iterator(
111114
if query_text and len(query_text) > 1024:
112115
query_text = query_text[:1021] + "..."
113116

117+
job_id = getattr(row_iterator, "job_id", None)
118+
job_url = None
119+
if job_id:
120+
project = getattr(row_iterator, "project", "")
121+
location = getattr(row_iterator, "location", "")
122+
job_url = f"https://console.cloud.google.com/bigquery?project={project}&j=bq:{location}:{job_id}&page=queryresults"
123+
114124
return cls(
115-
job_id=getattr(row_iterator, "job_id", None),
125+
job_id=job_id,
116126
query_id=getattr(row_iterator, "query_id", None),
117127
location=getattr(row_iterator, "location", None),
118128
project=getattr(row_iterator, "project", None),
@@ -126,9 +136,7 @@ def from_row_iterator(
126136
job_type="query",
127137
cached=getattr(row_iterator, "cache_hit", None),
128138
query=query_text,
129-
job_url=f"https://console.cloud.google.com/bigquery?project={getattr(row_iterator, 'project', '')}&j=bq:{getattr(row_iterator, 'location', '')}:{getattr(row_iterator, 'job_id', '')}&page=queryresults"
130-
if getattr(row_iterator, "job_id", None)
131-
else None,
139+
job_url=job_url,
132140
)
133141

134142

0 commit comments

Comments
 (0)