#!/usr/bin/env bash

set -e

scope="test"

if  [ "$scope" == "test" ]
then 
    import_from_mysql_command="mysql -ustaging -htidb.e23bdccc.cb17aaf2.us-west-2.prod.aws.tidbcloud.com -P4000 -pZqfjTO5G_CJx%AdiqFuyVS6P staging_central"
    export_to_mysql_command="mysql --host 127.0.0.1 --port 4000 -u root tmp_dw"
    countrycode_file="/tmp/dwcountrycode_file_test"
    tmp_sql_file="/tmp/dwtmpsqlfile_test"
    awk_script_tenant="/tmp/dwawkscript_test_tenant.awk"
    awk_script_cluster="/tmp/dwawkscript_test_cluster.awk"
    awk_script_backup="/tmp/dwawkscript_test_backup.awk"
    awk_script_restore="/tmp/dwawkscript_test_restore.awk"
    awk_script_import="/tmp/dwawkscript_test_import.awk"
    awk_script_cluster_scaling="/tmp/dwawkscript_test_scaling.awk"
    awk_script_df_jobs="/tmp/dwawkscript_df_jobs.awk"
    tmp_counter_file="/tmp/dwtmpcounterfile_test"
    alias date='gdate'
    sleep_time="10"
    unit="1"
elif [ "$scope" == "prod" ]
then
    import_from_mysql_command="mysql -hmetadb-tidb -P4000 dbaas"
    export_to_mysql_command="mysql -uapplication -h52.8.176.243 -P4000 -pDzORXVx5sr6faWkeoIfe tmp_dw"
    countrycode_file="/tmp/dwcountrycode_file_prod"
    tmp_sql_file="/tmp/dwtmpsqlfile_prod"
    awk_script_tenant="/tmp/dwawkscript_prod_tenant.awk"
    awk_script_cluster="/tmp/dwawkscript_prod_cluster.awk"
    awk_script_backup="/tmp/dwawkscript_prod_backup.awk"
    awk_script_restore="/tmp/dwawkscript_prod_restore.awk"
    awk_script_import="/tmp/dwawkscript_prod_import.awk"
    awk_script_cluster_scaling="/tmp/dwawkscript_prod_scaling.awk"
    awk_script_df_jobs="/tmp/dwawkscript_df_jobs.awk"
    tmp_counter_file="/tmp/dwtmpcounterfile_prod"
    sleep_time="900"
    unit="500"
fi



