-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
131 lines (120 loc) · 3.13 KB
/
app.js
File metadata and controls
131 lines (120 loc) · 3.13 KB
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
var restify = require('restify');
var request = require('request');
var redis = require("redis");
const server = restify.createServer({
name: 'arewetwet',
version: '1.0.0'
});
const redisCli = redis.createClient('redis://arewewet-bdd:6379');
redisCli.on("error", function (err) {
console.log("Error " + err);
});
const constants = {
'restEndpoint': 'http://www.meteofrance.com/mf3-rpc-portlet/rest/pluie/'
}
// Get data for city
function getRest(city) {
return new Promise((resolve, reject) => {
request(constants.restEndpoint + city, function (error, response, body) {
if (error) reject(error);
else resolve(JSON.parse(body));
});
});
}
// Store cache in redis
function redisCache(city) {
return new Promise((resolve, reject) => {
getRest(city)
.then(function (data) {
var obj = {};
var a = 0;
for (var i in data.dataCadran) {
var key = a;
var value = data.dataCadran[i].niveauPluie;
obj[key] = value;
a += 5;
}
console.log('Inserting ' + city + ' : ' + JSON.stringify(obj));
redisCli.set(city, JSON.stringify(obj), 'EX', 300);
resolve(data);
}).catch(function (err) {
reject(err);
});
});
}
// Get cities from redis
function redisGet(cities) {
return new Promise((resolve, reject) => {
redisCli.mget(cities, function(err, values) {
if (err) reject(err);
else {
// Build object
var obj = {};
for (var i in cities) {
obj[cities[i]] = JSON.parse(values[i]);
}
resolve(obj);
}
});
});
}
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
// Get data for city
server.get('/api/rest/:city', function (req, res, next) {
getRest(req.params.city)
.then(function (data) {
res.json(data);
}).catch(function (err) {
res.json(err);
});
return next();
});
// Get moving
server.get('/api/move/:move', function (req, res, next) {
var move = req.params.move.split(',');
var cities = [];
var whereWeAre = {};
for (var m in move) {
var arr = move[m].split(':');
var city = arr[0];
var time = arr[1];
cities.push(city);
var i = parseInt(time, 10);
while (i <= move[move.length-1].split(':')[1]) {
whereWeAre[i] = city;
i += 5;
}
}
console.log(whereWeAre);
// Get Meteo
var redisCachePromises = cities.map(function (city) {
return redisCache(city);
});
Promise.all(redisCachePromises)
.then(function(results) {
return redisGet(cities);
}).then(function (results) {
// Move compute
var obj = {};
// From departure city, for each 5min in the hour
var i = 0;
while (i < 60) {
console.log("On part @" + i);
obj[i] = {'run': {}};
// Run
for (var w in whereWeAre) {
var city = whereWeAre[w];
var when = parseInt(w, 10) + i;
obj[i].run[w] = results[city][when];
}
i += 5;
}
res.json({"move": obj, "raw": results});
});
});
// Start
server.listen(1664, function () {
console.log('%s listening at %s', server.name, server.url);
});