<commit_before>//postgis-preview
//A super simple node app + leaflet frontend for quickly viewing PostGIS query results

//dependencies
var express = require('express'),
  Mustache = require('mustache'),
  pgp = require('pg-promise')(),
  dbgeo = require('dbgeo'),
  jsonexport = require('jsonexport');

//create express app and prepare db connection
var app = express(),
  port = process.env.PORT || 4000,
  config = require('./config.js'),
  db = pgp(config);

//use express static to serve up the frontend
app.use(express.static(__dirname + '/public'));

//expose sql endpoint, grab query as URL parameter and send it to the database
app.get('/sql', function(req, res){
  var sql = req.query.q;
  var format = req.query.format || 'topojson';
  console.log('Executing SQL: ' + sql, format);

  //query using pg-promise
  db.any(sql)
    .then(function (data) { //use dbgeo to convert WKB from PostGIS into topojson
        if(format=='csv') {
          return jsonExport(data);
        } else {
          return dbGeoParse(data, format);
        }
       
    })
    .then(function (data) {
      if(format=='csv'){
        res.setHeader('Content-disposition', 'attachment; filename=query.csv');
        res.setHeader('Content-Type','text/csv');
        res.send(data);
      } else if (format=='geojson') {
        res.setHeader('Content-disposition', 'attachment; filename=query.geojson');
        res.setHeader('Content-Type','application/json');
        res.send(data);
      } else {
        res.send(data);
      }
        
    })
    .catch(function (err) { //send the error message if the query didn't work
        var msg = err.message || err;
        console.log("ERROR:", msg);
        res.send({
            error: msg
        });
    });

});

function dbGeoParse(data, format) {
    return new Promise(function (resolve, reject) {
        dbgeo.parse({
            data: data,
            outputFormat: format,
            geometryColumn: 'geom',
            geometryType: 'wkb'
        }, function (err, result) {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
}

function jsonExport(data) {
  //remove geom
  data.forEach(function(row) {
    (row.geom) ? delete row.geom : null;
  });

  return new Promise(function (resolve, reject) {
      jsonexport(data,function(err, csv){
      if(err) return console.log(err);
      resolve(csv);
    });
  });
}

//start the server
app.listen(port);
console.log('postgis-preview is listening on port ' + port + '...');<commit_msg>Update server.js<commit_after>//postgis-preview
//A super simple node app + leaflet frontend for quickly viewing PostGIS query results

//dependencies
var express = require('express'),
    Mustache = require('mustache'),
    pgp = require('pg-promise')(),
    dbgeo = require('dbgeo'),
    jsonexport = require('jsonexport');

//create express app and prepare db connection
var app = express(),
    port = process.env.PORT || 4000,
    config = require('./config.js'),
    db = pgp(config);

//use express static to serve up the frontend
app.use(express.static(__dirname + '/public'));

//expose sql endpoint, grab query as URL parameter and send it to the database
app.get('/sql', function (req, res) {
    var sql = req.query.q;
    var format = req.query.format || 'topojson';
    console.log('Executing SQL: ' + sql, format);

    //query using pg-promise
    db.any(sql)
        .then(function (data) { //use dbgeo to convert WKB from PostGIS into topojson
            return format == 'csv' ? jsonExport(data) : dbGeoParse(data, format);
        })
        .then(function (data) {
            if (format == 'csv') {
                res.setHeader('Content-disposition', 'attachment; filename=query.csv');
                res.setHeader('Content-Type', 'text/csv');
                res.send(data);
            } else if (format == 'geojson') {
                res.setHeader('Content-disposition', 'attachment; filename=query.geojson');
                res.setHeader('Content-Type', 'application/json');
                res.send(data);
            } else {
                res.send(data);
            }
        })
        .catch(function (err) { //send the error message if the query didn't work
            var msg = err.message || err;
            console.log("ERROR:", msg);
            res.send({
                error: msg
            });
        });

});

function dbGeoParse(data, format) {
    return new Promise(function (resolve, reject) {
        dbgeo.parse({
            data: data,
            outputFormat: format,
            geometryColumn: 'geom',
            geometryType: 'wkb'
        }, function (err, result) {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
}

function jsonExport(data) {
    //remove geom
    data.forEach(function (row) {
        delete row.geom;
    });
    
    return new Promise(function (resolve, reject) {
        jsonexport(data, function (err, csv) {
            if (err) {
                reject(err);
            } else {
                resolve(csv);
            }
        });
    });
}

//start the server
app.listen(port);
console.log('postgis-preview is listening on port ' + port + '...');
{'fraction_non_alphanumeric': 0.14240090174713507, 'fraction_numerical': 0.0015029118917903438, 'mean_word_length': 2.126247798003523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2513232', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1535, 'n_words': 400}