migration_row_to_columt_tests.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python3
  2. # NOT USED ANYWHERE, YOU CAN DELETE THIS IF YOU KNOW WHAT ARE YOU DOING
  3. import argparse
  4. import configparser
  5. import datetime
  6. import os
  7. import posixpath
  8. import traceback
  9. import time
  10. import ydb
  11. from collections import Counter
  12. dir = os.path.dirname(__file__)
  13. config = configparser.ConfigParser()
  14. config_file_path = f"{dir}/../../config/ydb_qa_db.ini"
  15. config.read(config_file_path)
  16. build_preset = os.environ.get("build_preset")
  17. branch = os.environ.get("branch_to_compare")
  18. DATABASE_ENDPOINT = config["QA_DB"]["DATABASE_ENDPOINT"]
  19. DATABASE_PATH = config["QA_DB"]["DATABASE_PATH"]
  20. def create_tables(pool, table_path):
  21. print(f"> create table: {table_path}")
  22. def callee(session):
  23. session.execute_scheme(f"""
  24. CREATE table IF NOT EXISTS`{table_path}` (
  25. branch Utf8 NOT NULL,
  26. build_type Utf8 NOT NULL,
  27. commit Utf8 NOT NULL,
  28. duration Double,
  29. job_id Uint64,
  30. job_name Utf8,
  31. log Utf8,
  32. logsdir Utf8,
  33. owners Utf8,
  34. pull Utf8,
  35. run_timestamp Timestamp NOT NULL,
  36. status_description Utf8,
  37. status Utf8 NOT NULL,
  38. stderr Utf8,
  39. stdout Utf8,
  40. suite_folder Utf8 NOT NULL,
  41. test_id Utf8 NOT NULL,
  42. test_name Utf8 NOT NULL,
  43. PRIMARY KEY (`test_name`, `suite_folder`,build_type, status, run_timestamp)
  44. )
  45. PARTITION BY HASH(`test_name`, `suite_folder`, branch, build_type )
  46. WITH (STORE = COLUMN)
  47. """)
  48. return pool.retry_operation_sync(callee)
  49. def bulk_upsert(table_client, table_path, rows):
  50. print(f"> bulk upsert: {table_path}")
  51. column_types = (
  52. ydb.BulkUpsertColumns()
  53. .add_column("branch", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  54. .add_column("build_type", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  55. .add_column("commit", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  56. .add_column("duration", ydb.OptionalType(ydb.PrimitiveType.Double))
  57. .add_column("job_id", ydb.OptionalType(ydb.PrimitiveType.Uint64))
  58. .add_column("job_name", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  59. .add_column("log", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  60. .add_column("logsdir", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  61. .add_column("owners", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  62. .add_column("pull", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  63. .add_column("run_timestamp", ydb.OptionalType(ydb.PrimitiveType.Timestamp))
  64. .add_column("status", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  65. .add_column("status_description", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  66. .add_column("stderr", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  67. .add_column("stdout", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  68. .add_column("suite_folder", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  69. .add_column("test_id", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  70. .add_column("test_name", ydb.OptionalType(ydb.PrimitiveType.Utf8))
  71. )
  72. table_client.bulk_upsert(table_path, rows, column_types)
  73. def main():
  74. if "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS" not in os.environ:
  75. print(
  76. "Error: Env variable CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS is missing, skipping"
  77. )
  78. return 1
  79. else:
  80. # Do not set up 'real' variable from gh workflows because it interfere with ydb tests
  81. # So, set up it locally
  82. os.environ["YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"] = os.environ[
  83. "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"
  84. ]
  85. with ydb.Driver(
  86. endpoint=DATABASE_ENDPOINT,
  87. database=DATABASE_PATH,
  88. credentials=ydb.credentials_from_env_variables(),
  89. ) as driver:
  90. driver.wait(timeout=10, fail_fast=True)
  91. session = ydb.retry_operation_sync(
  92. lambda: driver.table_client.session().create()
  93. )
  94. # settings, paths, consts
  95. tc_settings = ydb.TableClientSettings().with_native_date_in_result_sets(enabled=True)
  96. table_client = ydb.TableClient(driver, tc_settings)
  97. table_path = 'test_results/test_runs_column'
  98. with ydb.SessionPool(driver) as pool:
  99. create_tables(pool, table_path)
  100. # geting last timestamp from runs column
  101. default_start_date = datetime.datetime(2024, 7, 1)
  102. last_date_query = f"select max(run_timestamp) as last_run_timestamp from `{table_path}`"
  103. query = ydb.ScanQuery(last_date_query, {})
  104. it = table_client.scan_query(query)
  105. results = []
  106. start_time = time.time()
  107. while True:
  108. try:
  109. result = next(it)
  110. results = results + result.result_set.rows
  111. except StopIteration:
  112. break
  113. end_time = time.time()
  114. print(f"transaction 'geting last timestamp from runs column' duration: {end_time - start_time}")
  115. if results[0] and results[0].get( 'max_date_window', default_start_date) is not None:
  116. last_date = results[0].get(
  117. 'max_date_window', default_start_date).strftime("%Y-%m-%dT%H:%M:%SZ")
  118. else:
  119. last_date = ddefault_start_date.strftime("%Y-%m-%dT%H:%M:%SZ")
  120. print(f'last run_datetime in table : {last_date}')
  121. # geting timestamp list from runs
  122. last_date_query = f"""select distinct run_timestamp from `test_results/test_runs_results`
  123. where run_timestamp >=Timestamp('{last_date}')"""
  124. query = ydb.ScanQuery(last_date_query, {})
  125. it = table_client.scan_query(query)
  126. timestamps = []
  127. start_time = time.time()
  128. while True:
  129. try:
  130. result = next(it)
  131. timestamps = timestamps + result.result_set.rows
  132. except StopIteration:
  133. break
  134. end_time = time.time()
  135. print(f"transaction 'geting timestamp list from runs' duration: {end_time - start_time}")
  136. print(f'count of timestamps : {len(timestamps)}')
  137. for ts in timestamps:
  138. # getting history for dates >= last_date
  139. query_get_runs = f"""
  140. select * from `test_results/test_runs_results`
  141. where run_timestamp = cast({ts['run_timestamp']} as Timestamp)
  142. """
  143. query = ydb.ScanQuery(query_get_runs, {})
  144. # start transaction time
  145. start_time = time.time()
  146. it = driver.table_client.scan_query(query)
  147. # end transaction time
  148. results = []
  149. prepared_for_update_rows = []
  150. while True:
  151. try:
  152. result = next(it)
  153. results = results + result.result_set.rows
  154. except StopIteration:
  155. break
  156. end_time = time.time()
  157. print(f'transaction duration: {end_time - start_time}')
  158. print(f'runs data captured, {len(results)} rows')
  159. for row in results:
  160. prepared_for_update_rows.append({
  161. 'branch': row['branch'],
  162. 'build_type': row['build_type'],
  163. 'commit': row['commit'],
  164. 'duration': row['duration'],
  165. 'job_id': row['job_id'],
  166. 'job_name': row['job_name'],
  167. 'log': row['log'],
  168. 'logsdir': row['logsdir'],
  169. 'owners': row['owners'],
  170. 'pull': row['pull'],
  171. 'run_timestamp': row['run_timestamp'],
  172. 'status_description': row['status_description'],
  173. 'status': row['status'],
  174. 'stderr': row['stderr'],
  175. 'stdout': row['stdout'],
  176. 'suite_folder': row['suite_folder'],
  177. 'test_id': row['test_id'],
  178. 'test_name': row['test_name'],
  179. })
  180. print('upserting runs')
  181. with ydb.SessionPool(driver) as pool:
  182. full_path = posixpath.join(DATABASE_PATH, table_path)
  183. bulk_upsert(driver.table_client, full_path,
  184. prepared_for_update_rows)
  185. print('history updated')
  186. if __name__ == "__main__":
  187. main()