|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import functions_framework |
| 16 | +from google.cloud import bigquery |
| 17 | +import logging |
| 18 | +from flask import jsonify |
| 19 | +import os |
| 20 | + |
| 21 | +# Initialize BigQuery Client |
| 22 | +try: |
| 23 | + bq_client = bigquery.Client() |
| 24 | +except Exception as e: |
| 25 | + logging.warning(f"Failed to initialize BigQuery client: {e}") |
| 26 | + bq_client = None |
| 27 | + |
| 28 | +BQ_DATASET_ID = os.environ.get('BQ_DATASET_ID') |
| 29 | +SPANNER_PROJECT_ID = os.environ.get('SPANNER_PROJECT_ID') |
| 30 | +SPANNER_INSTANCE_ID = os.environ.get('SPANNER_INSTANCE_ID') |
| 31 | +SPANNER_DATABASE_ID = os.environ.get('SPANNER_DATABASE_ID') |
| 32 | +GCS_BUCKET_ID = os.environ.get('GCS_BUCKET_ID') |
| 33 | + |
| 34 | + |
| 35 | +@functions_framework.http |
| 36 | +def aggregation_helper(request): |
| 37 | + """ |
| 38 | + HTTP Cloud Function that takes importName and runs a BQ query. |
| 39 | + """ |
| 40 | + if not bq_client: |
| 41 | + return ('BigQuery client not initialized', 500) |
| 42 | + |
| 43 | + request_json = request.get_json(silent=True) |
| 44 | + if not request_json: |
| 45 | + return ('Request is not a valid JSON', 400) |
| 46 | + |
| 47 | + import_list = request_json.get('importList') |
| 48 | + if not import_list: |
| 49 | + return ("'importList' parameter is missing", 400) |
| 50 | + |
| 51 | + logging.info(f"Received request for importList: {import_list}") |
| 52 | + |
| 53 | + results = [] |
| 54 | + |
| 55 | + try: |
| 56 | + for import_item in import_list: |
| 57 | + import_name = import_item.get('importName') |
| 58 | + if not import_name: |
| 59 | + logging.warning( |
| 60 | + f"Skipping item without importName: {import_item}") |
| 61 | + continue |
| 62 | + |
| 63 | + query = None |
| 64 | + # Define specific queries based on importName |
| 65 | + if "india_census" in import_name: |
| 66 | + # Placeholder for India Census specific logic |
| 67 | + query = """ |
| 68 | + SELECT @import_name as import_name, CURRENT_TIMESTAMP() as execution_time |
| 69 | + """ |
| 70 | + elif "us_census" in import_name: |
| 71 | + # Placeholder for US Census specific logic |
| 72 | + query = """ |
| 73 | + SELECT @import_name as import_name, CURRENT_TIMESTAMP() as execution_time |
| 74 | + """ |
| 75 | + else: |
| 76 | + logging.info( |
| 77 | + f"No specific aggregation logic for import: {import_name}") |
| 78 | + continue |
| 79 | + |
| 80 | + if query: |
| 81 | + job_config = bigquery.QueryJobConfig(query_parameters=[ |
| 82 | + bigquery.ScalarQueryParameter("import_name", "STRING", |
| 83 | + import_name), |
| 84 | + ]) |
| 85 | + query_job = bq_client.query(query, job_config=job_config) |
| 86 | + query_results = query_job.result() |
| 87 | + for row in query_results: |
| 88 | + results.append(dict(row)) |
| 89 | + |
| 90 | + return jsonify({"status": "success"}), 200 |
| 91 | + |
| 92 | + except Exception as e: |
| 93 | + logging.error(f"Aggregation failed: {e}") |
| 94 | + return (f"Aggregation failed: {str(e)}", 500) |
0 commit comments