#!/bin/sh

# 训练新模型，并使用后面的数据计算AUC，评估模型效果

set -x

begin_date=$1
end_date=$2
model_name=$3
train_dim=$4
hdfs_path=$5

PROJECT_HOME=/root/zhaohp/recommend-emr-dataprocess/
HADOOP=/opt/apps/HADOOP-COMMON/hadoop-common-current/bin/hadoop
MODEL_PATH=${PROJECT_HOME}/model/recommend/
PREDICT_PATH=${PROJECT_HOME}/predict/recommend/

FM_TRAIN=/root/sunmingze/alphaFM/bin/fm_train


train_date=$begin_date
train_end_time=$(date -d "$end_date +1 day" +%Y%m%d)

main() {

    # 增量训练模型

    while [ "$train_date" != "$train_end_time" ]; do
        echo "==================== 开始训练 $train_date 模型 ===================="

        if [ "$train_date" == 20240801 ]; then
            echo -e "\t\t 无效的数据分区: $train_date, 跳过"
        elif [ "$train_date" == "$begin_date" ]; then
            $HADOOP fs -text ${hdfs_path}/${train_date}/* | ${FM_TRAIN} -m ${MODEL_PATH}/${model_name}_${train_date}.txt -dim ${train_dim} -core 8
        else
            if [ "$train_date" == 20240801 ]; then
                yesterday=$(date -d "$train_date -2 day" +%Y%m%d)
            else
                yesterday=$(date -d "$train_date -1 day" +%Y%m%d)
            fi
            $HADOOP fs -text ${hdfs_path}/${train_date}/* | ${FM_TRAIN} -m ${MODEL_PATH}/${model_name}_${train_date}.txt -dim ${train_dim} -core 8 -im ${MODEL_PATH}/${model_name}_${yesterday}.txt
        fi

        echo -e "==================== 训练 $train_date 模型结束 ====================\n\n\n\n\n\n"

        train_date=$(date -d "$train_date +1 day" +%Y%m%d)
    done

}

main

