<commit_before>package communicator;

/*
*  Copyright (c) ${date}, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import jvmmonitor.io.ExtractGCData;
import jvmmonitor.model.CPULoadLog;
import jvmmonitor.model.GarbageCollectionLog;
import jvmmonitor.model.MemoryUsageLog;
import org.apache.log4j.Logger;
import org.wso2.carbon.databridge.agent.AgentHolder;
import org.wso2.carbon.databridge.agent.DataPublisher;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.databridge.commons.exception.TransportException;
import org.wso2.carbon.databridge.commons.utils.DataBridgeCommonsUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Scanner;

public class EventPublisher {

    /*
    private static String HTTPD_LOG_STREAM;
    private static String VERSION;
    private static int defaultThriftPort;
    private static int defaultBinaryPort;

    private String type;
    private String url;
    private String authURL;
    private String username;
    private String password;
    */

    final static Logger logger = Logger.getLogger(EventPublisher.class);

    /*
    public EventPublisher(String HTTPD_LOG_STREAM, String VERSION, int defaultThriftPort, int defaultBinaryPort) {

        this.HTTPD_LOG_STREAM = HTTPD_LOG_STREAM;
        this.VERSION = VERSION;
        this.defaultThriftPort = defaultThriftPort;
        this.defaultBinaryPort = defaultBinaryPort;

    }

    public EventPublisher(int defaultThriftPort, int defaultBinaryPort) {
        this.defaultThriftPort = defaultThriftPort;
        this.defaultBinaryPort = defaultBinaryPort;
    }

    public void initialize() throws SocketException, UnknownHostException {

        logger.info("Starting DAS HttpLog Agent");
        String currentDir = System.getProperty("user.dir");
        System.setProperty("javax.net.ssl.trustStore", currentDir + "/jvm-monitor-agent/src/main/resources/client-truststore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "<PASSWORD>");

        AgentHolder.setConfigPath(getDataAgentConfigPath());
        String host = getLocalAddress().getHostAddress();

        type = getProperty("type", "Thrift");
        int receiverPort = defaultThriftPort;
        if (type.equals("Binary")) {
            receiverPort = defaultBinaryPort;
        }
        int securePort = receiverPort + 100;

        url = getProperty("url", "tcp://" + host + ":" + receiverPort);
        authURL = getProperty("authURL", "ssl://" + host + ":" + securePort);
        username = getProperty("username", "admin");
        password = getProperty("password", "<PASSWORD>");

    }
    */

    public void publishLogEvents(DataPublisher dataPublisher, String streamId, long date, CPULoadLog cpuLog) throws DataEndpointException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointConfigurationException {

        //DataPublisher dataPublisher = new DataPublisher(type, url, authURL, username, password);
        //String streamId = DataBridgeCommonsUtils.generateStreamId(HTTPD_LOG_STREAM, VERSION);

        Event event = new Event(streamId, System.currentTimeMillis(), null, null,
                new Object[]{cpuLog.getProcessCPULoad(), cpuLog.getSystemCPULoad(), date});

        dataPublisher.publish(event);
        //dataPublisher.shutdown();

    }

    public void publishLogEvents(DataPublisher dataPublisher, String streamId, GarbageCollectionLog gcLog) throws DataEndpointException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointConfigurationException {

//        DataPublisher dataPublisher = new DataPublisher(type, url, authURL, username, password);
//        String streamId = DataBridgeCommonsUtils.generateStreamId(HTTPD_LOG_STREAM, VERSION);

        Event event = new Event(streamId, System.currentTimeMillis(), null, null,
                new Object[]{gcLog.getGcType(),
                        gcLog.getDuration(),
                        gcLog.getStartTime(),
                        gcLog.getGcCause(),
                        gcLog.getEdenUsedMemoryAfterGC(),
                        gcLog.getEdenUsedMemoryBeforeGC(),
                        gcLog.getSurvivorUsedMemoryAfterGC(),
                        gcLog.getSurvivorUsedMemoryBeforeGC(),
                        gcLog.getOldGenUsedMemoryAfterGC(),
                        gcLog.getOldGenUsedMemoryBeforeGC(),
                        gcLog.getEdenCommittedMemoryAfterGC(),
                        gcLog.getEdenCommittedMemoryBeforeGC(),
                        gcLog.getSurvivorCommittedMemoryAfterGC(),
                        gcLog.getSurvivorCommittedMemoryBeforeGC(),
                        gcLog.getOldGenCommittedMemoryAfterGC(),
                        gcLog.getOldGenCommittedMemoryBeforeGC(),
                        gcLog.getEdenMaxMemoryAfterGC(),
                        gcLog.getEdenMaxMemoryBeforeGC(),
                        gcLog.getSurvivorMaxMemoryAfterGC(),
                        gcLog.getSurvivorMaxMemoryBeforeGC(),
                        gcLog.getOldGenMaxMemoryAfterGC(),
                        gcLog.getOldGenMaxMemoryBeforeGC()});

        dataPublisher.publish(event);
        //dataPublisher.shutdown();

    }

    public void publishLogEvents(DataPublisher dataPublisher, String streamId, long date, MemoryUsageLog memoryLog) throws DataEndpointException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointConfigurationException {
<commit_msg>Add logger<commit_after>package communicator;

/*
*  Copyright (c) ${date}, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import jvmmonitor.io.ExtractGCData;
import jvmmonitor.model.CPULoadLog;
import jvmmonitor.model.GarbageCollectionLog;
import jvmmonitor.model.MemoryUsageLog;
import org.apache.log4j.Logger;
import org.wso2.carbon.databridge.agent.AgentHolder;
import org.wso2.carbon.databridge.agent.DataPublisher;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.databridge.commons.exception.TransportException;
import org.wso2.carbon.databridge.commons.utils.DataBridgeCommonsUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Scanner;

public class EventPublisher {

    /*
    private static String HTTPD_LOG_STREAM;
    private static String VERSION;
    private static int defaultThriftPort;
    private static int defaultBinaryPort;

    private String type;
    private String url;
    private String authURL;
    private String username;
    private String password;
    */

    final static Logger logger = Logger.getLogger(EventPublisher.class);

    /*
    public EventPublisher(String HTTPD_LOG_STREAM, String VERSION, int defaultThriftPort, int defaultBinaryPort) {

        this.HTTPD_LOG_STREAM = HTTPD_LOG_STREAM;
        this.VERSION = VERSION;
        this.defaultThriftPort = defaultThriftPort;
        this.defaultBinaryPort = defaultBinaryPort;

    }

    public EventPublisher(int defaultThriftPort, int defaultBinaryPort) {
        this.defaultThriftPort = defaultThriftPort;
        this.defaultBinaryPort = defaultBinaryPort;
    }

    public void initialize() throws SocketException, UnknownHostException {

        logger.info("Starting DAS HttpLog Agent");
        String currentDir = System.getProperty("user.dir");
        System.setProperty("javax.net.ssl.trustStore", currentDir + "/jvm-monitor-agent/src/main/resources/client-truststore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "<PASSWORD>");

        AgentHolder.setConfigPath(getDataAgentConfigPath());
        String host = getLocalAddress().getHostAddress();

        type = getProperty("type", "Thrift");
        int receiverPort = defaultThriftPort;
        if (type.equals("Binary")) {
            receiverPort = defaultBinaryPort;
        }
        int securePort = receiverPort + 100;

        url = getProperty("url", "tcp://" + host + ":" + receiverPort);
        authURL = getProperty("authURL", "ssl://" + host + ":" + securePort);
        username = getProperty("username", "admin");
        password = getProperty("password", "<PASSWORD>");

    }
    */

    public void publishLogEvents(DataPublisher dataPublisher, String streamId, long date, CPULoadLog cpuLog) throws DataEndpointException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointConfigurationException {

        //DataPublisher dataPublisher = new DataPublisher(type, url, authURL, username, password);
        //String streamId = DataBridgeCommonsUtils.generateStreamId(HTTPD_LOG_STREAM, VERSION);

        Event event = new Event(streamId, System.currentTimeMillis(), null, null,
                new Object[]{cpuLog.getProcessCPULoad(), cpuLog.getSystemCPULoad(), date});

        dataPublisher.publish(event);
        logger.info("publish CPU data : "+cpuLog.getProcessCPULoad()+"\t"+cpuLog.getSystemCPULoad()+"\t"+date);
        //dataPublisher.shutdown();

    }

    public void publishLogEvents(DataPublisher dataPublisher, String streamId, GarbageCollectionLog gcLog) throws DataEndpointException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointConfigurationException {

//        DataPublisher dataPublisher = new DataPublisher(type, url, authURL, username, password);
//        String streamId = DataBridgeCommonsUtils.generateStreamId(HTTPD_LOG_STREAM, VERSION);

        Event event = new Event(streamId, System.currentTimeMillis(), null, null,
                new Object[]{gcLog.getGcType(),
                        gcLog.getDuration(),
                        gcLog.getStartTime(),
                        gcLog.getGcCause(),
                        gcLog.getEdenUsedMemoryAfterGC(),
                        gcLog.getEdenUsedMemoryBeforeGC(),
                        gcLog.getSurvivorUsedMemoryAfterGC(),
                        gcLog.getSurvivorUsedMemoryBeforeGC(),
                        gcLog.getOldGenUsedMemoryAfterGC(),
                        gcLog.getOldGenUsedMemoryBeforeGC(),
                        gcLog.getEdenCommittedMemoryAfterGC(),
                        gcLog.getEdenCommittedMemoryBeforeGC(),
                        gcLog.getSurvivorCommittedMemoryAfterGC(),
                        gcLog.getSurvivorCommittedMemoryBeforeGC(),
                        gcLog.getOldGenCommittedMemoryAfterGC(),
                        gcLog.getOldGenCommittedMemoryBeforeGC(),
                        gcLog.getEdenMaxMemoryAfterGC(),
                        gcLog.getEdenMaxMemoryBeforeGC(),
                        gcLog.getSurvivorMaxMemoryAfterGC(),
                        gcLog.getSurvivorMaxMemoryBeforeGC(),
                        gcLog.getOldGenMaxMemoryAfterGC(),
                        gcLog.getOldGenMaxMemoryBeforeGC()});

        dataPublisher.publish(event);
        logger.info("publish GC data : "+gcLog.getGcType()+"\t"+gcLog.getDuration()+"\t"+gcLog.getStartTime());
        //dataPublisher.shutdown();

    }

    public void publishLogEvents(DataPublisher dataPublisher, String streamId, long date, MemoryUsageLog memoryLog) throws DataEndpointException,
            DataEndpointAuthenticationException,
            DataEndpointAgentConfigurationException,
            TransportException,
            DataEndpointConfigurationException {

{'fraction_non_alphanumeric': 0.08738287208542166, 'fraction_numerical': 0.0029054986562068717, 'mean_word_length': 3.0175080245112342, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2014320', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3476, 'n_words': 747}