<commit_before>var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http');
var bodyParser = require("body-parser");
var Curl = require( 'node-libcurl' ).Curl;
var Promise = require('promise');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

var HOST = process.argv[2] || '127.0.0.1';
console.log('Will start on host: %s', HOST);

var PORT = process.argv[3] || 8080;
console.log('Will start on port: %s', PORT);

app.get('/haproxy', function(req, res){
    
    function IPs(filePath){
        
        var curlLog = '';
        
        var regIP = /^(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])){3}\:[0-9]{1,4}\n?$/;

        var haproxy_origin = fs.readFileSync(filePath, 'utf8');
        var haproxy_splited_rows = haproxy_origin.split('\n');

        //cut and leave only configuration part of haproxy.cfg file
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            if(haproxy_splited_rows[i] != ''){
                 var temp = haproxy_splited_rows[i].split(' ');
                 if(temp[0] == 'frontend'){
                     haproxy_splited_rows = haproxy_splited_rows.slice(i);
                     break;
                 }
            }
        }

        //delete odd spaces
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            haproxy_splited_rows[i] = haproxy_splited_rows[i].replace(/^\s*/,'').replace(/\s*$/,'');
        }

        //search for {frontend name : frontend address}
        var frontend_names = {};

        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            var temp = haproxy_splited_rows[i].split(' ');
            for(var j = 0; j < temp.length; ++j){
                if(temp[j] == 'acl'){

                    frontend_names[temp[j+1]] = temp[temp.length - 1];


                }
            }
        }

        //search for {backend name : backend address}
        var backend_part_rows = [];
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            var temp = haproxy_splited_rows[i].split(' ');
            if(temp[0] == 'backend'){
                backend_part_rows = haproxy_splited_rows.slice(i);
                break;
            }
        }

        var backends_string = '';
        for(var i = 0; i < backend_part_rows.length; ++i){
            backends_string += ' ' + backend_part_rows[i];
        }

        var backend_names = {}; // {backend name : addresses}
        var backends_array_spaces = backends_string.split(' ');
        for(var i = 0; i < backends_array_spaces.length; ++i){

            if(backends_array_spaces[i] == 'backend'){

                backend_names[backends_array_spaces[i + 1]] = '';

                var temp_adr = '';

                for(var j = i; j < backends_array_spaces.length; ++j){

                    if(regIP.test(backends_array_spaces[j])){
                        temp_adr += backends_array_spaces[j] + ' ';
                        backend_names[backends_array_spaces[i + 1]] = temp_adr;
                    }
                    if(backends_array_spaces[j] == 'backend' && j != i){
                        i = j - 1;
                        break;
                    }
                }

            }

        }

        // define pairs front-backend
        var pairs = [];
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            var temp = haproxy_splited_rows[i].split(' ');
            for(var j = 0; j < temp.length; ++j) {
                if (temp[j] == 'use_backend'){
                    var obj = {};
                    obj.frontend = temp[temp.length - 1];
                    obj.backend = temp[j + 1];
                    pairs.push(obj);
                }
            }
        }

        // build config array [{frontend: 'IP', backend: 'IP', status: ''}, {...}]

        var IPs = [];

        for(var i = 0; i < pairs.length; ++i){
            var obj = {};
            obj.frontend = frontend_names[pairs[i].frontend];
            obj.backend = backend_names[pairs[i].backend];
            obj.status = "Not available";
            IPs.push(obj);
        }

        // preparing for status check
        var tempBackends = [];
        
        for(var i = 0; i < IPs.length; ++i){
            IPs[i].backend = IPs[i].backend.replace(/^\s*/,'').replace(/\s*$/,'');
            tempBackends[i] = "http://" + IPs[i].backend;
        }
        
        

        

        function getPromise(backend) {
            return new Promise(function(resolve) {
                
                var curl = new Curl();

                curl.setOpt( 'URL', backend );
                curl.setOpt( 'CONNECTTIMEOUT', 5 );
                curl.setOpt( 'FOLLOWLOCATION', true );
                

                curl.on( 'end', function(statusCode, body, headers) {
                    console.info(backend + ' is available');
                    
                    curlLog += backend + ' status code ' + statusCode + '\n';
                    curlLog += backend + ' headers ' + headers + '\n';
                    curlLog += backend + ' body ' + body + '\n';
                    
                    
                    resolve("Available");
                    this.close();
                });
                
                curl.on( 'error', function(statusCode, body, headers){
                    console.info(backend + ' is not available');
                    
                    curlLog += backend + ' status code ' + statusCode + '\n';
                    curlLog += backend + ' headers ' + headers + '\n';
                    curlLog += backend + ' body ' + body + '\n';
              
                    resolve("Not available");
                    this.close(); 
                });
                
                curl.perform();
                
         });
        }

        Promise.all(tempBackends . map(getPromise)) .
        then(function(stats) {

            for(var i = 0; i < stats.length; ++i)
            {
                IPs[i].status = stats[i];
            }

            var data = { IPs : IPs,
                         log: curlLog};
            res.json(data);<commit_msg>log clean<commit_after>var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http');
var bodyParser = require("body-parser");
var Curl = require( 'node-libcurl' ).Curl;
var Promise = require('promise');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

var HOST = process.argv[2] || '127.0.0.1';
console.log('Will start on host: %s', HOST);

var PORT = process.argv[3] || 8080;
console.log('Will start on port: %s', PORT);

app.get('/haproxy', function(req, res){
    
    function IPs(filePath){
        
        var curlLog = '';
        
        var regIP = /^(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])){3}\:[0-9]{1,4}\n?$/;

        var haproxy_origin = fs.readFileSync(filePath, 'utf8');
        var haproxy_splited_rows = haproxy_origin.split('\n');

        //cut and leave only configuration part of haproxy.cfg file
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            if(haproxy_splited_rows[i] != ''){
                 var temp = haproxy_splited_rows[i].split(' ');
                 if(temp[0] == 'frontend'){
                     haproxy_splited_rows = haproxy_splited_rows.slice(i);
                     break;
                 }
            }
        }

        //delete odd spaces
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            haproxy_splited_rows[i] = haproxy_splited_rows[i].replace(/^\s*/,'').replace(/\s*$/,'');
        }

        //search for {frontend name : frontend address}
        var frontend_names = {};

        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            var temp = haproxy_splited_rows[i].split(' ');
            for(var j = 0; j < temp.length; ++j){
                if(temp[j] == 'acl'){

                    frontend_names[temp[j+1]] = temp[temp.length - 1];


                }
            }
        }

        //search for {backend name : backend address}
        var backend_part_rows = [];
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            var temp = haproxy_splited_rows[i].split(' ');
            if(temp[0] == 'backend'){
                backend_part_rows = haproxy_splited_rows.slice(i);
                break;
            }
        }

        var backends_string = '';
        for(var i = 0; i < backend_part_rows.length; ++i){
            backends_string += ' ' + backend_part_rows[i];
        }

        var backend_names = {}; // {backend name : addresses}
        var backends_array_spaces = backends_string.split(' ');
        for(var i = 0; i < backends_array_spaces.length; ++i){

            if(backends_array_spaces[i] == 'backend'){

                backend_names[backends_array_spaces[i + 1]] = '';

                var temp_adr = '';

                for(var j = i; j < backends_array_spaces.length; ++j){

                    if(regIP.test(backends_array_spaces[j])){
                        temp_adr += backends_array_spaces[j] + ' ';
                        backend_names[backends_array_spaces[i + 1]] = temp_adr;
                    }
                    if(backends_array_spaces[j] == 'backend' && j != i){
                        i = j - 1;
                        break;
                    }
                }

            }

        }

        // define pairs front-backend
        var pairs = [];
        for(var i = 0; i < haproxy_splited_rows.length; ++i){
            var temp = haproxy_splited_rows[i].split(' ');
            for(var j = 0; j < temp.length; ++j) {
                if (temp[j] == 'use_backend'){
                    var obj = {};
                    obj.frontend = temp[temp.length - 1];
                    obj.backend = temp[j + 1];
                    pairs.push(obj);
                }
            }
        }

        // build config array [{frontend: 'IP', backend: 'IP', status: ''}, {...}]

        var IPs = [];

        for(var i = 0; i < pairs.length; ++i){
            var obj = {};
            obj.frontend = frontend_names[pairs[i].frontend];
            obj.backend = backend_names[pairs[i].backend];
            obj.status = "Not available";
            IPs.push(obj);
        }

        // preparing for status check
        var tempBackends = [];
        
        for(var i = 0; i < IPs.length; ++i){
            IPs[i].backend = IPs[i].backend.replace(/^\s*/,'').replace(/\s*$/,'');
            tempBackends[i] = "http://" + IPs[i].backend;
        }
        
        var tempFrontends = [];
        
        for(var i = 0; i < IPs.length; ++i){
            IPs[i].frontend = IPs[i].frontend.replace(/^\s*/,'').replace(/\s*$/,'');
            tempFrontends[i] = "http://" + IPs[i].frontend;
        }
        
        var tempIP = [];
        for(var i = 0; i < IPs.length; ++i){
            tempIP[i] = tempFrontends[i] + '|' + tempBackends[i];
           
        }
         
        function getPromise(IP) {
            return new Promise(function(resolve) {
                
                var curl = new Curl();
                
                var backend = IP.split('|')[1];
                console.log('Backend ' + backend);
                var frontend = IP.split('|')[0]
                console.log('Frontend ' + frontend);

                curl.setOpt( 'URL', backend );
                //curl.setOpt( 'CURLOPT_HTTPHEADER', ['Host:' + frontend]);
                curl.setOpt( 'CONNECTTIMEOUT', 5 );
                curl.setOpt( 'FOLLOWLOCATION', true );
                curl.setOpt( 'NOBODY', true );
                

                curl.on( 'end', function(statusCode) {
                    console.info(backend + ' is available');
                    
                    curlLog += 'Requesting ' + backend + '\n';
                    curlLog += 'Received status code: ' + statusCode + '\n'; 

                    resolve("Available");
                    this.close();
                });
                
                curl.on( 'error', function(statusCode){
                    console.info(backend + ' is not available');
                  
                    curlLog += backend + ' ' + statusCode + '\n';
                   
                    resolve("Not available");
                    this.close(); 
                });
                
                curl.perform();
                
         });
        }

        Promise.all(tempIP . map(getPromise)) .
        then(function(stats) {

            for(var i = 0; i < stats.length; ++i)
            {
                IPs[i].status = stats[i];
            }

            var data = { IPs : IPs,
                         log: curlLog};
            res.json(data);
{'fraction_non_alphanumeric': 0.15350673478866697, 'fraction_numerical': 0.012385818238117356, 'mean_word_length': 1.3959569732937684, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7398260', 'n_tokens_mistral': 4122, 'n_tokens_neox': 3809, 'n_words': 747}