cat > $countrycode_file << EOF
    AD,EMEA
    AE,EMEA
    AF,EMEA
    AG,NA
    AI,NA
    AL,EMEA
    AM,EMEA
    AO,EMEA
    AQ,EMEA
    AR,NA
    AS,NA
    AT,EMEA
    AU,APAC
    AW,NA
    AX,EMEA
    AZ,EMEA
    BA,EMEA
    BB,NA
    BD,APAC
    BE,EMEA
    BF,EMEA
    BG,EMEA
    BH,EMEA
    BI,EMEA
    BJ,EMEA
    BL,EMEA
    BM,NA
    BN,APAC
    BO,NA
    BQ,EMEA
    BR,NA
    BS,NA
    BT,APAC
    BV,EMEA
    BW,EMEA
    BY,EMEA
    BZ,NA
    CA,NA
    CC,APAC
    CD,EMEA
    CF,EMEA
    CG,EMEA
    CH,EMEA
    CI,EMEA
    CK,EMEA
    CL,NA
    CM,EMEA
    CN,CO
    CO,NA
    CR,NA
    CU,NA
    CV,EMEA
    CW,EMEA
    CX,APAC
    CY,EMEA
    CZ,EMEA
    DE,EMEA
    DJ,EMEA
    DK,EMEA
    DM,NA
    DO,NA
    DZ,EMEA
    EC,NA
    EE,EMEA
    EG,EMEA
    EH,EMEA
    ER,EMEA
    ES,EMEA
    ET,EMEA
    FI,EMEA
    FJ,APAC
    FK,NA
    FM,APAC
    FO,EMEA
    FR,EMEA
    GA,EMEA
    GB,EMEA
    GD,NA
    GE,EMEA
    GF,EMEA
    GG,EMEA
    GH,EMEA
    GI,EMEA
    GL,EMEA
    GM,EMEA
    GN,EMEA
    GP,EMEA
    GQ,EMEA
    GR,EMEA
    GS,NA
    GT,NA
    GU,NA
    GW,EMEA
    GY,NA
    HK,CO
    HM,EMEA
    HN,NA
    HR,EMEA
    HT,NA
    HU,EMEA
    ID,APAC
    IE,EMEA
    IL,EMEA
    IM,EMEA
    IN,APAC
    IO,EMEA
    IQ,EMEA
    IR,EMEA
    IS,EMEA
    IT,EMEA
    JE,EMEA
    JM,NA
    JO,EMEA
    JP,Japan
    KE,EMEA
    KG,EMEA
    KH,APAC
    KI,APAC
    KM,EMEA
    KN,NA
    KP,APAC
    KR,APAC
    KW,EMEA
    KY,NA
    KZ,EMEA
    LA,APAC
    LB,EMEA
    LC,NA
    LI,EMEA
    LK,APAC
    LR,EMEA
    LS,EMEA
    LT,EMEA
    LU,EMEA
    LV,EMEA
    LY,EMEA
    MA,EMEA
    MC,EMEA
    MD,EMEA
    ME,EMEA
    MF,EMEA
    MG,EMEA
    MH,APAC
    MK,EMEA
    ML,EMEA
    MM,APAC
    MN,APAC
    MO,CO
    MP,APAC
    MQ,EMEA
    MR,EMEA
    MS,NA
    MT,EMEA
    MU,EMEA
    MV,APAC
    MW,EMEA
    MX,NA
    MY,APAC
    MZ,EMEA
    NA,EMEA
    NC,EMEA
    NE,EMEA
    NF,APAC
    NG,EMEA
    NI,NA
    NL,EMEA
    NO,EMEA
    NP,APAC
    NR,APAC
    NU,APAC
    NZ,APAC
    OM,EMEA
    PA,NA
    PE,NA
    PF,EMEA
    PG,APAC
    PH,APAC
    PK,APAC
    PL,EMEA
    PM,EMEA
    PN,EMEA
    PR,NA
    PS,EMEA
    PT,EMEA
    PW,APAC
    PY,NA
    QA,EMEA
    RE,EMEA
    RO,EMEA
    RS,EMEA
    RU,EMEA
    RW,EMEA
    SA,EMEA
    SB,APAC
    SC,EMEA
    SD,EMEA
    SE,EMEA
    SG,APAC
    SH,EMEA
    SI,EMEA
    SJ,EMEA
    SK,EMEA
    SL,EMEA
    SM,EMEA
    SN,EMEA
    SO,EMEA
    SR,NA
    SS,EMEA
    ST,EMEA
    SV,NA
    SX,EMEA
    SY,EMEA
    SZ,EMEA
    TC,NA
    TD,EMEA
    TF,EMEA
    TG,EMEA
    TH,APAC
    TJ,EMEA
    TK,APAC
    TL,APAC
    TM,EMEA
    TN,EMEA
    TO,APAC
    TR,EMEA
    TT,NA
    TV,APAC
    TW,CO
    TZ,EMEA
    UA,EMEA
    UG,EMEA
    UM,NA
    US,NA
    UY,NA
    UZ,EMEA
    VA,EMEA
    VC,NA
    VE,NA
    VG,NA
    VI,NA
    VN,APAC
    VU,APAC
    WF,EMEA
    WS,APAC
    YE,EMEA
    YT,EMEA
    ZA,EMEA
    ZM,EMEA
    ZW,EMEA
EOF

