get_build_diff.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import datetime
  3. from decimal import Decimal, ROUND_HALF_UP
  4. import get_current_build_size
  5. import get_main_build_size
  6. import humanize
  7. import os
  8. import subprocess
  9. # Форматирование числа
  10. def format_number(num):
  11. return humanize.intcomma(num).replace(",", " ")
  12. def bytes_to_human_iec(num):
  13. return humanize.naturalsize(num, binary=True)
  14. def main():
  15. yellow_treshold = int(os.environ.get("yellow_treshold"))
  16. red_treshold = int(os.environ.get("red_treshold"))
  17. github_srv = os.environ.get("GITHUB_SERVER_URL")
  18. repo_name = os.environ.get("GITHUB_REPOSITORY")
  19. repo_url = f"{github_srv}/{repo_name}/"
  20. branch = os.environ.get("branch_to_compare")
  21. current_pr_commit_sha = os.environ.get("commit_git_sha")
  22. if current_pr_commit_sha is not None:
  23. git_commit_time_bytes = subprocess.check_output(
  24. ["git", "show", "--no-patch", "--format=%cI", current_pr_commit_sha]
  25. )
  26. git_commit_time = datetime.datetime.fromisoformat(
  27. git_commit_time_bytes.decode("utf-8").strip()
  28. )
  29. git_commit_time_unix = int(git_commit_time.timestamp())
  30. else:
  31. print(f"Error: Cant get commit {current_pr_commit_sha} timestamp")
  32. return 1
  33. current_sizes_result = get_current_build_size.get_build_size()
  34. main_sizes_result = get_main_build_size.get_build_size(git_commit_time_unix)
  35. if main_sizes_result and current_sizes_result:
  36. main_github_sha = main_sizes_result["github_sha"]
  37. main_size_bytes = int(main_sizes_result["size_bytes"])
  38. main_size_stripped_bytes = int(main_sizes_result["size_stripped_bytes"])
  39. current_size_bytes = int(current_sizes_result["size_bytes"])
  40. current_size_stripped_bytes = int(current_sizes_result["size_stripped_bytes"])
  41. bytes_diff = current_size_bytes - main_size_bytes
  42. stripped_bytes_diff = current_size_stripped_bytes - main_size_stripped_bytes
  43. diff_perc = Decimal(bytes_diff * 100 / main_size_bytes).quantize(
  44. Decimal(".001"), rounding=ROUND_HALF_UP
  45. )
  46. stripped_diff_perc = Decimal(
  47. stripped_bytes_diff * 100 / main_size_stripped_bytes
  48. ).quantize(Decimal(".001"), rounding=ROUND_HALF_UP)
  49. human_readable_size = bytes_to_human_iec(current_size_bytes)
  50. human_readable_size_diff = bytes_to_human_iec(bytes_diff)
  51. human_readable_stripped_size_diff = bytes_to_human_iec(stripped_bytes_diff)
  52. if bytes_diff > 0:
  53. sign = "+"
  54. if bytes_diff >= red_treshold:
  55. color = "red"
  56. summary_core = f" >= {bytes_to_human_iec(red_treshold)} vs {branch}: **Alert**"
  57. elif bytes_diff >= yellow_treshold:
  58. color = "yellow"
  59. summary_core = f" >= {bytes_to_human_iec(yellow_treshold)} vs {branch}: **Warning**"
  60. else:
  61. color = "green"
  62. summary_core = f" < {bytes_to_human_iec(yellow_treshold)} vs {branch}: **OK**"
  63. else:
  64. sign = ""
  65. color = "green"
  66. summary_core = f" <= 0 Bytes vs {branch}: **OK**"
  67. if stripped_bytes_diff > 0:
  68. stripped_sign = "+"
  69. else:
  70. stripped_sign = ""
  71. summary_start = f"ydbd size **{human_readable_size}** changed* by **{sign}{human_readable_size_diff}**, which is"
  72. summary = f"{summary_start}{summary_core}"
  73. comment = (
  74. f"{summary}\n"
  75. f"|[ydbd size dash](https://datalens.yandex/cu6hzmpaki700)|{branch}: {main_github_sha} |merge: {current_pr_commit_sha} |diff | diff %%|\n"
  76. f"|:--- | ---: | ---: | ---: | ---: |\n"
  77. f"|ydbd size|**{format_number(main_size_bytes)}** Bytes |**{format_number(current_size_bytes)}** Bytes|**{sign}{human_readable_size_diff}**|**{sign}{diff_perc}%%**|\n"
  78. f"|ydbd stripped size|**{format_number(main_size_stripped_bytes)}** Bytes|**{format_number(current_size_stripped_bytes)}** Bytes|**{stripped_sign}{human_readable_stripped_size_diff}**|**{stripped_sign}{stripped_diff_perc}%%**|\n\n"
  79. f"<sup>*please be aware that the difference is based on comparing your commit and the last completed build from the post-commit, check [comparation]({repo_url}compare/{main_github_sha}..{current_pr_commit_sha})</sup>"
  80. )
  81. print(f"{color};;;{comment}")
  82. else:
  83. print(
  84. f"Error: Cant get build data: {branch}_sizes_result = {main_sizes_result}, current_sizes_result = {current_sizes_result}"
  85. )
  86. if __name__ == "__main__":
  87. main()