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