cat > $awk_script_tenant << EOF
BEGIN {
    sql_stmt = "insert ignore into tmp_tenants(fetch_time, fetch_unixtime, region, id, created_at, updated_at, name, alias, status, plan, kind, accept_service, zendesk_id, third_party_account_provider, third_party_account_code, bind_credit_card_at, account_manager) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt ")"
}
END {
    if (NR !=0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

cat > $awk_script_cluster << EOF
BEGIN {
    sql_stmt = "insert into tmp_clusters(fetch_time, fetch_unixtime, region, tenant_id, tenant_name, id, created_at, updated_at, deleted_time, creation_finished_time, name, created_by_id, status, replicas, provider_id, deploy_type, poc_deleted, free, project_id, import_id, replication_id, read_only, tcu_number) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt  ")"
}
END {
    if (NR != 0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

cat > $awk_script_backup << EOF
BEGIN {
    sql_stmt = "insert into tmp_backups(fetch_time, fetch_unixtime, region, tenant_id, tenant_name, id, created_at, updated_at, deleted_at, finished_at, backup_schedule_id, backup_size, targets, cluster_version, status, cleaned_up, old_backup_name, start_time_stamp, cluster_snapshot_id, schedule_type, cluster_id) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt  ")"
}
END {
    if (NR != 0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

cat > $awk_script_restore << EOF
BEGIN{
    sql_stmt = "insert into tmp_restores(fetch_time, fetch_unixtime, region, tenant_id, tenant_name, id, created_at, updated_at, name, finished_at, cluster_id, targets, backup_id, status) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt  ")"
}
END {
    if (NR != 0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

cat > $awk_script_import << EOF
BEGIN {
    sql_stmt = "insert into tmp_imports(fetch_time, fetch_unixtime, region, tenant_id, tenant_name, id, created_at, updated_at, old_id, cluster_id, status, message, started_at, finished_at, clean_up, bucket_region, aws_role_arn, data_format, data_source_type, filter, custom_csv_format, total_size, total_files, source_url, completed_tables, pending_tables, completed_percent) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt  ")"
}
END {
    if (NR != 0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

cat > $awk_script_cluster_scaling << EOF
BEGIN {
    sql_stmt = "insert into tmp_scalings(fetch_time, fetch_unixtime, region, tenant_id, tenant_name, id, created_at, updated_at, completed_at, status, cluster_id, type) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt  ")"
}
END {
    if (NR != 0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

cat > $awk_script_df_jobs << EOF
BEGIN{
    sql_stmt = "insert into tmp_df_jobs(fetch_time, fetch_unixtime, region, tenant_id, cluster_id, project_id, id, created_at, updated_at, finished_at, deleted_at, phase, provider, one_time, service_code) values"
}
{
    if (NR == 1)
        sql_stmt = sql_stmt "("
    else
        sql_stmt = sql_stmt ",("
    for (i = 1; i <= NF; i ++) {
        if (\$i != "NULL") {
            gsub(/'/, "\\\\'", \$i)
            sql_stmt = sql_stmt  "'"  \$i  "'"
        }
        else 
            sql_stmt = sql_stmt  "NULL"
        if (i != NF) 
            sql_stmt = sql_stmt  ","
    }
    sql_stmt = sql_stmt  ")"
}
END {
    if (NR != 0) {
        sql_stmt = sql_stmt ";"
        print sql_stmt
    }
}
EOF

appendRegionInfo() {
while  IFS= read -r  line
do
    country=$(echo "$line" | cut -f1)
    #country="SG"
    if [ -z "$country" ];
    then
        region="unknown"
    else
        region=$(grep -i "$country," $countrycode_file | sed 's/^[ \t]*//g' | awk -F ',' '{print $2}')
        if [ -z "$region" ];
        then 
            region="unknown"
        fi
    fi
    echo  "$line" | awk -F "\t" -v OFS="\t" "{\$1=\"$region\";print \$0 }"   
done
}

fetch_tenant_sql_prod="
(select 
    t.country, t.id, t.created_at, t.updated_at, t.name, t.alias, t.status, tp.plan, t.kind, t.accept_service, t.zendesk_id, t.third_party_account_provider, t.third_party_account_code, t.bind_credit_card_at, t.account_manager
from
    tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
where 
    ((not lower(t.name) like '%pingcap%' ) and  tp.plan = 'on_demand' and t.kind = 'external') or (t.id = 1372813089187041280)
)
union
(select 
    t.country, t.id, t.created_at, t.updated_at, t.name, t.alias, t.status, tp.plan, t.kind, t.accept_service, t.zendesk_id, t.third_party_account_provider, t.third_party_account_code, t.bind_credit_card_at, t.account_manager
from
    tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
where 
    ((not lower(t.name) like '%pingcap%' ) and  tp.plan = 'poc' and t.kind = 'external')
);"

fetch_tenant_sql_test="select 
    country, id, created_at, updated_at, name, alias, status, plan_deleted_column, kind, accept_service, zendesk_id, third_party_account_provider, third_party_account_code, bind_credit_card_at, account_manager
from
    tenants
limit 3;"

fetch_cluster_sql_prod="select
    t.country, t.id, t.name, c.id, c.created_at, c.updated_at, c.deleted_time, c.creation_finished_time, c.name, c.created_by_id, c.status, c.replicas, c.provider_id, c.deploy_type, c.poc_deleted, c.free, c.project_id, c.import_id, c.replication_id, c.read_only, c.tcu_number
from 
    clusters c join tenants t ON c.tenant_id = t.id
where
    c.tenant_id in (
        select 
            t.id
        from
            tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
        where 
            ((not lower(t.name) like '%pingcap%' ) and  tp.plan in ('poc', 'on_demand') and t.kind = 'external') 
        limit 
            $unit 
        offset 
            FETCH_OFFSET
        )
    or
    c.id = 1379661944632784068;"

fetch_cluster_sql_test="select
    t.country, t.id, t.name, c.id, c.created_at, c.updated_at, c.deleted_time, c.creation_finished_time, c.name, c.created_by_id, c.status, c.replicas, c.provider_id, c.deploy_type, c.poc_deleted, c.free, c.project_id, c.import_id, c.replication_id, c.read_only, c.tcu_number
from 
    clusters c join tenants t on c.tenant_id = t.id 
where
    t.country is not NULL and t.country <> '' and t.id in(
        select 
            id
        from
            tenants
        limit $unit offset FETCH_OFFSET
    )
limit 3;"

fetch_backup_sql_prod="select
    t.country, t.id, t.name, b.id, b.created_at, b.updated_at, b.deleted_at, b.finished_at, b.backup_schedule_id,b.backup_size,b.targets,b.cluster_version,b.status,b.cleaned_up,b.old_backup_name,b.start_time_stamp,b.cluster_snapshot_id,bs.type,bs.cluster_id
from
    backups b JOIN backup_schedules bs ON b.backup_schedule_id = bs.id JOIN clusters c ON c.id = bs.cluster_id JOIN tenants t ON c.tenant_id = t.id
where 
    c.tenant_id in (
        select 
            t.id
        from
            tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
        where 
            (not lower(t.name) like '%pingcap%' ) and  tp.plan in ('poc', 'on_demand') and t.kind = 'external' limit $unit offset FETCH_OFFSET 
        )
    or
    c.id = 1379661944632784068;"

fetch_backup_sql_test="select
    t.country, t.id, t.name, b.id, b.created_at, b.updated_at, b.deleted_at, b.finished_at, b.backup_schedule_id,b.backup_size,b.targets,b.cluster_version,b.status,b.cleaned_up,b.old_backup_name,b.start_time_stamp,b.cluster_snapshot_id,bs.type, bs.cluster_id
from
    backups b JOIN backup_schedules bs ON b.backup_schedule_id = bs.id JOIN clusters c ON c.id = bs.cluster_id JOIN tenants t ON c.tenant_id = t.id 
where
    t.country <> '' and t.id in (
        select 
            id
        from
            tenants
        limit $unit offset FETCH_OFFSET
    )
limit 3;"

fetch_restore_sql_prod="select
    t.country, t.id, t.name, r.id, r.created_at, r.updated_at, r.name, r.finished_at, r.cluster_id, r.targets, r.backup_id, r.status
from
    restores r JOIN clusters c ON c.id = r.cluster_id  JOIN tenants t ON c.tenant_id = t.id 
where
    c.tenant_id in (
        select 
            t.id
        from
            tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
        where 
            (not lower(t.name) like '%pingcap%' ) and  tp.plan in ('poc', 'on_demand') and t.kind = 'external' limit $unit offset FETCH_OFFSET
    )
    or
    c.id = 1379661944632784068;"

fetch_restore_sql_test="select
    t.country, t.id, t.name, r.id, r.created_at, r.updated_at, r.name, r.finished_at, r.cluster_id, r.targets, r.backup_id, r.status
from
    restores r JOIN clusters c ON c.id = r.cluster_id JOIN tenants t ON c.tenant_id = t.id 
where
    t.country <> '' and t.id in (
        select 
            id
        from
            tenants
        limit $unit offset FETCH_OFFSET
    )
limit 3;" 

fetch_import_sql_prod="select
    t.country, t.id, t.name, i.id, i.created_at, i.updated_at, i.old_id, i.cluster_id, i.status, i.message, i.started_at, i.finished_at, i.clean_up, i.bucket_region, i.aws_role_arn, i.data_format, i.data_source_type, i.filter, i.custom_csv_format, i.total_size, i.total_files, i.source_url, i.completed_tables,i.pending_tables,i.completed_percent
from
    imports  i join clusters c ON c.id = i.cluster_id JOIN tenants t ON c.tenant_id = t.id
where
    c.tenant_id in (
        select 
            t.id
        from
            tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
        where 
            (not lower(t.name) like '%pingcap%' ) and  tp.plan in ('poc', 'on_demand') and t.kind = 'external' limit $unit offset FETCH_OFFSET
    )
    or
    c.id = 1379661944632784068;"

fetch_import_sql_test="select
    t.country, t.id, t.name, i.id, i.created_at, i.updated_at, i.old_id, i.cluster_id, i.status, i.message, i.started_at, i.finished_at, i.clean_up, i.bucket_region, i.aws_role_arn, i.data_format, i.data_source_type, i.filter, i.custom_csv_format, i.total_size, i.total_files, i.source_url, i.completed_tables,i.pending_tables,i.completed_percent
from
    imports  i join clusters c ON c.id = i.cluster_id JOIN tenants t ON c.tenant_id = t.id 
where 
    t.country <> '' and t.id in (
        select
            id
        from
            tenants
        limit $unit offset FETCH_OFFSET
    )
limit 3;"

fetch_cluster_scaling_sql_prod="select
    t.country, t.id, t.name, co.id, co.created_at, co.updated_at, co.completed_at, co.status, c.id, co.type
from 
    cluster_operations co join clusters c on co.cluster_id = c.id JOIN tenants t ON c.tenant_id = t.id
where
    c.tenant_id in (
        select 
            t.id
        from
            tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
        where 
            (not lower(t.name) like '%pingcap%' ) and  tp.plan in ('poc', 'on_demand') and t.kind = 'external' limit $unit offset FETCH_OFFSET
    )
    or
    c.id = 1379661944632784068;"

fetch_cluster_scaling_sql_test="select
    t.country, t.id, t.name, co.id, co.created_at, co.updated_at, co.completed_at, co.status, c.id, co.type
from
    cluster_operations co join clusters c on co.cluster_id = c.id join tenants t on c.tenant_id = t.id
where
    t.country <> '' and t.id in (
        select 
            id
        from
            tenants
        limit $unit offset FETCH_OFFSET
    )
limit 3"

fetch_df_jobs_prod="select
    region, tenant_id, cluster_id, project_id, id, created_at, updated_at, finished_at, deleted_at, phase, provider, one_time, service_code 
from
    df_jobs
where
    tenant_id in (
        select 
            t.id
        from
            tenants t join billing.tenant_plans tp on t.id = tp.tenant_id
        where
            (not lower(t.name) like '%pingcap%' ) and  tp.plan in ('poc', 'on_demand') and t.kind = 'external'
        limit $unit offset FETCH_OFFSET
    )
    or
    cluster_id = 1379661944632784068;"

fetch_df_jobs_test="
select
    region, tenant_id, cluster_id, project_id, id, created_at, updated_at, finished_at, deleted_at, phase, provider, one_time, service_code 
from
    df_jobs
limit 10;
"

if [ "$scope" == "test" ]
then 
    fetch_tenant_sql="$fetch_tenant_sql_test"
    fetch_cluster_sql="$fetch_cluster_sql_test"
    fetch_backup_sql="$fetch_backup_sql_test"
    fetch_restore_sql="$fetch_restore_sql_test"
    fetch_import_sql="$fetch_import_sql_test"
    fetch_cluster_scaling_sql="$fetch_cluster_scaling_sql_test"
    fetch_df_jobs_sql="$fetch_df_jobs_test"
elif [ "$scope" == "prod" ]
then
    fetch_tenant_sql="$fetch_tenant_sql_prod"
    fetch_cluster_sql="$fetch_cluster_sql_prod"
    fetch_backup_sql="$fetch_backup_sql_prod"
    fetch_restore_sql="$fetch_restore_sql_prod"
    fetch_import_sql="$fetch_import_sql_prod"
    fetch_cluster_scaling_sql="$fetch_cluster_scaling_sql_prod"
    fetch_df_jobs_sql="$fetch_df_jobs_prod"
fi

while true; do
    new_ts="$(date -u '+%Y-%m-%d %T')"
    unix_ts=$(date --date "$new_ts" +'%s')
    echo $unix_ts
    echo "$fetch_tenant_sql" | $import_from_mysql_command | appendRegionInfo | awk 'NR != 1 {print}' | tee $tmp_counter_file | sed "s/^/$new_ts\t$unix_ts\t/"  | awk -F "\t" -f "$awk_script_tenant" > $tmp_sql_file
    #cat $tmp_sql_file | $export_to_mysql_command 
    linecount="$(awk 'END{print NR}' $tmp_counter_file)"
    divides="$(($linecount / $unit))"
    for i in `seq 0 $divides`;do
        echo $i
        offset="$(($i * $unit))"
        echo "$fetch_cluster_sql" | sed "s/FETCH_OFFSET/$offset/" | $import_from_mysql_command | appendRegionInfo |awk 'NR != 1 {print}' | sed "s/^/$new_ts\t$unix_ts\t/" | awk -F "\t" -f "$awk_script_cluster" >> $tmp_sql_file 
        #cat $tmp_sql_file | $export_to_mysql_command 
        echo "$fetch_backup_sql" | sed "s/FETCH_OFFSET/$offset/" | $import_from_mysql_command | appendRegionInfo |awk 'NR != 1 {print}' | sed "s/^/$new_ts\t$unix_ts\t/" | awk -F "\t" -f "$awk_script_backup" >> $tmp_sql_file 
        #cat $tmp_sql_file | $export_to_mysql_command 
        echo "$fetch_restore_sql" | sed "s/FETCH_OFFSET/$offset/" | $import_from_mysql_command | appendRegionInfo |awk 'NR != 1 {print}' | sed "s/^/$new_ts\t$unix_ts\t/" | awk -F "\t" -f "$awk_script_restore" >> $tmp_sql_file 
        #cat $tmp_sql_file | $export_to_mysql_command 
        echo "$fetch_import_sql" | sed "s/FETCH_OFFSET/$offset/" | $import_from_mysql_command |awk 'NR != 1 {print}' | appendRegionInfo | sed "s/^/$new_ts\t$unix_ts\t/" | awk -F "\t" -f "$awk_script_import" >> $tmp_sql_file 
        #echo "$fetch_import_sql" | $import_from_mysql_command | awk 'NR != 1 {print}' > ./testfile
        #cat ./testfile | appendRegionInfo
        #cat $tmp_sql_file | $export_to_mysql_command 
        echo "$fetch_cluster_scaling_sql" | sed "s/FETCH_OFFSET/$offset/" | $import_from_mysql_command | awk 'NR != 1 {print}' | appendRegionInfo | sed "s/^/$new_ts\t$unix_ts\t/" | awk -F "\t" -f "$awk_script_cluster_scaling" >> $tmp_sql_file
        #cat $tmp_sql_file | $export_to_mysql_command
        echo "$fetch_df_jobs_sql" | sed "s/FETCH_OFFSET/$offset/" | $import_from_mysql_command | awk 'NR != 1 {print}' | sed "s/^/$new_ts\t$unix_ts\t/" | awk -F "\t" -f "$awk_script_df_jobs" >> $tmp_sql_file
    done
    finish_ts="$(date -u '+%Y-%m-%d %T')"
    echo "insert into tmp_timestamps values('$new_ts', '$finish_ts')" >> $tmp_sql_file
    if [ "$scope" == "test" ];then
        cat $tmp_sql_file |$export_to_mysql_command 
    else
        cat $tmp_sql_file |$export_to_mysql_command
    fi
    echo "done"
    sleep $sleep_time
done
