<commit_before>//Lets require/import the HTTP module
var http = require('http');
var dispatcher = require('httpdispatcher');
var pg = require('pg');

//Create the connection to the db
var conString = "postgres://blacksburg_read:nrv@postgres1.ceipocejvkue.us-west-2.rds.amazonaws.com/blacksburg";

//Lets define a port we want to listen to
const PORT=(process.env.PORT || 5000);

//We need a function which handles requests and send response
function handleRequest(request, response){
    try {
        //log the request on console
        //console.log(request.url);
        //Disptach
        dispatcher.dispatch(request, response);
    } catch(err) {
        console.log(err);
    }
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening");
});


//For all your static (js/css/images/etc.) set the directory name (relative path).
dispatcher.setStatic('resources');

//A GET request
dispatcher.onGet("/inBlacksburg", function(req, res) {
    var lat = req.params.lat;
    var lon = req.params.lon;
    // get a pg client from the connection pool
    pg.connect(conString, function(err, client, done) {
        var handleError = function(err) {
            // no error occurred, continue with the request
            if(!err) return false;
            // An error occurred, remove the client from the connection pool.
            // A truthy value passed to done will remove the connection from the pool
            // instead of simply returning it to be reused.
            // In this case, if we have successfully received a client (truthy)
            // then it will be removed from the pool.
            if(client){
                done(client);
            }
            console.log(err);
            res.writeHead(500, {'content-type': 'application/json','Access-Control-Allow-Origin' : '*'});
            res.end(JSON.stringify({"error":"There was an error querying the database"}));
            return true;
        };
        // handle an error from the connection
        if(handleError(err)) return;
        var sql = "SELECT ST_WITHIN(ST_GeometryFromText('POINT(" + lon + " " + lat + ")', 4326),ST_TRANSFORM(ST_SetSRID(geom, 2284), 4326)) AS in_blacksburg FROM corporate_boundary;";
        client.query(sql, function(err, result) {
            // handle an error from the query
            if(handleError(err)) return;
            // return the client to the connection pool for other requests to reuse
            done();
            res.writeHead(200, {'Content-Type': 'application/json','Access-Control-Allow-Origin' : '*'});
            res.end(JSON.stringify(result.rows[0]));
        });
    });
});

//A POST request
dispatcher.onPost("/score", function(req, res) {
    var coordinates = req.body;
    // get a pg client from the connection pool
    pg.connect(conString, function(err, client, done) {
        var handleError = function(err) {
            // no error occurred, continue with the request
            if(!err) return false;
            if(client){
                done(client);
            }
            res.writeHead(500, {'content-type': 'application/json','Access-Control-Allow-Origin' : '*'});
            res.end(JSON.stringify({"error":"There was an error querying the database"}));
            return true;
        };
        // handle an error from the connection
        if(handleError(err)) return;
        var sql = "SELECT * FROM google_route_processing('" + coordinates + "');";
        client.query(sql, function(err, result) {
            // handle an error from the query
            if(handleError(err)) return;
            // return the client to the connection pool for other requests to reuse
            done();
            res.writeHead(200, {'Content-Type': 'application/json','Access-Control-Allow-Origin' : '*'});
            if (result.rowCount == 0) {
                res.end(JSON.stringify({"error":"No results"}))
            }
            var scores = {'day': 0, 'night': 0};
            var scoring = {"red": 1, "yellow": 2, "green": 3};
            for (var row in result.rows) {
                scores['day'] += scoring[result.rows[row]['day_score']];
                scores['night'] += scoring[result.rows[row]['night_score']];
            }
            var response = {
                "scores": {
                    "day": scores['day']/result.rowCount,
                    "night": scores['night']/result.rowCount
                },
                "roads": result.rows
            };
            res.end(JSON.stringify(response));
        });
    });
});<commit_msg>Weighted the scoring based on line length<commit_after>//Lets require/import the HTTP module
var http = require('http');
var dispatcher = require('httpdispatcher');
var pg = require('pg');

