Skip to content

Commit 3f0d598

Browse files
authored
United grafana scripts (#27)
United grafana scripts adapt gitea_info and failed_zuul to both clouds Reviewed-by: Vladimir Vshivkov
1 parent f58a4aa commit 3f0d598

File tree

2 files changed

+100
-65
lines changed

2 files changed

+100
-65
lines changed

failed_zuul.py

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
db_host = os.getenv("DB_HOST")
2020
db_port = os.getenv("DB_PORT")
21-
db_name = os.getenv("DB_CSV") # here we're using main postgres db since we don't need orphan PRs
21+
db_name = os.getenv("DB_ZUUL") # here we're using dedicated postgres db 'zuul' since Failed Zuul PRs panel should be placed on a same dashboard such as Open PRs
22+
db_csv = os.getenv("DB_CSV") # here rtc service table is located
2223
db_user = os.getenv("DB_USER")
2324
db_password = os.getenv("DB_PASSWORD")
2425

@@ -48,9 +49,9 @@ def connect_to_db(db_name):
4849
return None
4950

5051

51-
def create_prs_table(conn, cur, table_name):
52+
def create_prs_table(conn_zuul, cur_zuul, table_name):
5253
try:
53-
cur.execute(
54+
cur_zuul.execute(
5455
f'''CREATE TABLE IF NOT EXISTS {table_name} (
5556
id SERIAL PRIMARY KEY,
5657
"Service Name" VARCHAR(255),
@@ -65,7 +66,7 @@ def create_prs_table(conn, cur, table_name):
6566
"Parent PR Number" INT
6667
);'''
6768
)
68-
conn.commit()
69+
conn_zuul.commit()
6970
print(f"Table {table_name} has been created successfully")
7071
except psycopg2.Error as e:
7172
print(f"Create table: an error occurred while trying to create a table {table_name} in the database: {e}")
@@ -145,7 +146,8 @@ def get_f_pr_commits(org, repo, f_pr_number, gitea_token):
145146
print(f"Get failed PR commits: an error occurred while trying to get pull requests of {repo} repo for {org} org: {e}")
146147

147148

148-
def get_failed_prs(org, repo, gitea_token, conn, cur, table_name):
149+
def get_failed_prs(org, repo, gitea_token, conn_zuul, cur_zuul, table_name):
150+
149151
try:
150152
if repo != "doc-exports" and repo != "dsf":
151153
page = 1
@@ -176,14 +178,15 @@ def get_failed_prs(org, repo, gitea_token, conn, cur, table_name):
176178
zuul_url, status, created_at, days_passed = get_f_pr_commits(org, repo, f_pr_number, gitea_token)
177179
try:
178180
if all(item is not None for item in [zuul_url, status, created_at, days_passed]):
179-
cur.execute(f"""
181+
cur_zuul.execute(f"""
182+
180183
INSERT INTO public.{table_name}
181184
("Service Name", "Failed PR Title", "Failed PR URL", "Squad", "Failed PR State", "Zuul URL", "Zuul Check Status", "Days Passed", "Parent PR Number")
182185
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
183186
""",
184187
(service_name, title, f_pr_url, squad, f_pr_state, zuul_url, status, days_passed, f_par_pr_num)
185188
)
186-
conn.commit()
189+
conn_zuul.commit()
187190
except Exception as e:
188191
print(f"Failed PRs: an error occurred while inserting into {table_name} table: {e}")
189192
else:
@@ -196,56 +199,81 @@ def get_failed_prs(org, repo, gitea_token, conn, cur, table_name):
196199
print('Failed PRs: an error occurred:', e)
197200

198201

199-
def update_squad_and_title(conn, cur, table_name, rtc):
200-
print(f"Updating squads and titles with {rtc}...")
202+
def update_squad_and_title(cur_dict, conn_table, cur_table, rtctable, opentable):
203+
print(f"Updating squads and titles in {opentable}...")
201204
try:
202-
cur.execute(f"SELECT * FROM {table_name};")
203-
failed_prs_rows = cur.fetchall()
205+
cur_table.execute(f"SELECT * FROM {opentable};")
206+
failed_prs_rows = cur_table.fetchall()
204207

205208
for row in failed_prs_rows:
206-
cur.execute(
207-
f"""UPDATE {table_name}
208-
SET "Service Name" = rtc."Title", "Squad" = rtc."Category"
209-
FROM {rtc} AS rtc
210-
WHERE {table_name}."Service Name" = rtc."Repository"
211-
AND {table_name}.id = %s;""",
212-
(row[0],)
213-
)
214-
cur.execute(
215-
f"""UPDATE {table_name}
216-
SET "Squad" = 'Other'
217-
WHERE {table_name}."Service Name" IN ('doc-exports', 'docs_on_docs', 'docsportal')
218-
AND {table_name}.id = %s;""",
219-
(row[0],)
209+
service_name_index = 1
210+
id_index = 0
211+
212+
cur_dict.execute(
213+
f"""SELECT "Title", "Category"
214+
FROM {rtctable}
215+
WHERE "Repository" = %s;""",
216+
(row[service_name_index],)
220217
)
221-
conn.commit()
218+
rtc_row = cur_dict.fetchone()
219+
220+
if rtc_row:
221+
cur_table.execute(
222+
f"""UPDATE {opentable}
223+
SET "Service Name" = %s, "Squad" = %s
224+
WHERE id = %s;""",
225+
(rtc_row[0], rtc_row[1], row[id_index])
226+
)
227+
228+
if row[service_name_index] in ('doc-exports', 'docs_on_docs', 'docsportal'):
229+
cur_table.execute(
230+
f"""UPDATE {opentable}
231+
SET "Squad" = 'Other'
232+
WHERE id = %s;""",
233+
(row[id_index],)
234+
)
235+
236+
conn_table.commit()
222237

223238
except Exception as e:
224239
print(f"Error updating squad and title: {e}")
225-
conn.rollback()
240+
conn_table.rollback()
226241

227242

228243
def main(org, table_name, rtc):
229244
check_env_variables()
230245

231-
conn = connect_to_db(db_name)
232-
cur = conn.cursor()
246+
conn_zuul = connect_to_db(db_name)
247+
cur_zuul = conn_zuul.cursor()
248+
conn_csv = connect_to_db(db_csv)
249+
cur_csv = conn_csv.cursor()
233250

234-
cur.execute(f"DROP TABLE IF EXISTS {table_name}")
235-
conn.commit()
251+
cur_csv.execute(f"DROP TABLE IF EXISTS {table_name}")
252+
conn_csv.commit()
236253

237-
create_prs_table(conn, cur, table_name)
254+
create_prs_table(conn_zuul, cur_zuul, table_name)
238255

239256
repos = get_repos(org, gitea_token)
240257

241258
print("Gathering PRs info...")
242259
for repo in repos:
243-
get_failed_prs(org, repo, gitea_token, conn, cur, table_name)
260+
get_failed_prs(org, repo, gitea_token, conn_zuul, cur_zuul, table_name)
244261

245-
update_squad_and_title(conn, cur, table_name, rtc)
262+
update_squad_and_title(cur_csv, conn_zuul, cur_zuul, rtc, table_name)
246263

247-
cur.close()
248-
conn.close()
264+
cur_csv.close()
265+
conn_csv.close()
266+
cur_zuul.close()
267+
conn_zuul.close()
268+
269+
270+
if __name__ == "__main__":
271+
org_string = "docs"
272+
failed_table = "open_prs"
273+
rtc_table = "repo_title_category"
274+
275+
main(org_string, failed_table, rtc_table)
276+
main(f"{org_string}-swiss", f"{failed_table}_swiss", f"{rtc_table}_swiss")
249277

250278

251279
if __name__ == "__main__":

gitea_info.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -440,33 +440,45 @@ def get_github_open_prs(github_org, conn_csv, cur_csv, opentable, string):
440440
print('Github PRs: an error occurred:', e)
441441

442442

443-
def update_squad_and_title(conn_csv, cur_csv, rtctable, opentable):
444-
print("Updating squads and titles...")
443+
def update_squad_and_title(cur_csv, conn_table, cur_table, rtctable, opentable):
444+
print(f"Updating squads and titles in {opentable}...")
445445
try:
446-
cur_csv.execute("SELECT * FROM open_prs;")
447-
open_issues_rows = cur_csv.fetchall()
446+
cur_table.execute(f"SELECT * FROM {opentable};") # Use Zuul cursor here
447+
failed_prs_rows = cur_table.fetchall()
448+
449+
for row in failed_prs_rows:
450+
service_name_index = 1
451+
id_index = 0
448452

449-
for row in open_issues_rows:
450-
cur_csv.execute(
451-
f"""UPDATE {opentable}
452-
SET "Service Name" = rtc."Title", "Squad" = rtc."Category"
453-
FROM {rtctable} AS rtc
454-
WHERE {opentable}."Service Name" = rtc."Repository"
455-
AND {opentable}.id = %s;""",
456-
(row[0],)
457-
)
458453
cur_csv.execute(
459-
f"""UPDATE {opentable}
460-
SET "Squad" = 'Other'
461-
WHERE {opentable}."Service Name" IN ('doc-exports', 'docs_on_docs', 'docsportal')
462-
AND {opentable}.id = %s;""",
463-
(row[0],)
454+
f"""SELECT "Title", "Category"
455+
FROM {rtctable}
456+
WHERE "Repository" = %s;""",
457+
(str(row[service_name_index]),)
464458
)
465-
conn_csv.commit()
459+
rtc_row = cur_csv.fetchone()
460+
461+
if rtc_row:
462+
cur_table.execute(
463+
f"""UPDATE {opentable}
464+
SET "Service Name" = %s, "Squad" = %s
465+
WHERE id = %s;""",
466+
(rtc_row[0], rtc_row[1], row[id_index])
467+
)
468+
469+
if row[service_name_index] in ('doc-exports', 'docs_on_docs', 'docsportal'):
470+
cur_table.execute(
471+
f"""UPDATE {opentable}
472+
SET "Squad" = 'Other'
473+
WHERE id = %s;""",
474+
(row[id_index],)
475+
)
476+
477+
conn_table.commit()
466478

467479
except Exception as e:
468480
print(f"Error updating squad and title: {e}")
469-
conn_csv.rollback()
481+
conn_table.rollback()
470482

471483

472484
def main(org, gh_org, rtctable, opentable, string):
@@ -475,7 +487,8 @@ def main(org, gh_org, rtctable, opentable, string):
475487

476488
conn_csv = connect_to_db(db_csv)
477489
cur_csv = conn_csv.cursor()
478-
490+
conn_orph = connect_to_db(db_orph)
491+
cur_orph = conn_orph.cursor()
479492
g = Github(github_token)
480493
github_org = g.get_organization(gh_org)
481494

@@ -488,29 +501,23 @@ def main(org, gh_org, rtctable, opentable, string):
488501
print("Gathering parent PRs...")
489502
for repo in repos:
490503
get_parent_pr(org, repo)
491-
492504
get_pull_requests(org, "doc-exports")
493505

494506
update_service_titles(cur_csv, rtctable)
495507
add_squad_column(cur_csv, rtctable)
496508

497-
conn_orph = connect_to_db(db_orph)
498-
cur_orph = conn_orph.cursor()
499-
500509
cur_orph.execute(f"DROP TABLE IF EXISTS {opentable}")
501510
conn_orph.commit()
502-
503511
create_prs_table(conn_orph, cur_orph, opentable)
504512
compare_csv_files(conn_csv, cur_csv, conn_orph, cur_orph, opentable)
505-
506513
csv_erase()
507514

508515
get_github_open_prs(github_org, conn_csv, cur_csv, opentable, string)
509-
update_squad_and_title(conn_csv, cur_csv, rtctable, opentable)
516+
517+
update_squad_and_title(cur_csv, conn_orph, cur_orph, rtctable, opentable)
510518

511519
cur_csv.close()
512520
conn_csv.close()
513-
514521
cur_orph.close()
515522
conn_orph.close()
516523

0 commit comments

Comments
 (0)