# Copyright 2022 Google LLC All Rights Reserved.
#
# 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.

##########################################################################
#
# NOTE:
# This script is used for Demo, Testing, and Debugging purposes. 
# The script will build and deploy a docker container that runs
# a Dataflow (Apache Beam) job in interactive mode using the DirectRunner.
#
##########################################################################

# Get Directory of this script
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Remove/clean any existing container
docker rmi -f scoring_engine_test
docker rm -f $(docker ps -a -f name=scoring_engine_test -q)

# Load Config
. ./config || . ../../config

echo "GCP_PROJECT_ID: $TF_VAR_GCP_PROJECT_ID"

# Build scoring engine container
docker build -t scoring_engine_test -f ${SCRIPT_DIR}/Dockerfile ${SCRIPT_DIR}/

# Deploy (interactive mode with DirectRunner)
docker run -it --name scoring_engine_test \
--env TF_VAR_GCP_PROJECT_ID=$TF_VAR_GCP_PROJECT_ID \
--env TF_VAR_DATAFLOW_REGION=$TF_VAR_DATAFLOW_REGION \
--env TF_VAR_GCS_BUCKET_DATAFLOW=$TF_VAR_GCS_BUCKET_DATAFLOW \
--env TF_VAR_PUBSUB_TOPIC_TEXT_INPUT=$TF_VAR_PUBSUB_TOPIC_TEXT_INPUT \
--env TF_VAR_PUBSUB_TOPIC_TEXT_SCORED=$TF_VAR_PUBSUB_TOPIC_TEXT_SCORED \
--env TF_VAR_PUBSUB_TOPIC_TOXIC=$TF_VAR_PUBSUB_TOPIC_TOXIC \
--env TF_VAR_BIGQUERY_DATASET=$TF_VAR_BIGQUERY_DATASET \
--env TF_VAR_BIGQUERY_TABLE=$TF_VAR_BIGQUERY_TABLE \
--env TF_VAR_WINDOW_DURATION_SECONDS=$TF_VAR_WINDOW_DURATION_SECONDS \
--env TF_VAR_WINDOW_SLIDING_SECONDS=$TF_VAR_WINDOW_SLIDING_SECONDS \
--env TF_VAR_DATAFLOW_RUNNER=DirectRunner \
--env TF_VAR_TOXIC_USER_THRESHOLD=$TF_VAR_TOXIC_USER_THRESHOLD \
--env TF_VAR_MODEL_API_KEY=$TF_VAR_MODEL_API_KEY \
scoring_engine_test
