<commit_before>package com.splunk.sharedmc;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONObject;

/**
 * Knows a single Splunk instance by its host:port and forwards data to it.
 */
public class SingleSplunkConnection implements SplunkConnection, Runnable {
    private static final String LOGGER_PREFIX = "SplunkConnection - ";
    private static final String DEFAULT_RECONNECT_TIME = "10";
    private String BASE_URL = "http://%s:%s/services/collector/event/1.0";

    /**
     * Interval in seconds between attempts to connect to Splunk.
     */
    private static final int RECONNECT_TIME =
            Integer.valueOf(System.getProperty("splunk_mc.reconnect_time", DEFAULT_RECONNECT_TIME));

    private final Logger logger;
    private final String url;

    private CloseableHttpClient httpClient;

    private String server;
    private String token;

    // lazy
    private StringBuilder messagesToSend = new StringBuilder();
    private StringBuilder messagesOnRunway;

    /**
     * Constructor. Determines which Splunk instance this will connect to based on the host:port passed in. Set up a
     * shutdown hook to send any remaining messages to Splunk on close.
     *
     * @param host Host of Splunk to connect to.
     * @param port Port of Splunk to connect to.
     * @param server Name of Minecraft server.
     * @param startImmediately If true, creates a thread and starts this Splunk on construction.
     */
    public SingleSplunkConnection(String host, int port, String server, String token, boolean startImmediately) {
        logger = LogManager.getLogger(LOGGER_PREFIX + host + ':' + port);
        this.token = token;
        url = String.format(BASE_URL, host, port);
        httpClient = HttpClients.createDefault();

        this.server = server;

        addFlushShutdownHook();

        if (startImmediately) {
            new Thread(this).start();
        }
    }

    @Override
    public void run() {
        while (true) {
            sendData();
            try {
                Thread.sleep(1000 * RECONNECT_TIME);
            } catch (final InterruptedException e) {
                //eat exception.
            }
        }
    }

    /**
     * Queues up a message to send to this Spunk connections' Splunk instance.
     *
     * @param message The message to send.
     */
    @Override
    public void sendToSplunk(String message) {
        JSONObject event = new JSONObject();
        message = Calendar.getInstance().getTime().toString() + ' ' + message + " server=" + (this.server).trim();
        event.put("event", message);

        messagesToSend.append(event.toString());
    }

    private boolean sendData() {
        boolean success = false;
        // probably a better way to do this.
        if (messagesOnRunway == null && messagesToSend.length() > 0) {
            messagesOnRunway = messagesToSend;
            messagesToSend = new StringBuilder();
        }else{
            // no messages to send, so safe to say messages have been sent.
            return messagesOnRunway == null;
        }
        try {
            logger.info("Sending data to splunk...");
            HttpPost post = new HttpPost(url);
            post.setHeader("Authorization", "Splunk " + token);<commit_msg>Changed the timestamp in the events to the ISO 8601 format.<commit_after>package com.splunk.sharedmc;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONObject;

/**
 * Knows a single Splunk instance by its host:port and forwards data to it.
 */
public class SingleSplunkConnection implements SplunkConnection, Runnable {
    private static final String LOGGER_PREFIX = "SplunkConnection - ";
    private static final String DEFAULT_RECONNECT_TIME = "10";
    private String BASE_URL = "http://%s:%s/services/collector/event/1.0";

    /**
     * Interval in seconds between attempts to connect to Splunk.
     */
    private static final int RECONNECT_TIME =
            Integer.valueOf(System.getProperty("splunk_mc.reconnect_time", DEFAULT_RECONNECT_TIME));

    private final Logger logger;
    private final String url;

    private CloseableHttpClient httpClient;

    private String server;
    private String token;

    // lazy
    private StringBuilder messagesToSend = new StringBuilder();
    private StringBuilder messagesOnRunway;

    /**
     * Constructor. Determines which Splunk instance this will connect to based on the host:port passed in. Set up a
     * shutdown hook to send any remaining messages to Splunk on close.
     *
     * @param host Host of Splunk to connect to.
     * @param port Port of Splunk to connect to.
     * @param server Name of Minecraft server.
     * @param startImmediately If true, creates a thread and starts this Splunk on construction.
     */
    public SingleSplunkConnection(String host, int port, String server, String token, boolean startImmediately) {
        logger = LogManager.getLogger(LOGGER_PREFIX + host + ':' + port);
        this.token = <PASSWORD>;
        url = String.format(BASE_URL, host, port);
        httpClient = HttpClients.createDefault();

        this.server = server;

        addFlushShutdownHook();

        if (startImmediately) {
            new Thread(this).start();
        }
    }

    @Override
    public void run() {
        while (true) {
            sendData();
            try {
                Thread.sleep(1000 * RECONNECT_TIME);
            } catch (final InterruptedException e) {
                //eat exception.
            }
        }
    }

    /**
     * Queues up a message to send to this Spunk connections' Splunk instance.
     *
     * @param message The message to send.
     */
    @Override
    public void sendToSplunk(String message) {
        JSONObject event = new JSONObject();

        DateFormat df_8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");

        message = df_8601.format(new Date()) + ' ' + message + " server=" + (this.server).trim();
        event.put("event", message);

        messagesToSend.append(event.toString());
    }

    private boolean sendData() {
        boolean success = false;
        // probably a better way to do this.
        if (messagesOnRunway == null && messagesToSend.length() > 0) {
            messagesOnRunway = messagesToSend;
            messagesToSend = new StringBuilder();
        }else{
            // no messages to send, so safe to say messages have been sent.
            return messagesOnRunway == null;
        }
        try {
            logger.info("Sending data to splunk...");
            HttpPost post = new HttpPost(url);
            post.setHeader("Authorization", "Splunk " + token);
{'fraction_non_alphanumeric': 0.09316770186335403, 'fraction_numerical': 0.004309798453542908, 'mean_word_length': 2.7607244995233557, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7153587', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2096, 'n_words': 630}