-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.js
165 lines (135 loc) · 4.12 KB
/
requests.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var express = require('express');
var router = express.Router();
const spawn = require('child_process').spawn;
/* Test */
/*
router.get('/', function(req, res, next) {
var child = spawn('ls', [ '-lh', '/usr' ] );
child.stdout.on('data', function(chunk) {
// output will be here in chunks
process.stdout.write(chunk + '\n');
});
child.stderr.on('data', function(chunk) {
// output will be here in chunks
process.stdout.write(chunk + '\n');
});
res.render('new', { title: 'Express' });
});
*/
/* GET All Requests. */
router.get('/', function (req, res, next) {
var db = req.db;
var requests = db.get('requests');
requests.find({ email: req.user.email }, {}, function (e, rqsts) {
res.json(201, rqsts);
});
});
/* Write to the requests file */
router.post('/', function (req, res, next) {
var db = req.db;
var requests = db.get('requests');
/*
misc:
email:[email protected]
status:EMAILED
aggregationmetric:
lats:Array[3]
timelogged:
longs:Array[3]
variable:Array[1]
interval:
text:Request for extract
enddate:19810102
outputformat:csv
startdate:19810102
sourcetype:ErA5
*/
// If not multiple select
//var variable = req.body.variable;
// If multiple select
var variable = req.body["variable[]"];
var latN = req.body.latN;
var latS = req.body.latS;
var lonW = req.body.lonW;
var lonE = req.body.lonE;
var stdate = req.body.startdate;
var eddate = req.body.enddate;
var file = req.body.file;
var email = req.body.email;
var dateperformed = req.body.dateperformed;
var shadelevel = req.body.shadelevel;
var hod = req.body.hod;
var interval = req.body.interval;
var aggregation = req.body.aggregation;
var sourcetype = req.body.sourcetype;
if (variable != null && !Array.isArray(variable))
variable = [variable];
//latS,latN,lonW,lonE
var new_request = {
misc: "",
email: email,
status: "OPEN",
lats: [latS, latN],
timelogged: dateperformed,
longs: [lonW, lonE],
variable: variable,
text: "",
shadelevel: shadelevel,
hod: hod,
interval: interval,
aggregation: aggregation,
enddate: eddate,
outputformat: file,
startdate: stdate,
status_message: "",
sourcetype: sourcetype,
version:"2"
};
//Using the {w:1} option ensure you get the error back if the document fails to insert correctly.
//TODO handle error
//const sgMail = require('@sendgrid/mail')
//const sgMailApiKey = ''
//sgMail.setApiKey(sgMailApiKey)
requests.insert(new_request, { w: 1 }, function (err, request_added) {
//Request logged
if (request_added) {
const childPython = spawn('python', ['index1.py']);
childPython.stdout.on('data', (data) => {
var result = `${data}`;
console.log(result);
});
childPython.stderr.on(`data`, (data) => {
console.error(`stderr: ${data}`);
});
childPython.on('close', (code) => {
console.log(`childprocess exited with code ${code}`)
})
//sgMail.setApiKey(process.env.SENDGRID_API_KEY)
/*
const msg = {
to: req.body.email, // Change to your recipient
from: '', // Change to your verified sender
subject: 'Message from send grid',
text: 'Message from Send Grid',
}
sgMail.send(msg)
.then((respose) => {
console.log('Email sent')
})
.catch((error) => {
console.error(error)
})
*/
res.json(201, {
success: 'Request logged',
request_id: request_added._id.toString()
})
}
else {
res.json(500, {
error: 'Request not logged.'
});
}
});
});
module.exports = router;