<commit_before>/*
*  Copyright (c) 2015, 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.
*/
package org.wso2.carbon.analytics.spark.ui.client;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.analytics.spark.admin.stub.AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException;
import org.wso2.carbon.analytics.spark.admin.stub.AnalyticsProcessorAdminServiceStub;

import java.rmi.RemoteException;

public class AnalyticsExecutionClient {
    private AnalyticsProcessorAdminServiceStub stub;

    public AnalyticsExecutionClient(String cookie,
                                    String backEndServerURL,
                                    ConfigurationContext configCtx) throws AxisFault {
        String serviceURL = backEndServerURL + "AnalyticsProcessorAdminService";
        stub = new AnalyticsProcessorAdminServiceStub(configCtx, serviceURL);
        ServiceClient client = stub._getServiceClient();
        int timeout = 60 * 60 * 1000; // 1 hour
        stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(timeout);
        stub._getServiceClient().getOptions().setProperty(
                HTTPConstants.SO_TIMEOUT, timeout);
        stub._getServiceClient().getOptions().setProperty(
                HTTPConstants.CONNECTION_TIMEOUT, timeout);
        Options option = client.getOptions();
        option.setManageSession(true);
        option.setTimeOutInMilliSeconds(timeout);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
    }

    public AnalyticsExecutionClient(){

    }

