forked from coderdj/nodiaq
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathruns_mongo.js
74 lines (67 loc) · 2.77 KB
/
runs_mongo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var mongoose = require('mongoose');
var DataTable = require('mongoose-datatable').default;
var runsModel;
var dbURI = process.env.RUNS_URI;
var runsdb = mongoose.connection;
var runs;
var runsTableSchema;
//DataTable.configure({ verbose: false, debug : false });
mongoose.plugin(DataTable.init);
mongoose.connect(dbURI, {authSource : process.env.RUNS_MONGO_AUTH_DB, useNewUrlParser:true, useUnifiedTopology: true});
runsdb.on('error', console.error.bind(console, 'connection error:'));
runsdb.once('open', function callback ()
{
//console.log('Connection has succesfully opened');
var Schema = mongoose.Schema;
runsTableSchema = new Schema(
{
number : {type: Number, required: true},
detectors: [String],
start : Date,
end : Date,
user: String,
mode: String,
source: String,
bootstrax: [{state: String, host: String, time: Date, started_processing: Date}],
restrax: [{state: String, host: String}],
tags: [ {user: String, date: Date, name: String} ],
comments: [{user: String, date: Date, text: String}],
},
{ collection: process.env.RUNS_MONGO_COLLECTION});
runs = mongoose.model('runs', runsTableSchema);
runsModel = require('mongoose').model('runs');
});
exports.getDataForDataTable = function getData (request, response) {
var conditions = {};
if(typeof request.query['conditions'] !== 'undefined')
conditions = JSON.parse(request.query['conditions']);
// Date filtering
if(request.query['date_min'] !== undefined){
if(request.query['date_min'] !== '' &&
request.query['date_max'] == '' &&
!('start' in Object.keys(conditions)))
conditions['start'] = {"$gt": new Date(request.query['date_min'])};
else if(request.query['date_min'] !== '' &&
request.query['date_max'] !== '' &&
!('start' in Object.keys(conditions)))
conditions['start'] = {"$gt": new Date(request.query['date_min']),
"$lt": new Date(request.query['date_max'])};
else if(request.query['date_min'] == '' &&
request.query['date_max'] !== '' &&
!('start' in Object.keys(conditions)))
conditions['start'] = {"$lt": new Date(request.query['date_max'])};
}
//console.log(conditions);
runsModel.dataTable(request.query, {conditions: conditions})
.then((data) => response.send(data))
.catch(function(err) {
console.log("RUNS DB ERROR " + err); return response.send({error: err})
});
/* runsModel.dataTable(request.query, {"conditions": conditions}).then(
function (data) {
response.send(data);
}).catch(
function(err){
console.log('RUNS DB ERROR ' + err);
});*/
};