<commit_before>'use strict';

/* Services */
var serviceModule = angular.module('myApp.services', []);
serviceModule.value('version', '0.9');

serviceModule.factory('elastic', ['serverConfig','esFactory', function (serverConfig, esFactory) {
    function ElasticService(serverConfig, esFactory) {
        var serverUrl = serverConfig.host;
        var statussus = {"green": "success", "yellow": "warning", "red": "error"};
        var es = esFactory({"host": serverUrl, "apiVersion":"1.0", "log":"trace"});

        this.changeServerAddress = function (serverAddress) {
            serverUrl = serverAddress;
            es = esFactory({"host": serverUrl})
        };

        this.obtainServerAddress = function () {
            return serverUrl;
        };

        this.clusterStatus = function (callback) {
            es.cluster.health({}).then(function(data) {
                var numClients = data.number_of_nodes - data.number_of_data_nodes;
                var msg = data.cluster_name + " [nodes: " + data.number_of_nodes + ", clients: " + numClients + "]";
                callback(msg, statussus[data.status]);                    
            }, function(reason) {
                console.log(reason);
                callback("No connection", "error");
            });
        };

        this.clusterName = function (callback) {
            es.cluster.health().then(function(data) {
                callback(data.cluster_name);
            });
        };

        this.clusterHealth = function (callback) {
            es.cluster.health().then(function(data) {
                callback(data);
            });
        };

        this.clusterNodes = function (callback) {
            es.cluster.nodeInfo().then(function (data) {
                callback(data.nodes);
            });
        };

        this.plugins = function (callback) {
            es.cluster.nodeInfo({"plugin":true}).then(function(data) {
                var nodes = [];
                angular.forEach(data.nodes, function (node,node_id) {
                    var siteNode = {};
                    siteNode.name = node.name;
                    siteNode.plugins = [];
                    var httpAddress = node.http_address.substring(6,node.http_address.length-1);
                    angular.forEach(node.plugins, function(plugin) {
                        if (plugin.site) {
                            var sitePlugin = {};
                            sitePlugin.url = 'http://'+ httpAddress + plugin.url;
                            sitePlugin.name = plugin.name;
                            siteNode.plugins.push(sitePlugin);
                        }
                    })
                    nodes.push(siteNode);
                });
                callback(nodes);
            });
        };

        this.nodeInfo = function (nodeId, callback) {
            es.cluster.nodeInfo({"all":true,"nodeId":nodeId}).then(function (data) {
                callback(data.nodes[nodeId]);
            });
        };

        this.indexes = function (callback) {
            es.indices.status({}).then(function (data) {
                var indices = [];
                for (var index in data.indices) {
                    indices.push(index);
                }
                callback(indices);
            });
        };

        this.removeIndex = function (index, callback) {
            es.indices.delete({"index":index}).then(function(data) {
                callback();
            });
        };

        this.openIndex = function (index, callback) {
            es.indices.open({"index":index}).then(function(data) {
                callback();
            });
        };

        this.closeIndex = function (index, callback) {
            es.indices.close({"index":index}).then(function(data) {
                callback();
            });
        };

        this.indexesDetails = function (callback) {
            es.indices.status().then(function (statusData) {
                var indexesStatus = statusData.indices;
                var stateFilter = {};
                stateFilter.filterRoutingTable = true;
                stateFilter.filterNodes = true;
                stateFilter.filterBlocks = true;

                es.cluster.state(stateFilter).then(function(stateData) {
                    var indexesState = stateData.metadata.indices;
                    var indices = [];
                    angular.forEach(indexesState, function(value,key) {
                        var newIndex = {};
                        newIndex.name = key;
                        newIndex.numShards = value.settings['index.number_of_shards'];
                        if (value.state === 'open') {
                            newIndex.size = indexesStatus[key].index.size;
                            newIndex.numDocs = indexesStatus[key].docs.num_docs;
                            newIndex.state = true;
                        } else {
                            newIndex.state = false;
                        }
                        indices.push(newIndex);
                    });
                    callback(indices);
                });
            });
        };

        this.types = function (selectedIndex, callback) {
            var mappingFilter = {};
            if (selectedIndex.length > 0) {
                mappingFilter.index = selectedIndex.toString();
            }
            es.indices.getMapping(mappingFilter).then(function (data) {
                var myTypes = [];
                for (var index in data) {
                    for (var type in data[index].mappings) {
                        if (myTypes.indexOf(type) == -1 && type != "_default_") {
                            myTypes.push(type);
                        }
                    }
                }
                callback(myTypes);
            });
        };

        this.fields = function (selectedIndex, selectedType, callback) {
            var mappingFilter = {};
            if (selectedIndex.length > 0) {
                mappingFilter.index = selectedIndex.toString();
            }
            if (selectedType.length > 0) {
                mappingFilter.type = selectedType.toString();
            }
            es.indices.getMapping(mappingFilter).then(function (data) {
                var myTypes = [];
                var myFields = {};
                for (var index in data) {
                    for (var type in data[index].mappings) {
                        if (myTypes.indexOf(type) == -1 && type != "_default_") {
                            myTypes.push(type);
                            var properties = data[index].mappings[type].properties;
                            for (var field in properties) {
                                handleSubfields(properties[field], field, myFields, undefined);
                            }
                        }
                    }
                }
                callback(myFields);
            });
        };

        function handleSubfields(field, fieldName, myFields, nestedPath) {
            if (field.hasOwnProperty("properties")) {
                var nested = (field.type == "nested" | field.type == "object");
                if (nested) {
                    nestedPath = fieldName;
                }
                for (var subField in field.properties) {
                    var newField = fieldName + "." + subField;
                    handleSubfields(field.properties[subField], newField, myFields, nestedPath);
                }
            } else {
                if (field.type === "multi_field") {
                    for (var multiField in field.fields) {
                        var multiFieldName = fieldName + "." + multiField;
                        // TODO jettro : fix the nested documents with multi_fields
                        if (!myFields[multiFieldName] && fieldName !== multiField) {
                            myFields[multiFieldName] = field.fields[multiField];
                            myFields[multiFieldName].nestedPath = nestedPath;
                            myFields[multiFieldName].forPrint = multiFieldName + " (" + field.type + ")";
                        }
                    }
                }
                if (!myFields[fieldName]) {
                    myFields[fieldName] = field;
                    myFields[fieldName].nestedPath = nestedPath;
                    myFields[fieldName].forPrint = fieldName + " (" + field.type + ")";
                }
            }
        }

        this.doSearch = function(query, resultCallback, errorCallback) {
            es.search(query).then(function(results) {
                resultCallback(results)
            }, function(errors) {
                errorCallback(errors)
            });
        };
    }

    return new ElasticService(serverConfig, esFactory);
}]);

