diff --git a/procedures_js/alert_queries_runner.js b/procedures_js/alert_queries_runner.js index 54108de..616d75e 100644 --- a/procedures_js/alert_queries_runner.js +++ b/procedures_js/alert_queries_runner.js @@ -26,6 +26,10 @@ function exec(sqlText, binds = []) { return retval } +function randInt(n) { + return Math.floor(Math.random()*n) +} + function fillArray(value, len) { const arr = [] for (var i = 0; i < len; i++) { @@ -39,7 +43,7 @@ function defaultNullReference(columnAndType) { } const RUN_ID = Math.random().toString(36).substring(2).toUpperCase() -const RAW_ALERTS_TABLE = `${results_raw_alerts_table}` +const RAW_ALERTS_TABLE = `${results_raw_alerts_table}_${randInt(${alert_shards_count})}` const CREATE_ALERTS_SQL = `INSERT INTO $${RAW_ALERTS_TABLE} ( run_id, diff --git a/tables.tf b/tables.tf index 750475c..8f650e2 100644 --- a/tables.tf +++ b/tables.tf @@ -1,10 +1,10 @@ -resource "snowflake_table" "raw_alerts" { - count = var.create_tables ? 1 : 0 +resource "snowflake_table" "raw_alerts_tables" { + count = var.create_tables ? var.alert_shards_count : 0 provider = snowflake.alerting_role database = local.snowalert_database_name schema = local.results_schema - name = "RAW_ALERTS" + name = "RAW_ALERTS_${count.index}" change_tracking = true column { diff --git a/variables.tf b/variables.tf index 0bf1446..c6dc531 100644 --- a/variables.tf +++ b/variables.tf @@ -274,6 +274,11 @@ variable "enable_multiple_grants" { default = true } +variable "alert_shards_count" { + type = number + default = 2 +} + data "aws_caller_identity" "current" {} data "aws_region" "current" {} data "aws_partition" "current" {} diff --git a/views.tf b/views.tf index 6fc7587..b5a7f77 100644 --- a/views.tf +++ b/views.tf @@ -42,6 +42,26 @@ resource "snowflake_view" "alerts" { ] } +resource "snowflake_view" "raw_alerts" { + provider = snowflake.alerting_role + + database = local.snowalert_database_name + schema = local.results_schema + name = "RAW_ALERTS" + comment = "joins raw alert shards" + + statement = join( + " UNION ALL ", + [for table in [ + for i in range(var.alert_shards_count) : "raw_alerts_${i}"] : + "TABLE ${table}"] + ) + + depends_on = [ + local.raw_alerts_tables + ] +} + resource "snowflake_view" "violations" { provider = snowflake.alerting_role