<commit_before>var express = require('express');
var url = require('url');
var _ = require('lodash')
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser')

// create and configure server
var app = express();
app.use(bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({    // to support URL-encoded bodies
  extended: true
}));




// api routes
// helper for db connection pooling
var pg = require('pg');
function queryDatabase(text, values, cb) {
  pg.connect(process.env.DATABASE_URL, function(err, client, done) {
    client.query(text, values, function(err, result) {
      done();
      cb(err, result);
    });
  });
};

// This endpoint receives all evidence, 
app.post('/server/evidence/:app/:type/:version', function(request, response) {
  const timestamp = new Date().getTime();
  const {app, type, version} = request.params;
  const payload = JSON.stringify(request.body);
  const values = [app, type, version, payload, timestamp];

  const sql = `
    INSERT INTO evidence(payload, timestamp)
    VALUES ($1,$2,$3,$4,$5)`;
  queryDatabase(sql, values, function(err, result) {
    if (err) {
      console.log({ error: err });
      return response.code(500);
    }
    console.log(JSON.stringify(result));
    response.json({result});  
  });
});

app.get('/server/dump', function(request, response) {
  queryDatabase('SELECT * FROM evidence', [], function(err, result) {
    if (err) {
      console.log({ error: err });
      return response.code(500);
    }
    console.log(JSON.stringify(result));<commit_msg>Fix insert query bug<commit_after>var express = require('express');
var url = require('url');
var _ = require('lodash')
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser')

// create and configure server
var app = express();
app.use(bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({    // to support URL-encoded bodies
  extended: true
}));




// api routes
// helper for db connection pooling
var pg = require('pg');
function queryDatabase(text, values, cb) {
  pg.connect(process.env.DATABASE_URL, function(err, client, done) {
    client.query(text, values, function(err, result) {
      done();
      cb(err, result);
    });
  });
};

// This endpoint that receives all evidence.
// The payload is determined by the type, but for now it only
// supports JSON serialization and puts everything in the same
// Postgres table with a jsonb column.
app.post('/server/evidence/:app/:type/:version', function(request, response) {
  const timestamp = new Date().getTime();
  const {app, type, version} = request.params;
  const payload = JSON.stringify(request.body);
  const values = [app, type, version, payload, timestamp];

  const sql = `
    INSERT INTO evidence(app, type, version, timestamp, json)
    VALUES ($1,$2,$3,$4,$5)`;
  queryDatabase(sql, values, function(err, result) {
    if (err) {
      console.log({ error: err });
      return response.code(500);
    }
    console.log(JSON.stringify(result));
    response.json({result});  
  });
});

// For debugging.
app.get('/server/dump', function(request, response) {
  queryDatabase('SELECT * FROM evidence', [], function(err, result) {
    if (err) {
      console.log({ error: err });
      return response.code(500);
    }
    console.log(JSON.stringify(result));
{'fraction_non_alphanumeric': 0.16042154566744732, 'fraction_numerical': 0.006440281030444965, 'mean_word_length': 3.752433936022253, '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': '7271280', 'n_tokens_mistral': 1040, 'n_tokens_neox': 1016, 'n_words': 285}