Skip to content

Commit 44a0518

Browse files
authored
Merge pull request #53 from koichiro/feature/fxi-slack-report
Fix slack report
2 parents 8ab3cc8 + cc2c40e commit 44a0518

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

lib/airbnb_payous/bigquery_gateway.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,48 @@ def load_and_merge!(rows:)
6464
load_job.wait_until_done!
6565
raise load_job.error if load_job.failed?
6666

67-
total_rows = load_job.output_rows
68-
@logger.info("Loaded #{total_rows} rows to staging table.")
67+
# Use job output rows, fallback to input rows count if zero (sometimes stats are delayed)
68+
total_rows_loaded = load_job.output_rows.to_i
69+
total_rows_loaded = rows.length if total_rows_loaded.zero?
70+
71+
@logger.info("Load job finished. output_rows: #{load_job.output_rows}, rows.length: #{rows.length}. Using #{total_rows_loaded} as reference.")
6972

73+
# Fetch a fresh table reference to avoid caching issues
7074
target_table = dataset.table(table_id)
7175
result = { inserted_count: 0, updated_count: 0 }
7276

7377
if target_table.nil?
74-
@logger.info("Target table #{qualified_table_name(table_id)} not found. Creating it for the first time.")
78+
@logger.info("Target table #{qualified_table_name(table_id)} does not exist. Path: :create_table")
7579
copy_job = @bigquery.copy_job qualified_table_name(staging_table_id), qualified_table_name(table_id), write: "truncate"
7680
copy_job.wait_until_done!
7781
raise copy_job.error if copy_job.failed?
78-
@logger.info("Target table created successfully.")
82+
@logger.info("Target table created via copy_job.")
7983

8084
result[:mode] = :create_table
81-
result[:inserted_count] = total_rows
85+
result[:inserted_count] = total_rows_loaded
8286
else
87+
@logger.info("Target table #{qualified_table_name(table_id)} exists. Path: :merge")
8388
merge_sql = build_merge_query(rows.first.keys)
8489
query_job = @bigquery.query_job(merge_sql)
8590
query_job.wait_until_done!
8691
raise query_job.error if query_job.failed?
8792

88-
inserted_count = query_job.respond_to?(:dml_stats) && query_job.dml_stats ? query_job.dml_stats.inserted_row_count.to_i : 0
89-
updated_count = query_job.respond_to?(:dml_stats) && query_job.dml_stats ? query_job.dml_stats.updated_row_count.to_i : 0
90-
@logger.info("MERGE operation completed. Rows inserted: #{inserted_count}, Rows updated: #{updated_count}.")
93+
# Capture DML stats
94+
if query_job.respond_to?(:dml_stats) && query_job.dml_stats
95+
result[:inserted_count] = query_job.dml_stats.inserted_row_count.to_i
96+
result[:updated_count] = query_job.dml_stats.updated_row_count.to_i
97+
@logger.info("DML Stats - Inserted: #{result[:inserted_count]}, Updated: #{result[:updated_count]}")
98+
else
99+
# Fallback: if dml_stats is nil, try to get from raw statistics or use total affected
100+
affected = query_job.num_dml_affected_rows.to_i
101+
@logger.warn("DML Stats not found. num_dml_affected_rows: #{affected}")
102+
result[:inserted_count] = affected
103+
end
91104

92105
result[:mode] = :merge
93-
result[:inserted_count] = inserted_count
94-
result[:updated_count] = updated_count
95106
end
107+
108+
@logger.info("Final result for notification: #{result.inspect}")
96109
result
97110
ensure
98111
temp_file&.close!

test/bigquery_gateway_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def dml_stats
9999
@dml_stats
100100
end
101101

102+
def num_dml_affected_rows
103+
@dml_stats.inserted_row_count + @dml_stats.updated_row_count
104+
end
105+
102106
def error; nil; end
103107
end
104108

0 commit comments

Comments
 (0)