package com.speedledger.measure.jenkins;

import com.google.common.collect.Sets;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Plugin;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.ClientConfig;
import io.searchbox.core.Index;
import jenkins.model.Jenkins;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Send Jenkins build information to Elasticsearch.
 */
@Extension
public class BuildListener extends RunListener<Run> {
    private static final Logger LOG = Logger.getLogger(BuildListener.class.getName());

    private final JestClient jestClient;
    private Config config;

    public BuildListener() {
        LOG.info("Initializing");

        final JestClientFactory factory = new JestClientFactory();
        ClientConfig clientConfig = new ClientConfig.Builder(new ArrayList<String>()).multiThreaded(true).build();
        factory.setClientConfig(clientConfig);
        jestClient = factory.getObject();
    }

    public synchronized void loadConfig() {
        final Plugin plugin = Jenkins.getInstance().getPlugin("jenkins-elasticsearch");
        if (plugin instanceof ElasticsearchPlugin) {
            ElasticsearchPlugin elasticsearchPlugin = (ElasticsearchPlugin) plugin;

            Config newConfig = elasticsearchPlugin.getConfig();
            if (newConfig == null && config != null) {
                config = newConfig;
                LOG.info("Clearing configuration");
                jestClient.setServers(new HashSet<String>());
            } else if (newConfig != null && !newConfig.equals(config)) {
                config = newConfig;
                LOG.info("Applying new configuration: " + newConfig);
                jestClient.setServers(Sets.newHashSet(config.getUrl()));
            }
        }
    }

    @Override
    public void onCompleted(Run run, TaskListener listener) {
        loadConfig();

        final boolean validConfig = config != null && config.nonEmptyValues();
        if (validConfig) {
            try {
                final Build build = createBuild(run, listener);
                final Index index = new Index.Builder(build).index(config.getIndexName()).type(config.getTypeName()).build();

                final JestResult result = jestClient.execute(index);

                if (!result.isSucceeded()) {
                    LOG.warning("Failed to index Jenkins build on ElasticSearch: " + build + " Got error message: " + result.getErrorMessage());
                }
            } catch (Exception e) {
                LOG.log(Level.SEVERE, "Error", e);
            }
        }
    }

    private Build createBuild(Run run, TaskListener listener) {
        final Job job = run.getParent();

        EnvVars environment = null;
        try {
            environment = run.getEnvironment(listener);
        } catch (IOException e) {
            LOG.log(Level.WARNING, "Error getting environment", e);
        } catch (InterruptedException e) {
            LOG.log(Level.WARNING, "Error getting environment", e);
        }

        final Build build = new Build();
        build.setDuration(run.getDuration());
        build.setJobName(job.getFullName());
        build.setSuccessful(run.getResult().isCompleteBuild());
        build.setStartTime(run.getStartTimeInMillis());
        build.setNumber(run.getNumber());
        build.setEnvironment(new HashMap<String, String>(environment));
        build.setSystemProperties(System.getProperties());

        return build;
    }
}
@@ -1415,16 +1415,8 @@
 in(%22
-jenkins-
 elas
@@ -1424,16 +1424,24 @@
 icsearch
+-jenkins
 %22);%0A    

Fix listener does not find plugin instance