const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');
const mongodb = require('mongodb');
const cors = require('cors');
const _ = require('lodash');


const ObjectID = mongodb.ObjectID;

const app = express();

app.use(cors());
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json({limit: '5mb'}));

const RESULTS_COLLECTION = 'results';

// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
let db;

function startApp() {
  // Initialize the app.
  const port = process.env.PORT || 8080;
  const server = app.listen(port, () => {
    const port = server.address().port;
    console.log('App now running on port', port);
  });
}

if (process.env.MONGODB_URI) {
  // Connect to the database before starting the application server.
  mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
    if (err) {
      console.log('Unable to connect to MongoDB')
      console.log(err);
      process.exit(1);
    }

    // Save database object from the callback for reuse.
    db = database;
    console.log('Database connection ready');

    startApp();
  });
} else {
  console.log('MongoDB not available, starting without it');
  startApp();
}

app.get('/', (req, res) => {
  res.sendFile('./results.html', { root: './' });
});

app.post('/results', (req, res, next) => {
  const newRecord = req.body;

  db.collection(RESULTS_COLLECTION).insertOne(newRecord, (err, doc) => {
    if (err) { return next(err); }

    res.status(201).json(doc.ops[0]);
  });
});

app.get('/results', function(req, res) {
  db.collection(RESULTS_COLLECTION).find({}).toArray((err, results) => {
    if (err) { return next(err); }

    const markers = [
      {
        timestamp: '2016-11-26T12:11:11.831Z',
        label: 'Restarted router'
      }
    ];

    const firstResult = _.find(results, d => d._id.valueOf() === '583903393033b1001134f615');
    if (firstResult) {
      markers.push({
        timestamp: firstResult.timestamp,
        label: 'Speedlog created'
      });
    }

    res.status(200).json({
      results: _.sortBy(results, d => new Date(d.timestamp)),
      markers,
    });
  });
});

app.post('/health', (req, res) => {
  console.log(req.body);
  res.status(200).send('ok');
});
@@ -1606,24 +1606,127 @@
 %0A  %7D);%0A%7D);%0A%0A
+const findResult = (results, id) =%3E %7B%0A  return _.find(results, d =%3E d._id.toString() === id) %7C%7C %7B%7D;%0A%7D%0A%0A
 app.get('/re
@@ -1916,34 +1916,65 @@
 mp: 
-'2016-11-26T12:11:11.831Z'
+findResult(results, '583903393033b1001134f615').timestamp
 ,%0A  
@@ -1991,24 +1991,24 @@
 l: '
-Restarted router
+Speedlog created
 '%0A  
@@ -2016,246 +2016,287 @@
    %7D
+, %7B
 %0A    
-%5D;%0A%0A    const firstResult = _.find(results, d =%3E d._id.valueOf() === '583903393033b1001134f615');%0A    if (firstResult) %7B%0A      markers.push(%7B%0A        timestamp: firstResult.timestamp,%0A        label: 'Speedlog created'%0A      %7D);%0A    %7D
+    timestamp: '2016-11-26T12:11:11.831Z',%0A        label: 'Restarted router'%0A      %7D, %7B%0A        timestamp: findResult(results, '583acb0c5621b50011e2bcfa').timestamp,%0A        label: 'Run every 10 mins'%0A      %7D%0A    %5D;%0A%0A    // %222016-11-27T12:01:00.841Z%22%0A%0A    markers.push();
 %0A%0A  

Add new event