pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
        buildDiscarder logRotator(
          artifactDaysToKeepStr: '30',
          artifactNumToKeepStr: '100',
          daysToKeepStr: '365',
          numToKeepStr: '1000'
        )
        timeout(time: 6, unit: 'HOURS')
    }

    parameters {
        choice(name: 'Environment', choices: ['prod', 'qa'], description: 'The environment to run against.')
        choice(name: 'flow_name', choices: [
        'amazon_digital_services',
        'amazon_instant_video',
        'amazon_music',
        'amazon_unlimited_marketshare',
        'apple_music_streams',
        'awa',
        'bandsintown',
        'beatport',
        'deezer',
        'fandango_now',
        'gfk',
        'google_play',
        'itunes',
        'itunes_marketshare',
        'kugou',
        'kuwo',
        'line',
        'mrc',
        'music_analytics_reports',
        'pandora',
        'playstation_video',
        'qq',
        'ritmogestion',
        'soundcloud',
        'spotify',
        'spotify_charts',
        'spotify_track_metrics',
        'tiktok',
        'tiktok_weekly',
        'vevo',
        'wynk',
        'xbox',
        'youtube_asset',
        'youtube_bulk_reports',
        'youtube_claim',
        'youtube_facts',
        'youtube_neo4j',
        'youtube_video'])
        choice(name: 'licensor', choices: [
        'sme',
        'theorchard',
        'awal',
        'smej',
        'smejp',
        'smejpintl',
        'altafonte'])
        string(name: 'period_start', description: 'start date (inclusive) of period to ingest. Format YYYY-MM-DD')
        string(name: 'period_end', description: 'end date (inclusive) of period to ingest. Format YYYY-MM-DD')
        choice(name: 'reload', choices: [
        'None',
        'full',
        'soft'],
        description: 'if "None" then no reload forced. The flow uses ingestion status to decide if it need to be reloaded. full - sends "reload: "True" to context which clears all statuses from DynamoDb soft - sends "soft_reload: "True" to flow context and clears task_status related to facts table in order they to be re-executed.')
        string(name: 'context', defaultValue: '{}', description: 'default context to be used as initial for each date in the period. Example: {"countries": "US,FI"} or {"report_name": "adsupported"}')
        booleanParam(name: 'cancel', defaultValue: false, description: 'when selected then scheduled but not yet started tasks within matching pariod/licensor/flow will be deleted')
    }

    environment {
        CREDENTIALS_ID = "${params.Environment == 'prod' ? 'prod-feed-ingestion-exec-creds' : 'qa-feed-ingestion-exec-creds'}"
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@master"
            }
        }
        stage('Run exec job') {
            when {
                expression {
                    params.Environment == 'qa' || params.Environment == 'prod'
                }
            }
            steps {
                    withCredentials([
                        aws(credentialsId: env.CREDENTIALS_ID,
                        accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                        secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'),
                        usernamePassword(credentialsId: 'swf_analytics_ingest_data_backload_snowflake_credentials',
                        usernameVariable: 'SNOWFLAKE_USER',
                        passwordVariable: 'SNOWFLAKE_PASSWORD'),
                        string(credentialsId: 'swf_analytics_ingest_data_backload_exec_user_key', variable: 'SNOWFLAKE_KEY')
                    ])
                    {
                        dockerRun(
                            imageTag: 'latest',
                            script: 'python feed_ingestion/bin/backload_scheduler.py',
                            imageRepo: '086679231553.dkr.ecr.us-east-1.amazonaws.com/swf-feed-ingestion',
                            envVars: [
                                ENVIRONMENT: "'${params.Environment}'",
                                LICENSOR: "'${params.licensor}'",
                                FLOW: "'${params.flow_name}'",
                                PERIOD_START: "${params.period_start}",
                                PERIOD_END: "'${params.period_end}'",
                                RELOAD: "'${params.reload}'",
                                CONTEXT: "'${params.context}'",
                                CANCEL: "'${params.cancel}'",
                                AWS_ACCESS_KEY_ID: "'${AWS_ACCESS_KEY_ID}'",
                                AWS_SECRET_ACCESS_KEY: "'${AWS_SECRET_ACCESS_KEY}'",
                                AWS_DEFAULT_REGION: 'us-east-1',
                                SNOWFLAKE_SCHEMA: "'${params.Environment}'",
                                SNOWFLAKE_ACCOUNT: "delphi.us-east-1",
                                SNOWFLAKE_ROLE: "swf_analytics_ingest_data_backload_exec_role",
                                SNOWFLAKE_DATABASE: "FACTS",
                                SNOWFLAKE_WAREHOUSE: "${params.Environment}_ETL_WAREHOUSE",
                                SNOWFLAKE_USER: "'${SNOWFLAKE_USER}'",
                                SNOWFLAKE_PASSWORD: "'${SNOWFLAKE_PASSWORD}'",
                                SNOWFLAKE_KEY: "'${SNOWFLAKE_KEY}'",
                                FEED_INGESTION_TABLE: "${params.Environment}_feed_ingestion_status",
                            ]
                    )
                }
            }
        }
    }
}

