pipeline {
    agent any

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

    parameters {
        choice(name: 'ENVIRONMENT', choices: ['qa', 'prod'])
        booleanParam(name: 'WAIT_UNTIL_COMPLETE', defaultValue: true)
        booleanParam(name: 'DAILY_RUN', defaultValue: false)
        booleanParam(name: 'WITH_CHARTS', defaultValue: false, description: 'include charts ingestion starting from -1 day')
        booleanParam(name: 'SKIP_SOCIALS', defaultValue: false, description: 'to skip socials feeds')
    }

    triggers {
        parameterizedCron(
            '''
                    # social chartmetric ingestion - runs every hour at :30 after
                    # social chartmetric backfill- runs daily at 11:30PM Eastern
                    # all store chartmetric ingestion - runs at 4:30 am, 10:30 am, 4:30 pm, 10:30 pm Eastern
                    # youtube-neo4j ingestion runs daily at 11:30PM Eastern

                    45 3,5,10,11,15,17,21 * * * % ENVIRONMENT=prod;WAIT_UNTIL_COMPLETE=true;DAILY_RUN=false;WITH_CHARTS=false
                    45 1,4,7,9,13,16,19,22 * * * % ENVIRONMENT=prod;WAIT_UNTIL_COMPLETE=true;DAILY_RUN=false;WITH_CHARTS=true
                    00 21 * * * % ENVIRONMENT=prod;WAIT_UNTIL_COMPLETE=true;DAILY_RUN=true;WITH_CHARTS=false
            '''
        )
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                script {
                    currentBuild.displayName = "${params.ENVIRONMENT} DAILY_RUN: ${params.DAILY_RUN} WITH_CHARTS: ${params.WITH_CHARTS}"
                }
                library "jenkins-global-libraries@master"
            }
        }

        stage('swf-chartmetric-charts-exec') {
           when {
                expression {
                    params.WITH_CHARTS == true
                }
            }
            options {
                timeout(time: 120, unit: 'MINUTES')
            }
            steps {
                script {
                    build job: 'swf-chartmetric-charts-exec',
                    propagate: true,
                    wait: true,
                    parameters: [
                        string(name: 'ENVIRONMENT', value: params.ENVIRONMENT),
                        string(name: 'DAYS_BACK', value: '3'),
                        string(name: 'PLATFORM_NAMES', value: 'appleMusic,amazon,deezer,linemusic,shazam,soundcloud,recochoku,tiktok,youtube'),
                        booleanParam(name: 'KAFKA', value: true),
                        booleanParam(name: 'WAIT_UNTIL_COMPLETE', value: params.WAIT_UNTIL_COMPLETE),
                    ]
                }
            }
        }
        stage('swf-chartmetric_participants-exec') {
            options {
                timeout(time: 120, unit: 'MINUTES')
            }
            steps {
                script {
                    build job: 'swf-chartmetric-participants-exec',
                    propagate: true,
                    wait: true,
                    parameters: [
                        string(name: 'ENVIRONMENT', value: params.ENVIRONMENT),
                        booleanParam(name: 'WAIT_UNTIL_COMPLETE', value: params.WAIT_UNTIL_COMPLETE),
                    ]
                }
            }
        }
        stage('socials') {
            options {
                timeout(time: 120, unit: 'MINUTES')
            }
            when {
                expression { params.SKIP_SOCIALS == false }
            }
            steps {
                script {
                    build job: 'swf-chartmetric-socials-exec',
                    propagate: true,
                    wait: true,
                    parameters: [
                        string(name: 'ENVIRONMENT', value: params.ENVIRONMENT),
                        booleanParam(name: 'WAIT_UNTIL_COMPLETE', value: params.WAIT_UNTIL_COMPLETE),
                    ]
                }
                script {
                    build job: 'redis-cache-clear',
                    propagate: true,
                    wait: true,
                    parameters: [
                        string(name: 'REDIS_SERVER', value: 'prod-ows-socials.oudhyr.ng.0001.use1.cache.amazonaws.com'),
                        string(name: 'KEY_PATTERN', value: ''),
                        booleanParam(name: 'WAIT_UNTIL_COMPLETE', value: params.WAIT_UNTIL_COMPLETE),
                    ]
                }
            }
        }
        stage('socials-backfill') {
           when {
                allOf {
                expression { params.SKIP_SOCIALS == false }
                expression { params.DAILY_RUN == true }
                }
            }
            options {
                timeout(time: 120, unit: 'MINUTES')
            }
            steps {
                script {
                    build job: 'swf-chartmetric-socials-backfill-exec',
                    propagate: true,
                    wait: true,
                    parameters: [
                        string(name: 'ENVIRONMENT', value: params.ENVIRONMENT),
                        booleanParam(name: 'WAIT_UNTIL_COMPLETE', value: params.WAIT_UNTIL_COMPLETE),
                    ]
                }
                script {
                    build job: 'redis-cache-clear',
                    propagate: true,
                    wait: true,
                    parameters: [
                        string(name: 'REDIS_SERVER', value: 'prod-ows-socials.oudhyr.ng.0001.use1.cache.amazonaws.com'),
                    ]
                }

            }
        }
        stage('swf-chartmetric-tracks-exec') {
            when {
                expression {
                    params.DAILY_RUN == true
                }
            }
            options {
                timeout(time: 180, unit: 'MINUTES')
            }
            steps {
                script {
                        build job: 'swf-chartmetric-tracks-exec',
                        propagate: true,
                        wait: true,
                        parameters: [
                            string(name: 'ENVIRONMENT', value: params.ENVIRONMENT),
                            booleanParam(name: 'WAIT_UNTIL_COMPLETE', value: params.WAIT_UNTIL_COMPLETE),
                        ]
                }
            }
        }
    }
    post {
        failure {
            script {
                if (params.ENVIRONMENT == 'prod') {
                    slackSend (
                        color: "danger",
                        channel: "#data-alerts",
                        message: """The build ${currentBuild.fullDisplayName} has failed.! ${env.JOB_NAME} - #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"""
                    )
                }
            }
        }
        aborted {
            script {
                if (params.ENVIRONMENT == 'prod') {
                    slackSend (
                        color: "danger",
                        channel: "#data-alerts",
                        message: """The build ${currentBuild.fullDisplayName} has failed.! ${env.JOB_NAME} - #${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"""
                    )
                }
            }
        }
    }
}
