Skip to content

Commit a203b4b

Browse files
util: consolidate Pub/Sub publishing into a single pipeline-level message
Instead of publishing one Pub/Sub message per platform-design-variant, collect all design records during the report walk and publish a single message containing the full pipeline run. Build-level fields are at the top; per-design data (platform, design, variant, rules, metrics) lives in a nested `designs` array. Also adds test_uploadMetadata.py with 15 unit/integration tests covering key substitution, payload schema, 10 MB size budget, datetime serialization, and an end-to-end round-trip. Signed-off-by: Miguel Pedro <miguel.pedro@precisioninno.com>
1 parent 4547021 commit a203b4b

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

‎flow/util/uploadMetadata.py‎

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
parser.add_argument("--variant", type=str, default="base")
3939

4040
# --- 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+
)
4148
parser.add_argument("--pubsubProjectID", type=str, help="GCP project ID for Pub/Sub")
4249
parser.add_argument(
4350
"--pubsubTopicID",
@@ -194,34 +201,46 @@ def upload_data(db, dataFile, platform, design, variant, args, rules):
194201

195202

196203
# --- 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."""
201206
with open(dataFile) as f:
202207
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+
203217

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."""
205220
payload = {
221+
"payload_schema_version": 2,
222+
"jenkins_env": args.jenkinsEnv,
206223
"build_id": args.buildID,
207224
"branch_name": args.branchName,
208225
"pipeline_id": args.pipelineID,
209226
"change_branch": args.changeBranch,
210227
"commit_sha": args.commitSHA,
211228
"jenkins_url": args.jenkinsURL,
212-
"rules": rules,
229+
"designs": design_records,
213230
}
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
222233
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."
224235
)
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}).")
225244

226245

227246
# --- END PUBSUB ---
@@ -264,6 +283,10 @@ def get_rules(dataFile):
264283

265284
RUN_FILENAME = "metadata.json"
266285

286+
# --- PUBSUB ---
287+
design_records = []
288+
# --- END PUBSUB ---
289+
267290
for reportDir, dirs, files in sorted(os.walk("reports", topdown=False)):
268291
dirList = reportDir.split(os.sep)
269292
if len(dirList) != 4:
@@ -293,12 +316,17 @@ def get_rules(dataFile):
293316

294317
# --- PUBSUB ---
295318
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+
)
304322
# --- 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

Comments
 (0)