pdf_report.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from fpdf import FPDF
  2. import os
  3. from datetime import datetime
  4. import logging
  5. logger = logging.getLogger(__name__)
  6. logger.addHandler(logging.StreamHandler())
  7. class ReportPDF(FPDF):
  8. def __init__(self, repository_name, start_date, end_date):
  9. FPDF.__init__(self,orientation='P',unit='mm',format='A4')
  10. self.repo = repository_name
  11. self.start_end = f"{datetime.strptime(start_date, '%Y-%m-%d').strftime('%b %d, %Y')} to {datetime.strptime(end_date, '%Y-%m-%d').strftime('%b %d, %Y')}"
  12. def header(self):
  13. self.set_font('Arial', 'B', 12)
  14. self.cell(100, 10, f'AutoTriage Report: {self.repo}', 0, 0)
  15. self.cell(90, 10, self.start_end, 0, 0, 'R')
  16. self.ln(20)
  17. def footer(self):
  18. self.set_y(-15)
  19. self.set_font('Arial', 'I', 8)
  20. self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
  21. def exec_summary(self, text):
  22. self.set_font('Arial', 'B', 16)
  23. self.cell(0, 8, 'Executive Summary', 'B', 0, 'L')
  24. self.ln(10)
  25. self.set_font('Arial', '', 10)
  26. self.multi_cell(0, 5, text)
  27. self.ln(10)
  28. def add_challenge(self, challenge_data):
  29. # title
  30. self.set_font('Arial', '', 14)
  31. self.cell(0, 10, f"{challenge_data['key_challenge']}", 0, 0, 'L')
  32. self.ln(8)
  33. # psosible causes
  34. self.set_font('Arial', 'B', 10)
  35. self.cell(0, 10, "Possible Causes", 0, 0, 'L')
  36. self.ln(5)
  37. self.set_font('Arial', '', 10)
  38. x_list = challenge_data['possible_causes']
  39. if isinstance(x_list, str):
  40. x_list = x_list.split(',')
  41. for x in x_list:
  42. self.cell(0, 10, "* " + x, 0, 0, 'L')
  43. self.ln(5)
  44. self.ln(3)
  45. # remediations
  46. self.set_font('Arial', 'B', 10)
  47. self.cell(0, 10, "Remediations", 0, 0, 'L')
  48. self.ln(5)
  49. self.set_font('Arial', '', 10)
  50. x_list = challenge_data['remediations']
  51. if isinstance(x_list, str):
  52. x_list = x_list.split(',')
  53. for x in x_list:
  54. self.cell(0, 10, "* " + x, 0, 0, 'L')
  55. self.ln(5)
  56. self.ln(3)
  57. # affected issues
  58. self.set_font('Arial', 'B', 10)
  59. self.cell(30, 10, f"Affected issues: ", 0, 0, 'L')
  60. x_list = challenge_data['affected_issues']
  61. if isinstance(x_list, str):
  62. x_list = x_list.split(',')
  63. for iss in x_list:
  64. self.set_text_color(0,0,255)
  65. self.cell(12, 10, str(iss), 0, 0, 'L', link=f"https://github.com/{self.repo}/issues/{iss}")
  66. self.set_text_color(0,0,0)
  67. self.ln(15)
  68. def challenges_section(self, key_challenges_data):
  69. self.set_font('Arial', 'B', 16)
  70. self.cell(0, 8, 'Key Challenges', 'B', 0, 'L')
  71. self.ln(10)
  72. for cd in key_challenges_data:
  73. self.add_challenge(cd)
  74. self.ln(20)
  75. def open_ques_section(self, open_questions):
  76. self.set_font('Arial', 'B', 16)
  77. self.cell(0, 8, 'Open Questions', 'B', 0, 'L')
  78. self.ln(10)
  79. self.set_font('Arial', '', 10)
  80. if isinstance(open_questions, str):
  81. open_questions = open_questions.split(',')
  82. for qq in open_questions:
  83. self.multi_cell(0, 5, "* " + qq, 0, 0, 'L')
  84. self.ln(5)
  85. self.ln(5)
  86. def add_graphs_section(self, title, plot_paths):
  87. self.set_font('Arial', 'B', 16)
  88. self.cell(0, 8, f'[Viz] {title}', 'B', 0, 'L')
  89. self.ln(10)
  90. for path in plot_paths:
  91. if os.path.exists(path):
  92. self.add_plot(path)
  93. else:
  94. self.set_font('Arial', 'BI', 10)
  95. self.cell(0, 8, '< Plot not found, make sure you have push-acces to this repo >', 0, 0)
  96. self.ln(10)
  97. def add_plot(self, img):
  98. self.image(img, x=30, w=150)
  99. self.ln(5)
  100. def create_report_pdf(repo_name, start_date, end_date, key_challenges_data, executive_summary, open_questions, out_folder):#, image1, image2):
  101. out_path = f'{out_folder}/report.pdf'
  102. logger.info(f"Creating PDF report at {out_path}")
  103. pdf = ReportPDF(repo_name, start_date, end_date)
  104. pdf.add_page()
  105. pdf.exec_summary(executive_summary)
  106. pdf.open_ques_section(open_questions)
  107. pdf.challenges_section(key_challenges_data)
  108. pdf.add_page()
  109. pdf.add_graphs_section("Repo Maintenance", [f'{out_folder}/plots/engagement_sankey.png'])
  110. pdf.add_page()
  111. pdf.add_graphs_section("Traffic in the last 2 weeks", [f'{out_folder}/plots/{x}.png' for x in ['views_clones','resources', 'referrers']])
  112. pdf.add_page()
  113. pdf.add_graphs_section("New issues in the last 2 weeks", [f'{out_folder}/plots/{x}.png' for x in ['themes', 'severity', 'sentiment', 'expertise']])
  114. pdf.output(out_path, 'F')