    public String execute(String query) throws RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        String resultString;
        AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto queryResult = stub.executeQuery(query);
        resultString = toJsonResult(query, queryResult);
        return resultString;
    }

    public AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto[] executeScriptContent(String scriptContent) throws RemoteException,
            AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        return stub.execute(scriptContent);
    }

    public AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto[] executeScript(String scriptName)
            throws RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        return stub.executeScript(scriptName);
    }

    public void deleteScript(String scriptName) throws RemoteException,
            AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        stub.deleteScript(scriptName);
    }

    public void saveScript(String scriptName, String scriptContent, String cron) throws RemoteException,
            AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        stub.saveScript(scriptName, scriptContent, cron);
    }

    public String[] getAllScripts() throws RemoteException {
        return stub.getAllScriptNames();
    }

    public AnalyticsProcessorAdminServiceStub.AnalyticsScriptDto getScriptContent(String scriptName)
            throws RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        return stub.getScript(scriptName);
    }

    public void updateScript(String scriptName, String scriptContent, String cron) throws
            RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        if (scriptContent != null) {
            stub.updateScriptContent(scriptName, scriptContent);
        }
        if (cron != null) {
            stub.updateScriptTask(scriptName, cron);
        }
    }

    public String toJsonResult(String query, AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto res) {
        JsonObject resObj = new JsonObject();

        JsonObject meta = new JsonObject();
        meta.addProperty("code", 200);
        meta.addProperty("responseMessage", "EXECUTED QUERY : " + query);
        JsonArray colArray = new JsonArray();

        if (res != null) {
            for (String col : res.getColumnNames()) {
                if (col != null) {
                    colArray.add(new JsonPrimitive(col));
                }
            }
        }
        meta.add("columns", colArray);
        resObj.add("meta", meta);

        JsonObject response = new JsonObject();
        JsonArray rows = new JsonArray();

        if (res != null) {
            for (AnalyticsProcessorAdminServiceStub.AnalyticsRowResultDto row : res.getRowsResults()) {
                if (row != null) {
                    JsonArray singleRow = new JsonArray();
                    int i = 0;
                    for (String elm : row.getColumnValues()) {
                        if (elm != null) {
                            singleRow.add(new JsonPrimitive(elm.toString()));
                        } else {
                            singleRow.add(new JsonPrimitive("NULL"));
                        }<commit_msg>Fixing spark console issue after registry based analytics storage.<commit_after>/*
*  Copyright (c) 2015, 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.
*/
package org.wso2.carbon.analytics.spark.ui.client;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.analytics.spark.admin.stub.AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException;
import org.wso2.carbon.analytics.spark.admin.stub.AnalyticsProcessorAdminServiceStub;

import java.rmi.RemoteException;

public class AnalyticsExecutionClient {
    private AnalyticsProcessorAdminServiceStub stub;

    public AnalyticsExecutionClient(String cookie,
                                    String backEndServerURL,
                                    ConfigurationContext configCtx) throws AxisFault {
        String serviceURL = backEndServerURL + "AnalyticsProcessorAdminService";
        stub = new AnalyticsProcessorAdminServiceStub(configCtx, serviceURL);
        ServiceClient client = stub._getServiceClient();
        int timeout = 60 * 60 * 1000; // 1 hour
        stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(timeout);
        stub._getServiceClient().getOptions().setProperty(
                HTTPConstants.SO_TIMEOUT, timeout);
        stub._getServiceClient().getOptions().setProperty(
                HTTPConstants.CONNECTION_TIMEOUT, timeout);
        Options option = client.getOptions();
        option.setManageSession(true);
        option.setTimeOutInMilliSeconds(timeout);
        option.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, cookie);
    }

    public AnalyticsExecutionClient(){

    }

    public String execute(String query) throws RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto queryResult = stub.executeQuery(query);
        return toJsonResult(query, queryResult);
    }

    public AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto[] executeScriptContent(String scriptContent) throws RemoteException,
            AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        return stub.execute(scriptContent);
    }

    public AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto[] executeScript(String scriptName)
            throws RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        return stub.executeScript(scriptName);
    }

    public void deleteScript(String scriptName) throws RemoteException,
            AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        stub.deleteScript(scriptName);
    }

    public void saveScript(String scriptName, String scriptContent, String cron) throws RemoteException,
            AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        stub.saveScript(scriptName, scriptContent, cron);
    }

    public String[] getAllScripts() throws RemoteException {
        return stub.getAllScriptNames();
    }

    public AnalyticsProcessorAdminServiceStub.AnalyticsScriptDto getScriptContent(String scriptName)
            throws RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        return stub.getScript(scriptName);
    }

    public void updateScript(String scriptName, String scriptContent, String cron) throws
            RemoteException, AnalyticsProcessorAdminServiceAnalyticsProcessorAdminExceptionException {
        if (scriptContent != null) {
            stub.updateScriptContent(scriptName, scriptContent);
        }
        if (cron != null) {
            stub.updateScriptTask(scriptName, cron);
        }
    }

    public String toJsonResult(String query, AnalyticsProcessorAdminServiceStub.AnalyticsQueryResultDto res) {
        JsonObject resObj = new JsonObject();

        JsonObject meta = new JsonObject();
        meta.addProperty("code", 200);
        meta.addProperty("responseMessage", "EXECUTED QUERY : " + query);
        JsonArray colArray = new JsonArray();

        if (res != null) {
            for (String col : res.getColumnNames()) {
                if (col != null) {
                    colArray.add(new JsonPrimitive(col));
                }
            }
        }
        meta.add("columns", colArray);
        resObj.add("meta", meta);

        JsonObject response = new JsonObject();
        JsonArray rows = new JsonArray();

        if (res != null && res.getRowsResults() != null) {
            for (AnalyticsProcessorAdminServiceStub.AnalyticsRowResultDto row : res.getRowsResults()) {
                if (row != null) {
                    JsonArray singleRow = new JsonArray();
                    int i = 0;
                    for (String elm : row.getColumnValues()) {
                        if (elm != null) {
                            singleRow.add(new JsonPrimitive(elm.toString()));
                        } else {
                            singleRow.add(new JsonPrimitive("NULL"));
                        }
{'fraction_non_alphanumeric': 0.07868383404864092, 'fraction_numerical': 0.00555415299166877, 'mean_word_length': 3.1092669432918396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1123142', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2873, 'n_words': 682}