serviceModule.factory('configuration', ['$rootScope', 'localStorage', function ($rootScope, localStorage) {<commit_msg>Fixed problem with multi_field changes<commit_after>'use strict';

/* Services */
var serviceModule = angular.module('myApp.services', []);
serviceModule.value('version', '0.9');

serviceModule.factory('elastic', ['serverConfig','esFactory', function (serverConfig, esFactory) {
    function ElasticService(serverConfig, esFactory) {
        var serverUrl = serverConfig.host;
        var statussus = {"green": "success", "yellow": "warning", "red": "error"};
        var es = esFactory({"host": serverUrl, "apiVersion":"1.0", "log":"trace"});

        this.changeServerAddress = function (serverAddress) {
            serverUrl = serverAddress;
            es = esFactory({"host": serverUrl})
        };

        this.obtainServerAddress = function () {
            return serverUrl;
        };

        this.clusterStatus = function (callback) {
            es.cluster.health({}).then(function(data) {
                var numClients = data.number_of_nodes - data.number_of_data_nodes;
                var msg = data.cluster_name + " [nodes: " + data.number_of_nodes + ", clients: " + numClients + "]";
                callback(msg, statussus[data.status]);                    
            }, function(reason) {
                console.log(reason);
                callback("No connection", "error");
            });
        };

        this.clusterName = function (callback) {
            es.cluster.health().then(function(data) {
                callback(data.cluster_name);
            });
        };

        this.clusterHealth = function (callback) {
            es.cluster.health().then(function(data) {
                callback(data);
            });
        };

        this.clusterNodes = function (callback) {
            es.cluster.nodeInfo().then(function (data) {
                callback(data.nodes);
            });
        };

        this.plugins = function (callback) {
            es.cluster.nodeInfo({"plugin":true}).then(function(data) {
                var nodes = [];
                angular.forEach(data.nodes, function (node,node_id) {
                    var siteNode = {};
                    siteNode.name = node.name;
                    siteNode.plugins = [];
                    var httpAddress = node.http_address.substring(6,node.http_address.length-1);
                    angular.forEach(node.plugins, function(plugin) {
                        if (plugin.site) {
                            var sitePlugin = {};
                            sitePlugin.url = 'http://'+ httpAddress + plugin.url;
                            sitePlugin.name = plugin.name;
                            siteNode.plugins.push(sitePlugin);
                        }
                    })
                    nodes.push(siteNode);
                });
                callback(nodes);
            });
        };

        this.nodeInfo = function (nodeId, callback) {
            es.cluster.nodeInfo({"all":true,"nodeId":nodeId}).then(function (data) {
                callback(data.nodes[nodeId]);
            });
        };

        this.indexes = function (callback) {
            es.indices.status({}).then(function (data) {
                var indices = [];
                for (var index in data.indices) {
                    indices.push(index);
                }
                callback(indices);
            });
        };

        this.removeIndex = function (index, callback) {
            es.indices.delete({"index":index}).then(function(data) {
                callback();
            });
        };

        this.openIndex = function (index, callback) {
            es.indices.open({"index":index}).then(function(data) {
                callback();
            });
        };

        this.closeIndex = function (index, callback) {
            es.indices.close({"index":index}).then(function(data) {
                callback();
            });
        };

        this.indexesDetails = function (callback) {
            es.indices.status().then(function (statusData) {
                var indexesStatus = statusData.indices;
                var stateFilter = {};
                stateFilter.filterRoutingTable = true;
                stateFilter.filterNodes = true;
                stateFilter.filterBlocks = true;

                es.cluster.state(stateFilter).then(function(stateData) {
                    var indexesState = stateData.metadata.indices;
                    var indices = [];
                    angular.forEach(indexesState, function(value,key) {
                        var newIndex = {};
                        newIndex.name = key;
                        newIndex.numShards = value.settings['index.number_of_shards'];
                        if (value.state === 'open') {
                            newIndex.size = indexesStatus[key].index.size;
                            newIndex.numDocs = indexesStatus[key].docs.num_docs;
                            newIndex.state = true;
                        } else {
                            newIndex.state = false;
                        }
                        indices.push(newIndex);
                    });
                    callback(indices);
                });
            });
        };

        this.types = function (selectedIndex, callback) {
            var mappingFilter = {};
            if (selectedIndex.length > 0) {
                mappingFilter.index = selectedIndex.toString();
            }
            es.indices.getMapping(mappingFilter).then(function (data) {
                var myTypes = [];
                for (var index in data) {
                    for (var type in data[index].mappings) {
                        if (myTypes.indexOf(type) == -1 && type != "_default_") {
                            myTypes.push(type);
                        }
                    }
                }
                callback(myTypes);
            });
        };

        this.fields = function (selectedIndex, selectedType, callback) {
            var mappingFilter = {};
            if (selectedIndex.length > 0) {
                mappingFilter.index = selectedIndex.toString();
            }
            if (selectedType.length > 0) {
                mappingFilter.type = selectedType.toString();
            }
            es.indices.getMapping(mappingFilter).then(function (data) {
                var myTypes = [];
                var myFields = {};
                for (var index in data) {
                    for (var type in data[index].mappings) {
                        if (myTypes.indexOf(type) == -1 && type != "_default_") {
                            myTypes.push(type);
                            var properties = data[index].mappings[type].properties;
                            for (var field in properties) {
                                handleSubfields(properties[field], field, myFields, undefined);
                            }
                        }
                    }
                }
                callback(myFields);
            });
        };

        function handleSubfields(field, fieldName, myFields, nestedPath) {
            if (field.hasOwnProperty("properties")) {
                var nested = (field.type == "nested" | field.type == "object");
                if (nested) {
                    nestedPath = fieldName;
                }
                for (var subField in field.properties) {
                    var newField = fieldName + "." + subField;
                    handleSubfields(field.properties[subField], newField, myFields, nestedPath);
                }
            } else {
                if (field.hasOwnProperty("fields")) {
                    for (var multiField in field.fields) {
                        var multiFieldName = fieldName + "." + multiField;
                        // TODO jettro : fix the nested documents with multi_fields
                        if (!myFields[multiFieldName] && fieldName !== multiField) {
                            myFields[multiFieldName] = field.fields[multiField];
                            myFields[multiFieldName].nestedPath = nestedPath;
                            myFields[multiFieldName].forPrint = multiFieldName + " (" + field.type + ")";
                        }
                    }
                }
                if (!myFields[fieldName]) {
                    myFields[fieldName] = field;
                    myFields[fieldName].nestedPath = nestedPath;
                    myFields[fieldName].forPrint = fieldName + " (" + field.type + ")";
                }
            }
        }

        this.doSearch = function(query, resultCallback, errorCallback) {
            es.search(query).then(function(results) {
                resultCallback(results)
            }, function(errors) {
                errorCallback(errors)
            });
        };
    }

    return new ElasticService(serverConfig, esFactory);
}]);

serviceModule.factory('configuration', ['$rootScope', 'localStorage', function ($rootScope, localStorage) {
{'fraction_non_alphanumeric': 0.11578125871007303, 'fraction_numerical': 0.001226378281955516, 'mean_word_length': 1.3277539898793305, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 9, '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': '6836144', 'n_tokens_mistral': 4377, 'n_tokens_neox': 4129, 'n_words': 745}