From 4a6edcc170104ee56803fa8e0d5bc3176b9f8a31 Mon Sep 17 00:00:00 2001 From: Charlie Wang <2144018+kingman@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:19:14 +0100 Subject: [PATCH] new view for aggregated stat on purchase propensity predictions (#268) --- .../terraform/modules/monitor/main.tf | 28 +++++++++++++ ...hase_propensity_smart_bidding_view.sql.tpl | 41 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 templates/purchase_propensity_smart_bidding_view.sql.tpl diff --git a/infrastructure/terraform/modules/monitor/main.tf b/infrastructure/terraform/modules/monitor/main.tf index ae94c891..4129828b 100644 --- a/infrastructure/terraform/modules/monitor/main.tf +++ b/infrastructure/terraform/modules/monitor/main.tf @@ -28,6 +28,8 @@ locals { activation_project_url = "${local.p_key}=${var.activation_project_id}" mds_dataform_repo = "marketing-analytics" + + purchase_propensity_dataset = "purchase_propensity" } module "project_services" { @@ -259,3 +261,29 @@ data "template_file" "looker_studio_dashboard_url" { dataflow_log_table_id = local.dataflow_log_table_id } } + +data "template_file" "purchase_propensity_prediction_stats_query" { + template = file("${local.source_root_dir}/templates/purchase_propensity_smart_bidding_view.sql.tpl") + vars = { + project_id = var.feature_store_project_id + purchase_propensity_dataset = local.purchase_propensity_dataset + activation_dataset = "activation" + } +} + +data "google_bigquery_dataset" "purchase_propensity_dataset" { + dataset_id = local.purchase_propensity_dataset + project = var.feature_store_project_id +} + +resource "google_bigquery_table" "purchase_propensity_prediction_stats" { + project = var.feature_store_project_id + dataset_id = data.google_bigquery_dataset.purchase_propensity_dataset.dataset_id + table_id = "purchase_propensity_prediction_stats" + deletion_protection = false + + view { + query = data.template_file.purchase_propensity_prediction_stats_query.rendered + use_legacy_sql = false + } +} diff --git a/templates/purchase_propensity_smart_bidding_view.sql.tpl b/templates/purchase_propensity_smart_bidding_view.sql.tpl new file mode 100644 index 00000000..f3891c15 --- /dev/null +++ b/templates/purchase_propensity_smart_bidding_view.sql.tpl @@ -0,0 +1,41 @@ +-- Copyright 2024 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +SELECT + p_stat.inference_date, + p_stat.p_p_decile, + p_stat.number_of_users, + conf.value*p_stat.number_of_users AS predicted_purchase_value +FROM ( + SELECT + inference_date, + p_p_decile, + COUNT(p_p_decile) AS number_of_users + FROM ( + SELECT + PARSE_DATE('%Y_%m_%d', SUBSTR(_TABLE_SUFFIX, 1,10)) AS inference_date, + NTILE(10) OVER (PARTITION BY _TABLE_SUFFIX ORDER BY b.prediction_prob DESC) AS p_p_decile, + FROM + `${project_id}.${purchase_propensity_dataset}.predictions_*` b + WHERE + ENDS_WITH(_TABLE_SUFFIX, '_view') ) + GROUP BY + inference_date, + p_p_decile ) AS p_stat +JOIN + `${project_id}.${activation_dataset}.vbb_activation_configuration` conf +ON + p_stat.p_p_decile = decile +WHERE + conf.activation_type = 'purchase-propensity' \ No newline at end of file