-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (58 loc) · 1.76 KB
/
server.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
var express = require('express');
var app = express();
const { lunchbox } = require('./model');
const minPrice = 7;
const maxPrice = 20;
const nextWeekBudget = (budget) => {
const price = [];
let leftover = budget;
while (price.length < 5) {
// get a random price
const newPrice = Math.random() * (maxPrice * 0.8 - minPrice) + minPrice;
// check if the budget still has space for it
if ((leftover - newPrice) > minPrice) {
leftover = leftover - newPrice;
price.push(Math.floor(newPrice * 100) / 100);
} else {
// well, we still gotta eat...
price.push(minPrice);
}
}
// return random prices for each of the weekdays
return price;
}
const calculateFeatures = (budget) => {
const features = [];
const prices = nextWeekBudget(budget);
for (let i = 0; i < 5; i++) {
const date = new Date();
date.setDate(date.getDate() + i + 1);
features.push([prices[i], date.getDay(), date.getDate(), date.getMonth() + 1, 0]);
}
return features;
}
app.get('/mealplan', function (request, response) {
if (request.query) {
const { budget, seed, maxFeatures, nEstimators} = request.query;
const features = calculateFeatures(budget);
const params = {
options: {
seed: parseInt(seed),
maxFeatures: parseFloat(maxFeatures),
replacement: true,
nEstimators: parseInt(nEstimators),
},
features: features,
};
// get predictions for given parameters
lunchbox(params).then((result) => {
console.log('Recipes ready!', result.prediction);
// sending back predictions to front end
response.send(result);
});
}
});
app.listen(8080, function () {
console.log('Server has started listening on port 8080.');
});
app.use(express.static('src'));