Skip to content

Commit dadaa2f

Browse files
authored
Fix still missing updates and add helper (#169)
cleaning
1 parent fd267f7 commit dadaa2f

1 file changed

Lines changed: 89 additions & 65 deletions

File tree

api/bugzilla/api_bugzilla.py

Lines changed: 89 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def bugzilla_query_desktop_bugs(self):
128128
print(f"Saved {len(df_new)} new bugs. Total now: {len(df_new)}")
129129

130130
# Insert data
131-
self.db.report_bugzilla_desktop_bugs(df_new)
131+
self.db.report_bugzilla_desktop_bugs_update_insert(df_new)
132132

133133
# Update data
134134
self.bugzilla_query_desktop_bugs_update()
@@ -138,8 +138,8 @@ def bugzilla_query_desktop_bugs_update(self):
138138
# Query bugzilla with these fields where updated is > fecha query
139139

140140
# Calculate start of yesterday in UTC
141-
last_change_time = (datetime.utcnow() - DatetimeUtils.delta_hours(24)).strftime("%Y-%m-%dT%H:%M:%SZ") # noqa
142-
print(f"Update bugs if any after yesterday {last_change_time}")
141+
last_change_time = (datetime.utcnow() - DatetimeUtils.delta_hours(48)).strftime("%Y-%m-%dT%H:%M:%SZ") # noqa
142+
print(f"Update bugs if any after {last_change_time}")
143143

144144
query = {
145145
**BUGZILLA_QA_WHITEBOARD_FILTER,
@@ -175,7 +175,7 @@ def bugzilla_query_desktop_bugs_update(self):
175175
df_update = pd.DataFrame(rows)
176176
print(f"Updated {len(df_update)} bugs")
177177

178-
self.db.bugzilla_desktop_bugs_update_insert(df_update)
178+
self.db.report_bugzilla_desktop_bugs_update_insert(df_update)
179179

180180
def bugzilla_query(self):
181181
all_bugs = []
@@ -296,6 +296,37 @@ def bugzilla_qe_verify(self):
296296
qe_needed_count = self.db.report_bugzilla_qa_needed_count(data_frame) # noqa
297297
self.db.report_bugzilla_qa_needed_count_insert(qe_needed_count)
298298

299+
def bugzilla_helper_refresh_bugs(self, bug_ids: list[int]):
300+
print(f"Refreshing {len(bug_ids)} bugs: {bug_ids}")
301+
bugs = BugzillaHelper().get_bugs(bug_ids) # .get_bugs() fetches by ID list
302+
303+
rows = []
304+
for bug in bugs:
305+
resolved_raw = getattr(bug, "cf_last_resolved", None)
306+
resolved_at = pd.to_datetime(str(resolved_raw)) if resolved_raw else None
307+
308+
rows.append({
309+
"bug_id": bug.id,
310+
"summary": bug.summary,
311+
"product": bug.product,
312+
"qa_whiteboard": getattr(bug, "cf_qa_whiteboard", ""),
313+
"severity": bug.severity,
314+
"priority": bug.priority,
315+
"status": bug.status,
316+
"resolution": bug.resolution,
317+
"created_at": pd.to_datetime(str(bug.creation_time)),
318+
"last_change_time": pd.to_datetime(str(bug.last_change_time)),
319+
"whiteboard": bug.whiteboard,
320+
"keyword": bug.keywords,
321+
"resolved_at": resolved_at
322+
})
323+
324+
df_update = pd.DataFrame(rows)
325+
326+
# This method does an UPDATE on conflict (not INSERT)
327+
self.db.bugzilla_desktop_bugs_update_insert(df_update)
328+
print(f"Updated {len(df_update)} bugs in database.")
329+
299330

300331
class DatabaseBugzilla(Database):
301332

@@ -309,67 +340,6 @@ def qa_needed_delete(self):
309340
print("Delete entries from db first")
310341
self.clean_table(ReportBugzillaQENeeded)
311342

312-
def bugzilla_desktop_bugs_update_insert(self, payload):
313-
for index, row in payload.iterrows():
314-
try:
315-
kw = row.get('keyword', [])
316-
bugzilla_bug_keyword = ", ".join(kw) if isinstance(kw, list) and kw else None # noqa
317-
318-
bug_id = row['bug_id']
319-
320-
# Check if the bug already exists
321-
existing = self.session.query(ReportBugzillaSoftvisionBugs).filter_by( # noqa
322-
bugzilla_key=bug_id
323-
).one_or_none()
324-
if existing:
325-
print(f"Updating bug {bug_id}")
326-
# Compare last_change_time to update
327-
last_change_remote = pd.to_datetime(row['last_change_time']) # noqa
328-
if last_change_remote > existing.bugzilla_bug_last_change_time: # noqa
329-
existing.bugzilla_summary = row['summary']
330-
existing.bugzilla_product = row['product']
331-
existing.bugzilla_qa_whiteboard = row['qa_whiteboard']
332-
existing.bugzilla_bug_severity = row['severity']
333-
existing.bugzilla_bug_priority = row['priority']
334-
existing.bugzilla_bug_status = row['status']
335-
existing.bugzilla_bug_resolution = None if pd.isna(row['resolution']) else row['resolution'] # noqa
336-
existing.bugzilla_bug_created_at = row['created_at']
337-
existing.bugzilla_bug_last_change_time = row['last_change_time'] # noqa
338-
existing.bugzilla_bug_whiteboard = None if pd.isna(row['whiteboard']) else row['whiteboard'] # noqa
339-
340-
existing.bugzilla_bug_keyword = bugzilla_bug_keyword,
341-
existing.bugzilla_bug_resolved_at = None if pd.isna(row['resolved_at']) else row['resolved_at'] # noqa
342-
except KeyError as e:
343-
print(f"Missing key: {e} in row {index}")
344-
345-
self.session.commit()
346-
347-
def report_bugzilla_desktop_bugs(self, payload):
348-
for index, row in payload.iterrows():
349-
try:
350-
kw = row.get('keyword', [])
351-
bugzilla_bug_keyword = ", ".join(kw) if isinstance(kw, list) and kw else None # noqa
352-
353-
report = ReportBugzillaSoftvisionBugs(
354-
bugzilla_key=row['bug_id'],
355-
bugzilla_summary=row['summary'],
356-
bugzilla_product=row['product'],
357-
bugzilla_qa_whiteboard=row['qa_whiteboard'],
358-
bugzilla_bug_severity=row['severity'],
359-
bugzilla_bug_priority=row['priority'],
360-
bugzilla_bug_status=row['status'],
361-
bugzilla_bug_resolution=None if pd.isna(row['resolution']) else row['resolution'], # noqa
362-
bugzilla_bug_created_at=row['created_at'],
363-
bugzilla_bug_last_change_time=row['last_change_time'], # noqa
364-
bugzilla_bug_whiteboard=None if pd.isna(row['whiteboard']) else row['whiteboard'], # noqa
365-
bugzilla_bug_keyword=bugzilla_bug_keyword,
366-
bugzilla_bug_resolved_at=None if pd.isna(row['resolved_at']) else row['resolved_at'] # noqa
367-
)
368-
except KeyError as e:
369-
print(f"Missing key: {e} in row {index}")
370-
self.session.add(report)
371-
self.session.commit()
372-
373343
def report_bugzilla_qa_needed(self, payload):
374344

375345
selected_columns = {
@@ -449,3 +419,57 @@ def report_bugzilla_meta_bug(self, payload):
449419
print(f"Missing key: {e} in row {index}")
450420
self.session.add(report)
451421
self.session.commit()
422+
423+
def report_bugzilla_desktop_bugs_update_insert(self, payload):
424+
for index, row in payload.iterrows():
425+
try:
426+
kw = row.get('keyword', [])
427+
bugzilla_bug_keyword = ", ".join(kw) if isinstance(kw, list) and kw else None # noqa
428+
429+
bug_id = row['bug_id']
430+
431+
# Check if the bug already exists
432+
existing = self.session.query(ReportBugzillaSoftvisionBugs).filter_by(
433+
bugzilla_key=bug_id
434+
).one_or_none()
435+
436+
if existing:
437+
print(f"Updating bug {bug_id}")
438+
# Compare last_change_time to update
439+
last_change_remote = pd.to_datetime(row['last_change_time'])
440+
if last_change_remote > existing.bugzilla_bug_last_change_time:
441+
existing.bugzilla_summary = row['summary']
442+
existing.bugzilla_product = row['product']
443+
existing.bugzilla_qa_whiteboard = row['qa_whiteboard']
444+
existing.bugzilla_bug_severity = row['severity']
445+
existing.bugzilla_bug_priority = row['priority']
446+
existing.bugzilla_bug_status = row['status']
447+
existing.bugzilla_bug_resolution = None if pd.isna(row['resolution']) else row['resolution'] # noqa
448+
existing.bugzilla_bug_created_at = row['created_at']
449+
existing.bugzilla_bug_last_change_time = row['last_change_time']
450+
existing.bugzilla_bug_whiteboard = None if pd.isna(row['whiteboard']) else row['whiteboard'] # noqa
451+
existing.bugzilla_bug_keyword = bugzilla_bug_keyword
452+
existing.bugzilla_bug_resolved_at = None if pd.isna(row['resolved_at']) else row['resolved_at'] # noqa
453+
else:
454+
print(f"Inserting new bug {bug_id}")
455+
new_bug = ReportBugzillaSoftvisionBugs(
456+
bugzilla_key=bug_id,
457+
bugzilla_summary=row['summary'],
458+
bugzilla_product=row['product'],
459+
bugzilla_qa_whiteboard=row['qa_whiteboard'],
460+
bugzilla_bug_severity=row['severity'],
461+
bugzilla_bug_priority=row['priority'],
462+
bugzilla_bug_status=row['status'],
463+
bugzilla_bug_resolution=None if pd.isna(row['resolution']) else row['resolution'], # noqa
464+
bugzilla_bug_created_at=row['created_at'],
465+
bugzilla_bug_last_change_time=row['last_change_time'],
466+
bugzilla_bug_whiteboard=None if pd.isna(row['whiteboard']) else row['whiteboard'], # noqa
467+
bugzilla_bug_keyword=bugzilla_bug_keyword,
468+
bugzilla_bug_resolved_at=None if pd.isna(row['resolved_at']) else row['resolved_at'] # noqa
469+
)
470+
self.session.add(new_bug)
471+
472+
except KeyError as e:
473+
print(f"Missing key: {e} in row {index}")
474+
475+
self.session.commit()

0 commit comments

Comments
 (0)