get_test_history.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. import configparser
  3. import datetime
  4. import os
  5. import time
  6. import ydb
  7. dir = os.path.dirname(__file__)
  8. config = configparser.ConfigParser()
  9. config_file_path = f"{dir}/../../config/ydb_qa_db.ini"
  10. config.read(config_file_path)
  11. DATABASE_ENDPOINT = config["QA_DB"]["DATABASE_ENDPOINT"]
  12. DATABASE_PATH = config["QA_DB"]["DATABASE_PATH"]
  13. def get_test_history(test_names_array, last_n_runs_of_test_amount, build_type):
  14. if "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS" not in os.environ:
  15. print(
  16. "Error: Env variable CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS is missing, skipping"
  17. )
  18. return {}
  19. else:
  20. # Do not set up 'real' variable from gh workflows because it interfere with ydb tests
  21. # So, set up it locally
  22. os.environ["YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"] = os.environ[
  23. "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"
  24. ]
  25. results = {}
  26. with ydb.Driver(
  27. endpoint=DATABASE_ENDPOINT,
  28. database=DATABASE_PATH,
  29. credentials=ydb.credentials_from_env_variables(),
  30. ) as driver:
  31. driver.wait(timeout=10, fail_fast=True)
  32. session = ydb.retry_operation_sync(
  33. lambda: driver.table_client.session().create()
  34. )
  35. batch_size = 500
  36. start_time = time.time()
  37. for start in range(0, len(test_names_array), batch_size):
  38. test_names_batch = test_names_array[start:start + batch_size]
  39. history_query = f"""
  40. PRAGMA AnsiInForEmptyOrNullableItemsCollections;
  41. DECLARE $test_names AS List<Utf8>;
  42. DECLARE $rn_max AS Int32;
  43. DECLARE $build_type AS Utf8;
  44. $test_names=[{','.join("'{0}'".format(x) for x in test_names_batch)}];
  45. $rn_max = {last_n_runs_of_test_amount};
  46. $build_type = '{build_type}';
  47. $tests=(
  48. SELECT
  49. suite_folder ||'/' || test_name as full_name,test_name,build_type, commit, branch, run_timestamp, status, status_description,
  50. ROW_NUMBER() OVER (PARTITION BY test_name ORDER BY run_timestamp DESC) AS rn
  51. FROM
  52. `test_results/test_runs_column`
  53. where (job_name ='Nightly-run' or job_name like 'Postcommit%') and
  54. build_type = $build_type and
  55. suite_folder ||'/' || test_name in $test_names
  56. and status != 'skipped'
  57. );
  58. select full_name,test_name,build_type, commit, branch, run_timestamp, status, status_description,rn
  59. from $tests
  60. WHERE rn <= $rn_max
  61. ORDER BY test_name, run_timestamp;
  62. """
  63. query = ydb.ScanQuery(history_query, {})
  64. it = driver.table_client.scan_query(query)
  65. query_result = []
  66. while True:
  67. try:
  68. result = next(it)
  69. query_result = query_result + result.result_set.rows
  70. except StopIteration:
  71. break
  72. for row in query_result:
  73. if not row["full_name"].decode("utf-8") in results:
  74. results[row["full_name"].decode("utf-8")] = {}
  75. results[row["full_name"].decode("utf-8")][row["run_timestamp"]] = {
  76. "status": row["status"],
  77. "commit": row["commit"],
  78. "datetime": datetime.datetime.fromtimestamp(int(row["run_timestamp"] / 1000000)).strftime("%H:%m %B %d %Y"),
  79. "status_description": row["status_description"],
  80. }
  81. end_time = time.time()
  82. print(
  83. f'durations of getting history for {len(test_names_array)} tests :{end_time-start_time} sec')
  84. return results
  85. if __name__ == "__main__":
  86. get_test_history(test_names_array, last_n_runs_of_test_amount, build_type)