|
38 | 38 | parser.add_argument("--variant", type=str, default="base") |
39 | 39 |
|
40 | 40 | # --- PUBSUB args --- |
| 41 | +parser.add_argument( |
| 42 | + "--jenkinsEnv", |
| 43 | + type=str, |
| 44 | + default="unknown", |
| 45 | + choices=["public", "secure", "unknown"], |
| 46 | + help="Jenkins environment (public or secure)", |
| 47 | +) |
41 | 48 | parser.add_argument("--pubsubProjectID", type=str, help="GCP project ID for Pub/Sub") |
42 | 49 | parser.add_argument( |
43 | 50 | "--pubsubTopicID", |
@@ -194,34 +201,46 @@ def upload_data(db, dataFile, platform, design, variant, args, rules): |
194 | 201 |
|
195 | 202 |
|
196 | 203 | # --- PUBSUB --- |
197 | | -def publish_to_pubsub( |
198 | | - publisher, topic_path, dataFile, platform, design, variant, args, rules |
199 | | -): |
200 | | - """Publish a single design's metrics to Pub/Sub as a JSON message.""" |
| 204 | +def build_design_record(dataFile, platform, design, variant, rules): |
| 205 | + """Return a dict for one design to be included in the pipeline-level payload.""" |
201 | 206 | with open(dataFile) as f: |
202 | 207 | data = json.load(f) |
| 208 | + metrics = {re.sub(":", "__", k): v for k, v in data.items()} |
| 209 | + return { |
| 210 | + "platform": platform, |
| 211 | + "design": design, |
| 212 | + "variant": variant, |
| 213 | + "rules": rules, |
| 214 | + "metrics": metrics, |
| 215 | + } |
| 216 | + |
203 | 217 |
|
204 | | - # Build the payload: CLI args + metrics with ':' replaced by '__' |
| 218 | +def publish_pipeline_report(publisher, topic_path, design_records, args): |
| 219 | + """Publish one message for the entire pipeline run.""" |
205 | 220 | payload = { |
| 221 | + "payload_schema_version": 2, |
| 222 | + "jenkins_env": args.jenkinsEnv, |
206 | 223 | "build_id": args.buildID, |
207 | 224 | "branch_name": args.branchName, |
208 | 225 | "pipeline_id": args.pipelineID, |
209 | 226 | "change_branch": args.changeBranch, |
210 | 227 | "commit_sha": args.commitSHA, |
211 | 228 | "jenkins_url": args.jenkinsURL, |
212 | | - "rules": rules, |
| 229 | + "designs": design_records, |
213 | 230 | } |
214 | | - |
215 | | - for k, v in data.items(): |
216 | | - new_key = re.sub(":", "__", k) |
217 | | - payload[new_key] = v |
218 | | - |
219 | | - message_data = json.dumps(payload).encode("utf-8") |
220 | | - future = publisher.publish(topic_path, data=message_data) |
221 | | - message_id = future.result() |
| 231 | + message_data = json.dumps(payload, default=str).encode("utf-8") |
| 232 | + size_kb = len(message_data) / 1024 |
222 | 233 | print( |
223 | | - f"[INFO] Published to Pub/Sub (message ID: {message_id}) for {platform} {design} {variant}." |
| 234 | + f"[INFO] Publishing pipeline report ({len(design_records)} designs, {size_kb:.1f} KB) to Pub/Sub." |
224 | 235 | ) |
| 236 | + future = publisher.publish( |
| 237 | + topic_path, |
| 238 | + data=message_data, |
| 239 | + payload_schema_version="2", |
| 240 | + jenkins_env=args.jenkinsEnv, |
| 241 | + ) |
| 242 | + message_id = future.result() |
| 243 | + print(f"[INFO] Published pipeline report to Pub/Sub (message ID: {message_id}).") |
225 | 244 |
|
226 | 245 |
|
227 | 246 | # --- END PUBSUB --- |
@@ -264,6 +283,10 @@ def get_rules(dataFile): |
264 | 283 |
|
265 | 284 | RUN_FILENAME = "metadata.json" |
266 | 285 |
|
| 286 | +# --- PUBSUB --- |
| 287 | +design_records = [] |
| 288 | +# --- END PUBSUB --- |
| 289 | + |
267 | 290 | for reportDir, dirs, files in sorted(os.walk("reports", topdown=False)): |
268 | 291 | dirList = reportDir.split(os.sep) |
269 | 292 | if len(dirList) != 4: |
@@ -293,12 +316,17 @@ def get_rules(dataFile): |
293 | 316 |
|
294 | 317 | # --- PUBSUB --- |
295 | 318 | if publisher: |
296 | | - try: |
297 | | - publish_to_pubsub( |
298 | | - publisher, topic_path, dataFile, platform, design, variant, args, rules |
299 | | - ) |
300 | | - except Exception as e: |
301 | | - print( |
302 | | - f"[WARN] Pub/Sub publish failed for {platform} {design} {variant}: {e}" |
303 | | - ) |
| 319 | + design_records.append( |
| 320 | + build_design_record(dataFile, platform, design, variant, rules) |
| 321 | + ) |
304 | 322 | # --- END PUBSUB --- |
| 323 | + |
| 324 | +# --- PUBSUB --- |
| 325 | +if publisher and design_records: |
| 326 | + try: |
| 327 | + publish_pipeline_report(publisher, topic_path, design_records, args) |
| 328 | + except Exception as e: |
| 329 | + print(f"[WARN] Pub/Sub publish failed for pipeline report: {e}") |
| 330 | +elif publisher and not design_records: |
| 331 | + print("[WARN] Pub/Sub publisher initialized but no design records were collected.") |
| 332 | +# --- END PUBSUB --- |
0 commit comments