//Create the connection to the db
var conString = "postgres://blacksburg_read:nrv@postgres1.ceipocejvkue.us-west-2.rds.amazonaws.com/blacksburg";

//Lets define a port we want to listen to
const PORT=(process.env.PORT || 5000);

//We need a function which handles requests and send response
function handleRequest(request, response){
    try {
        //log the request on console
        //console.log(request.url);
        //Disptach
        dispatcher.dispatch(request, response);
    } catch(err) {
        console.log(err);
    }
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening");
});


//For all your static (js/css/images/etc.) set the directory name (relative path).
dispatcher.setStatic('resources');

//A GET request
dispatcher.onGet("/inBlacksburg", function(req, res) {
    var lat = req.params.lat;
    var lon = req.params.lon;
    // get a pg client from the connection pool
    pg.connect(conString, function(err, client, done) {
        var handleError = function(err) {
            // no error occurred, continue with the request
            if(!err) return false;
            // An error occurred, remove the client from the connection pool.
            // A truthy value passed to done will remove the connection from the pool
            // instead of simply returning it to be reused.
            // In this case, if we have successfully received a client (truthy)
            // then it will be removed from the pool.
            if(client){
                done(client);
            }
            console.log(err);
            res.writeHead(500, {'content-type': 'application/json','Access-Control-Allow-Origin' : '*'});
            res.end(JSON.stringify({"error":"There was an error querying the database"}));
            return true;
        };
        // handle an error from the connection
        if(handleError(err)) return;
        var sql = "SELECT ST_WITHIN(ST_GeometryFromText('POINT(" + lon + " " + lat + ")', 4326),ST_TRANSFORM(ST_SetSRID(geom, 2284), 4326)) AS in_blacksburg FROM corporate_boundary;";
        client.query(sql, function(err, result) {
            // handle an error from the query
            if(handleError(err)) return;
            // return the client to the connection pool for other requests to reuse
            done();
            res.writeHead(200, {'Content-Type': 'application/json','Access-Control-Allow-Origin' : '*'});
            res.end(JSON.stringify(result.rows[0]));
        });
    });
});

//A POST request
dispatcher.onPost("/score", function(req, res) {
    var coordinates = req.body;
    // get a pg client from the connection pool
    pg.connect(conString, function(err, client, done) {
        var handleError = function(err) {
            // no error occurred, continue with the request
            if(!err) return false;
            if(client){
                done(client);
            }
            res.writeHead(500, {'content-type': 'application/json','Access-Control-Allow-Origin' : '*'});
            res.end(JSON.stringify({"error":"There was an error querying the database"}));
            return true;
        };
        // handle an error from the connection
        if(handleError(err)) return;
        var sql = "SELECT * FROM google_route_processing('" + coordinates + "');";
        client.query(sql, function(err, result) {
            // handle an error from the query
            if(handleError(err)) return;
            // return the client to the connection pool for other requests to reuse
            done();
            res.writeHead(200, {'Content-Type': 'application/json','Access-Control-Allow-Origin' : '*'});
            if (result.rowCount == 0) {
                res.end(JSON.stringify({"error":"No results"}))
            }
            var scores = {'day': 0, 'night': 0};
            var scoring = {"red": 1, "yellow": 2, "green": 3};
            var totalLength = 0;
            for (var row in result.rows) {
                var lineLength = result.rows[row]['mylinedistance'];
                scores['day'] += scoring[result.rows[row]['day_score']]*lineLength;
                scores['night'] += scoring[result.rows[row]['night_score']]*lineLength;
                totalLength += lineLength;
            }
            var response = {
                "scores": {
                    "day": scores['day']/totalLength,
                    "night": scores['night']/totalLength
                },
                "roads": result.rows
            };
            res.end(JSON.stringify(response));
        });
    });
});
{'fraction_non_alphanumeric': 0.13010495687415566, 'fraction_numerical': 0.00779382728878728, 'mean_word_length': 2.21765295887663, 'pattern_counts': {'":': 20, '<': 3, '<?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': '3029465', 'n_tokens_mistral': 2647, 'n_tokens_neox': 2558, 'n_words': 758}