-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathuploadMetadata.py
More file actions
executable file
·306 lines (267 loc) · 9.67 KB
/
uploadMetadata.py
File metadata and controls
executable file
·306 lines (267 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
import json
import argparse
import re
import os
from datetime import datetime, timezone
# --- FIRESTORE (remove when deprecating) ---
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# --- END FIRESTORE ---
# --- PUBSUB ---
from google.cloud import pubsub_v1
from google.oauth2 import service_account
# --- END PUBSUB ---
# make sure the working dir is flow/
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
# Create the argument parser
parser = argparse.ArgumentParser(description="Process some integers.")
# Add the named arguments
parser.add_argument("--buildID", type=str, help="Build ID from jenkins")
parser.add_argument("--branchName", type=str, help="Current Branch Name")
parser.add_argument("--pipelineID", type=str, help="Jenkins pipeline ID")
parser.add_argument("--commitSHA", type=str, help="Current commit sha")
parser.add_argument("--jenkinsURL", type=str, help="Jenkins Report URL")
parser.add_argument(
"--changeBranch", type=str, help="Branch corresponding to change request"
)
parser.add_argument("--cred", type=str, help="Service account credentials file")
parser.add_argument("--variant", type=str, default="base")
# --- PUBSUB args ---
parser.add_argument("--pubsubProjectID", type=str, help="GCP project ID for Pub/Sub")
parser.add_argument(
"--pubsubTopicID",
type=str,
default="ci-metrics-reports-topics",
help="Pub/Sub topic ID",
)
parser.add_argument(
"--pubsubCred", type=str, help="Service account credentials file for Pub/Sub"
)
parser.add_argument("--runID", type=str, help="Unique identifier for this CI run")
# --- END PUBSUB args ---
# Parse the arguments
args = parser.parse_args()
# --- FIRESTORE (remove when deprecating) ---
def upload_data(db, dataFile, platform, design, variant, args, rules):
# Set the document data
key = args.commitSHA + "-" + platform + "-" + design + "-" + variant
doc_ref = db.collection("build_metrics").document(key)
doc_ref.set(
{
"build_id": args.buildID,
"branch_name": args.branchName,
"pipeline_id": args.pipelineID,
"change_branch": args.changeBranch,
"commit_sha": args.commitSHA,
"jenkins_url": args.jenkinsURL,
"rules": rules,
}
)
# Load JSON data from file
with open(dataFile) as f:
data = json.load(f)
# Replace the character ':' in the keys
new_data = {}
stages = []
excludes = ["run", "commit", "total_time", "constraints"]
gen_date = datetime.now()
for k, v in data.items():
new_key = re.sub(":", "__", k) # replace ':' with '__'
new_data[new_key] = v
stage_name = k.split("__")[0]
if stage_name not in excludes:
stages.append(stage_name)
if k == "run__flow__generate_date":
# Convert string to datetime
gen_date = datetime.strptime(v, "%Y-%m-%d %H:%M")
new_data[k] = gen_date
stages = set(stages)
new_data["stages"] = stages
# Set the data to the document in Firestore
doc_ref.update(new_data)
branch_doc_ref = db.collection("branches").document(args.branchName)
# check if date is greater than the one in the document if it exists
if branch_doc_ref.get().exists:
current_date = branch_doc_ref.get().to_dict().get("run__flow__generate_date")
current_date = current_date.replace(tzinfo=timezone.utc)
gen_date = gen_date.replace(tzinfo=timezone.utc)
if current_date is not None and gen_date > current_date:
branch_doc_ref.update(
{
"run__flow__generate_date": gen_date,
"jenkins_url": args.jenkinsURL,
"change_branch": args.changeBranch,
}
)
else:
branch_doc_ref.update(
{
"jenkins_url": args.jenkinsURL,
"change_branch": args.changeBranch,
}
)
else:
branch_doc_ref.set(
{
"name": args.branchName,
"run__flow__generate_date": gen_date,
"jenkins_url": args.jenkinsURL,
}
)
commit_doc_ref = db.collection("commits").document(args.commitSHA)
if commit_doc_ref.get().exists:
current_date = commit_doc_ref.get().to_dict().get("run__flow__generate_date")
current_date = current_date.replace(tzinfo=timezone.utc)
gen_date = gen_date.replace(tzinfo=timezone.utc)
if current_date is not None and gen_date > current_date:
commit_doc_ref.update(
{
"run__flow__generate_date": gen_date,
"jenkins_url": args.jenkinsURL,
}
)
else:
commit_doc_ref.update(
{
"jenkins_url": args.jenkinsURL,
}
)
else:
commit_doc_ref.set(
{
"sha": args.commitSHA,
"run__flow__generate_date": gen_date,
"jenkins_url": args.jenkinsURL,
}
)
platform_doc_ref = db.collection("platforms").document(platform)
if platform_doc_ref.get().exists:
designs = platform_doc_ref.get().to_dict().get("designs")
if design not in designs:
design_ref = {
"name": design,
"rules": rules,
}
designs[design] = design_ref
platform_doc_ref.update(
{
"designs": designs,
}
)
else:
designs = {}
design_ref = {
"name": design,
"rules": rules,
}
designs[design] = design_ref
platform_doc_ref.set(
{
"designs": designs,
"name": platform,
}
)
if (
not doc_ref.get().exists
or not branch_doc_ref.get().exists
or not commit_doc_ref.get().exists
or not platform_doc_ref.get().exists
):
raise Exception(f"Failed to upload data for {platform} {design} {variant}.")
# --- END FIRESTORE ---
# --- PUBSUB ---
def publish_to_pubsub(
publisher, topic_path, dataFile, platform, design, variant, args, rules
):
"""Publish a single design's metrics to Pub/Sub as a JSON message."""
with open(dataFile) as f:
data = json.load(f)
# Build the payload: CLI args + metrics with ':' replaced by '__'
payload = {
"build_id": args.buildID,
"branch_name": args.branchName,
"pipeline_id": args.pipelineID,
"change_branch": args.changeBranch,
"commit_sha": args.commitSHA,
"jenkins_url": args.jenkinsURL,
"run_id": args.runID,
"rules": rules,
}
for k, v in data.items():
new_key = re.sub(":", "__", k)
payload[new_key] = v
message_data = json.dumps(payload).encode("utf-8")
future = publisher.publish(topic_path, data=message_data)
message_id = future.result()
print(
f"[INFO] Published to Pub/Sub (message ID: {message_id}) for {platform} {design} {variant}."
)
# --- END PUBSUB ---
def get_rules(dataFile):
data = {}
if os.path.exists(dataFile):
with open(dataFile) as f:
data = json.load(f)
return data
# --- FIRESTORE init (remove when deprecating) ---
db = None
if args.cred:
firebase_admin.initialize_app(credentials.Certificate(args.cred))
db = firestore.client()
# --- END FIRESTORE init ---
# --- PUBSUB init ---
publisher = None
topic_path = None
if args.pubsubCred and args.pubsubProjectID:
pubsub_credentials = service_account.Credentials.from_service_account_file(
args.pubsubCred
)
publisher = pubsub_v1.PublisherClient(credentials=pubsub_credentials)
topic_path = publisher.topic_path(args.pubsubProjectID, args.pubsubTopicID)
print(f"[INFO] Pub/Sub publisher initialized for topic: {topic_path}")
elif args.pubsubProjectID:
# No credentials file — use default credentials (e.g., emulator or ADC)
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(args.pubsubProjectID, args.pubsubTopicID)
print(
f"[INFO] Pub/Sub publisher initialized (default creds) for topic: {topic_path}"
)
# --- END PUBSUB init ---
RUN_FILENAME = "metadata.json"
for reportDir, dirs, files in sorted(os.walk("reports", topdown=False)):
dirList = reportDir.split(os.sep)
if len(dirList) != 4:
continue
# basic info about test design
platform = dirList[1]
design = dirList[2]
variant = dirList[3]
dataFile = os.path.join(reportDir, RUN_FILENAME)
if not os.path.exists(dataFile):
print(f"[WARN] No data file for {platform} {design} {variant}.")
continue
if platform == "sky130hd_fakestack" or platform == "src":
print(f"[WARN] Skiping upload {platform} {design} {variant}.")
continue
print(f"[INFO] Get rules for {platform} {design} {variant}.")
rules = get_rules(
os.path.join("designs", platform, design, f"rules-{variant}.json")
)
# --- FIRESTORE (remove when deprecating) ---
if db:
print(f"[INFO] Upload data for {platform} {design} {variant}.")
upload_data(db, dataFile, platform, design, variant, args, rules)
# --- END FIRESTORE ---
# --- PUBSUB ---
if publisher:
try:
publish_to_pubsub(
publisher, topic_path, dataFile, platform, design, variant, args, rules
)
except Exception as e:
print(
f"[WARN] Pub/Sub publish failed for {platform} {design} {variant}: {e}"
)
# --- END PUBSUB ---