|
55 | 55 | """
|
56 | 56 |
|
57 | 57 | import argparse
|
58 |
| -import json |
59 | 58 | import logging
|
60 | 59 | import os
|
61 | 60 | from collections import defaultdict
|
|
65 | 64 |
|
66 | 65 | import requests
|
67 | 66 | import yaml
|
| 67 | +from cache_manager import get_cache_stats, make_cached_request |
68 | 68 | from dotenv import load_dotenv
|
69 | 69 |
|
70 |
| -from cache_manager import CACHE_DIR, CacheManager |
71 |
| - |
72 | 70 |
|
73 | 71 | load_dotenv()
|
74 | 72 |
|
|
141 | 139 | "linux.g5.4xlarge.nvidia.cpu", # a nonexistent label used by a repo
|
142 | 140 | ]
|
143 | 141 |
|
144 |
| -HEADERS = { |
145 |
| - "Authorization": f"Bearer {GITHUB_TOKEN}", |
146 |
| - "Accept": "application/vnd.github+json", |
147 |
| -} |
148 |
| - |
149 | 142 | BASE_URL = "https://api.github.com"
|
150 | 143 | WORKFLOW_RUN_LOOKBACK = (datetime.utcnow() - timedelta(days=180)).isoformat() + "Z"
|
151 | 144 |
|
152 |
| -# Global cache manager instance |
153 |
| -cache_manager = CacheManager() |
154 |
| - |
155 |
| - |
156 |
| -def make_cached_request( |
157 |
| - url: str, headers: Optional[Dict[str, str]] = None |
158 |
| -) -> Optional[Dict]: |
159 |
| - """ |
160 |
| - Make an HTTP request with caching. Returns the JSON response if successful. |
161 |
| -
|
162 |
| - Args: |
163 |
| - url: The URL to request |
164 |
| - headers: Optional headers for the request |
165 |
| -
|
166 |
| - Returns: |
167 |
| - JSON response data if successful, None if failed |
168 |
| - """ |
169 |
| - # Check cache first |
170 |
| - cached_response = cache_manager.get(url) |
171 |
| - if cached_response: |
172 |
| - logging.info(f"[make_cached_request] Using cached response for: {url}") |
173 |
| - return cached_response |
174 |
| - |
175 |
| - # Make actual HTTP request |
176 |
| - logging.info(f"[make_cached_request] Making HTTP request to: {url}") |
177 |
| - try: |
178 |
| - response = requests.get(url, headers=headers or HEADERS) |
179 |
| - response.raise_for_status() |
180 |
| - data = response.json() |
181 |
| - |
182 |
| - # Cache successful response |
183 |
| - cache_manager.set(url, data) |
184 |
| - logging.info(f"[make_cached_request] Successfully cached response for: {url}") |
185 |
| - return data |
186 |
| - |
187 |
| - except requests.exceptions.RequestException as e: |
188 |
| - logging.error(f"[make_cached_request] HTTP request failed for {url}: {e}") |
189 |
| - return None |
190 |
| - except json.JSONDecodeError as e: |
191 |
| - logging.error( |
192 |
| - f"[make_cached_request] Failed to parse JSON response for {url}: {e}" |
193 |
| - ) |
194 |
| - return None |
195 |
| - |
196 | 145 |
|
197 | 146 | def get_repos(org: str) -> List[str]:
|
198 | 147 | logging.info(f"[get_repos] Start fetching repositories for org: {org}")
|
@@ -445,32 +394,6 @@ def save_to_yaml(data: Dict, filename: str = "runner_labels_summary.yml"):
|
445 | 394 | logging.info(f"[save_to_yaml] Data successfully saved to {filename}")
|
446 | 395 |
|
447 | 396 |
|
448 |
| -def clear_cache(): |
449 |
| - """Clear all cached data.""" |
450 |
| - import shutil |
451 |
| - |
452 |
| - if CACHE_DIR.exists(): |
453 |
| - shutil.rmtree(CACHE_DIR) |
454 |
| - CACHE_DIR.mkdir(exist_ok=True) |
455 |
| - logging.info(f"[clear_cache] Cleared cache directory: {CACHE_DIR}") |
456 |
| - else: |
457 |
| - logging.info(f"[clear_cache] Cache directory does not exist: {CACHE_DIR}") |
458 |
| - |
459 |
| - |
460 |
| -def get_cache_stats(): |
461 |
| - """Get statistics about the cache.""" |
462 |
| - if not CACHE_DIR.exists(): |
463 |
| - return {"total_files": 0, "total_size_mb": 0} |
464 |
| - |
465 |
| - cache_files = list(CACHE_DIR.glob("*.json")) |
466 |
| - total_size = sum(f.stat().st_size for f in cache_files) |
467 |
| - |
468 |
| - return { |
469 |
| - "total_files": len(cache_files), |
470 |
| - "total_size_mb": round(total_size / (1024 * 1024), 2), |
471 |
| - } |
472 |
| - |
473 |
| - |
474 | 397 | def download_scale_config(url: str, dest: str = "scale-config.yml") -> bool:
|
475 | 398 | """Download scale-config.yml from the given URL if it does not exist locally."""
|
476 | 399 | if os.path.exists(dest):
|
|
0 commit comments