send_build_stats.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. import datetime
  3. import os
  4. import ydb
  5. import uuid
  6. import subprocess
  7. YDBD_PATH = "ydb/apps/ydbd/ydbd"
  8. FROM_ENV_COLUMNS = [
  9. "github_head_ref",
  10. "github_workflow",
  11. "github_workflow_ref",
  12. "github_sha",
  13. "github_repository",
  14. "github_event_name",
  15. "github_ref_type",
  16. "github_ref_name",
  17. "github_ref",
  18. ]
  19. STRING_COLUMNS = FROM_ENV_COLUMNS + [
  20. "id",
  21. "git_commit_message",
  22. "binary_path",
  23. "build_preset",
  24. ]
  25. DATETIME_COLUMNS = [
  26. "git_commit_time",
  27. ]
  28. UINT64_COLUMNS = [
  29. "size_bytes",
  30. "size_stripped_bytes",
  31. ]
  32. ALL_COLUMNS = STRING_COLUMNS + DATETIME_COLUMNS + UINT64_COLUMNS
  33. def sanitize_str(s):
  34. # YDB SDK expects bytes for 'String' columns
  35. if s is None:
  36. s = "N\A"
  37. return s.encode("utf-8")
  38. def main():
  39. if "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS" not in os.environ:
  40. print("Env variable CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS is missing, skipping")
  41. return 1
  42. # Do not set up 'real' variable from gh workflows because it interfere with ydb tests
  43. # So, set up it locally
  44. os.environ["YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"] = os.environ["CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"]
  45. with ydb.Driver(
  46. endpoint="grpcs://ydb.serverless.yandexcloud.net:2135",
  47. database="/ru-central1/b1ggceeul2pkher8vhb6/etn6d1qbals0c29ho4lf",
  48. credentials=ydb.credentials_from_env_variables()
  49. ) as driver:
  50. driver.wait(timeout=10, fail_fast=True)
  51. session = ydb.retry_operation_sync(
  52. lambda: driver.table_client.session().create()
  53. )
  54. with session.transaction() as tx:
  55. text_query_builder = []
  56. for type_ in STRING_COLUMNS:
  57. text_query_builder.append("DECLARE ${} as String;".format(type_))
  58. for type_ in UINT64_COLUMNS:
  59. text_query_builder.append("DECLARE ${} as Uint64;".format(type_))
  60. for type_ in DATETIME_COLUMNS:
  61. text_query_builder.append("DECLARE ${} as Datetime;".format(type_))
  62. text_query_builder.append(
  63. """INSERT INTO binary_size
  64. (
  65. {}
  66. )
  67. VALUES
  68. (
  69. {}
  70. );
  71. """.format(
  72. ", \n ".join(ALL_COLUMNS),
  73. ", \n ".join(["$" + column for column in ALL_COLUMNS]),
  74. )
  75. )
  76. text_query = "\n".join(text_query_builder)
  77. prepared_query = session.prepare(text_query)
  78. binary_size_bytes = subprocess.check_output(
  79. ["bash", "-c", "cat {} | wc -c".format(YDBD_PATH)]
  80. )
  81. binary_size_stripped_bytes = subprocess.check_output(
  82. ["bash", "-c", "./ya tool strip {} -o - | wc -c".format(YDBD_PATH)]
  83. )
  84. build_preset = os.environ.get("build_preset", None)
  85. github_sha = os.environ.get("GITHUB_SHA", None)
  86. if github_sha is not None:
  87. git_commit_time_bytes = subprocess.check_output(
  88. ["git", "show", "--no-patch", "--format=%cI", github_sha]
  89. )
  90. git_commit_message_bytes = subprocess.check_output(
  91. ["git", "log", "--format=%s", "-n", "1", github_sha]
  92. )
  93. git_commit_time = datetime.datetime.fromisoformat(
  94. git_commit_time_bytes.decode("utf-8").strip()
  95. )
  96. git_commit_message = git_commit_message_bytes.decode("utf-8").strip()
  97. git_commit_time_unix = int(git_commit_time.timestamp())
  98. else:
  99. git_commit_time = None
  100. git_commit_message = None
  101. git_commit_time_unix = 0
  102. parameters = {
  103. "$id": sanitize_str(str(uuid.uuid4())),
  104. "$build_preset": sanitize_str(build_preset),
  105. "$binary_path": sanitize_str(YDBD_PATH),
  106. "$size_stripped_bytes": int(binary_size_stripped_bytes.decode("utf-8")),
  107. "$size_bytes": int(binary_size_bytes.decode("utf-8")),
  108. "$git_commit_time": git_commit_time_unix,
  109. "$git_commit_message": sanitize_str(git_commit_message),
  110. }
  111. for column in FROM_ENV_COLUMNS:
  112. value = os.environ.get(column.upper(), None)
  113. parameters["$" + column] = sanitize_str(value)
  114. print("Executing query:\n{}".format(text_query))
  115. print("With parameters:")
  116. for k, v in parameters.items():
  117. print("{}: {}".format(k, v))
  118. tx.execute(prepared_query, parameters, commit_tx=True)
  119. if __name__ == "__main__":
  120. exit(main())