Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from functools import lru_cache
|
| 4 |
+
from typing import List, Tuple
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from datasets import Dataset, load_dataset
|
| 8 |
+
|
| 9 |
+
DATASET_ID = os.environ.get(
|
| 10 |
+
"CIRCLECI_RESULTS_DATASET_ID", "transformers-community/circleci-test-results"
|
| 11 |
+
)
|
| 12 |
+
MAX_ROWS = 200
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@lru_cache(maxsize=1)
|
| 16 |
+
def _load_dataset() -> Dataset | None:
|
| 17 |
+
try:
|
| 18 |
+
return load_dataset(DATASET_ID, split="train")
|
| 19 |
+
except Exception as error:
|
| 20 |
+
print(f"Failed to load dataset {DATASET_ID}: {error}")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _filter_records(dataset: Dataset, repo: str, pr: str, sha: str) -> List[dict]:
|
| 25 |
+
repo = repo.strip().lower()
|
| 26 |
+
pr = pr.strip()
|
| 27 |
+
sha = sha.strip().lower()
|
| 28 |
+
|
| 29 |
+
def _matches(example: dict) -> bool:
|
| 30 |
+
if repo and repo not in (example.get("repository") or "").lower():
|
| 31 |
+
return False
|
| 32 |
+
if pr and pr != (example.get("pr_number") or ""):
|
| 33 |
+
return False
|
| 34 |
+
if sha and sha not in (example.get("commit_sha") or "").lower():
|
| 35 |
+
return False
|
| 36 |
+
return True
|
| 37 |
+
|
| 38 |
+
items = [ex for ex in dataset if _matches(ex)]
|
| 39 |
+
items.sort(key=lambda ex: ex.get("collected_at") or "", reverse=True)
|
| 40 |
+
return items[:MAX_ROWS]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def query(repo: str, pr: str, sha: str) -> Tuple[List[List[str]], str]:
|
| 44 |
+
dataset = _load_dataset()
|
| 45 |
+
if dataset is None:
|
| 46 |
+
return [], json.dumps({"error": "Dataset not available"}, indent=2)
|
| 47 |
+
|
| 48 |
+
records = _filter_records(dataset, repo, pr, sha)
|
| 49 |
+
table_rows = []
|
| 50 |
+
for record in records:
|
| 51 |
+
table_rows.append(
|
| 52 |
+
[
|
| 53 |
+
record.get("collected_at", ""),
|
| 54 |
+
record.get("repository", ""),
|
| 55 |
+
record.get("pr_number", ""),
|
| 56 |
+
record.get("commit_sha", "")[:12],
|
| 57 |
+
record.get("workflow_id", ""),
|
| 58 |
+
str(record.get("failure_count", 0)),
|
| 59 |
+
str(record.get("job_count", 0)),
|
| 60 |
+
str(record.get("test_count", 0)),
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
details = json.dumps({}, indent=2)
|
| 65 |
+
if records:
|
| 66 |
+
payload = records[0].get("payload_json", "{}")
|
| 67 |
+
try:
|
| 68 |
+
details = json.dumps(json.loads(payload), indent=2)
|
| 69 |
+
except json.JSONDecodeError:
|
| 70 |
+
details = json.dumps({"error": "Unable to parse payload"}, indent=2)
|
| 71 |
+
|
| 72 |
+
return table_rows, details
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def refresh_dataset() -> str:
|
| 76 |
+
_load_dataset.cache_clear()
|
| 77 |
+
dataset = _load_dataset()
|
| 78 |
+
if dataset is None:
|
| 79 |
+
return "Failed to refresh dataset."
|
| 80 |
+
return f"Dataset refreshed with {len(dataset)} rows."
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown(
|
| 85 |
+
"""
|
| 86 |
+
# CircleCI Test Collection Helper
|
| 87 |
+
|
| 88 |
+
Use the filters below to inspect CircleCI test aggregation records for the Transformers repository (or any
|
| 89 |
+
repository that uploads data to the `transformers-community/circleci-test-results` dataset).
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
repo_box = gr.Textbox(label="Repository", placeholder="huggingface/transformers")
|
| 95 |
+
pr_box = gr.Textbox(label="PR number")
|
| 96 |
+
sha_box = gr.Textbox(label="Commit SHA (prefix accepted)")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
search_btn = gr.Button("Search")
|
| 100 |
+
refresh_btn = gr.Button("Refresh dataset cache")
|
| 101 |
+
|
| 102 |
+
table = gr.Dataframe(
|
| 103 |
+
headers=[
|
| 104 |
+
"Collected at",
|
| 105 |
+
"Repository",
|
| 106 |
+
"PR",
|
| 107 |
+
"Commit",
|
| 108 |
+
"Workflow ID",
|
| 109 |
+
"Failures",
|
| 110 |
+
"Jobs",
|
| 111 |
+
"Tests",
|
| 112 |
+
],
|
| 113 |
+
wrap=True,
|
| 114 |
+
)
|
| 115 |
+
json_view = gr.Code(label="Latest entry details", language="json")
|
| 116 |
+
status = gr.Markdown("")
|
| 117 |
+
|
| 118 |
+
search_btn.click(query, inputs=[repo_box, pr_box, sha_box], outputs=[table, json_view])
|
| 119 |
+
refresh_btn.click(refresh_dataset, outputs=status)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.queue(max_size=20).launch()
|