node {
    checkout scm
    try {
        docker.image('s444452/ium:1.4').inside('-v /mlruns:/mlruns') {
        stage('Preparation') {
            properties([
                parameters([
                gitParameter(
                    branchFilter: 'origin/(.*)',
                    defaultValue: 'master',
                    description: 'Select branch',
                    name: 'BRANCH',
                    type: 'PT_BRANCH'
                ),
                buildSelector(
                    defaultSelector: upstream(),
                    description: 'Which build to use for copying artifacts',
                    name: 'BUILD_SELECTOR'
                ),
                string(
                    defaultValue: "0",
                    description: 'Build number',
                    name: 'BUILD_NR'
                ),
                string(
                    defaultValue: ".",
                    description: 'Data path',
                    name: 'DATA_PATH'
                ),
                string(
                    defaultValue: "1",
                    description: 'EPOCHS',
                    name: 'EPOCHS'
                ),
                string(
                    defaultValue: "20000",
                    description: 'Num words',
                    name: 'NUM_WORDS'
                ),
                string(
                    defaultValue: "150",
                    description: 'Batch size',
                    name: 'BATCH_SIZE'
                ),
                string(
                    defaultValue: "300",
                    description: 'Pad length',
                    name: 'PAD_LENGTH'
                )
                ])
            ])
        }
        stage('Copy artifacts') {
            copyArtifacts filter: 'train_data.csv', fingerprintArtifacts: true, projectName: 's444452-create-dataset'
            copyArtifacts filter: 'test_data.csv', fingerprintArtifacts: true, projectName: 's444452-create-dataset'
            copyArtifacts filter: 'neural_network_evaluation.csv', projectName: "s444452-evaluation/${params.BRANCH}", optional: true
            copyArtifacts filter: 'model/neural_net', projectName: "s444452-training/${params.BRANCH}", selector: buildParameter('BUILD_SELECTOR')
        }
        stage('Run script') {
            withEnv(["BUILD_NR=${params.BUILD_NR}","DATA_PATH=${params.DATA_PATH}","EPOCHS=${params.EPOCHS}",
                "NUM_WORDS=${params.NUM_WORDS}", "BATCH_SIZE=${params.BATCH_SIZE}","PAD_LENGTH=${params.PAD_LENGTH}"]) {
                sh "chmod u+x Scripts/evaluate_neural_network.py"
                sh "python3 Scripts/evaluate_neural_network.py $BUILD_NR $DATA_PATH $EPOCHS $NUM_WORDS $BATCH_SIZE $PAD_LENGTH"
            }
        }
        stage('Archive artifacts') {
            archiveArtifacts "neural_network_evaluation.csv, evaluation.png"
            archiveArtifacts "my_runs/**"
            archiveArtifacts "mlruns/**"
        }
        }
    } catch (e) {
        currentBuild.result = "FAILED"
        throw e
    } finally {
        notifyBuild(currentBuild.result)
    }
}
def notifyBuild(String buildStatus = 'STARTED') {
    buildStatus = buildStatus ?: 'SUCCESS'

    def subject = "Job: ${env.JOB_NAME}"
    def lastline = ""
    try {
        def filePath = readFile "${WORKSPACE}/neural_network_evaluation.csv"
        def lines = filePath.readLines()
        lastline = lines.get(lines.size()-1)
    }
    catch (e) {
        println(e.toString())
    }

    def build_params = "Path: ${params.DATA_PATH}, Epochs: ${params.EPOCHS}, Num_words: ${params.NUM_WORDS}, Batch_size: ${params.BATCH_SIZE}, Pad_length: ${params.PAD_LENGTH}"
    def details = "Build nr: ${env.BUILD_NUMBER}, status: ${buildStatus} \n url: ${env.BUILD_URL} \n build params: ${build_params} \n metrics: ${lastline}"

    emailext (
        subject: subject,
        body: details,
        to: 'e19191c5.uam.onmicrosoft.com@emea.teams.ms'
    )
}