-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
336 lines (269 loc) · 9.19 KB
/
controller.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
var {
firebase,
db
} = require('./init');
var _ = require('lodash');
var axios = require('axios');
let t = 1800; // Time limit for every order
var dataDict = [];
function storeInit(json) { // API function for onboarding
for (data of json.branches) {
console.log(json.delB);
db.collection(json.name).doc(data.name + "_" + data.pin).set({
pincode: data.pin,
name: data.name + "_" + data.pin,
lat: data.lat,
lng: data.lng,
limit: data.limit,
delB: json.delB,
load: {}
})
}
}
async function getLogs(store, keys) { // get Store info
let mydocs = []
if (keys == undefined) {
let qs = await db.collection(store).get();
return qs.docs;
}
for (key of keys) {
let doc = await db.collection(store).doc(key).get()
mydocs.push(doc)
}
return mydocs;
}
async function brain(lat, lng) {
let arr = await getLogs("McDonalds");
let mainArr = [];
arr.forEach(x => {
mainArr.push(x.data())
});
db.collection("live").doc(lat + "_" + lng).set({
lat,
lng,
serve: false
});
let L1filteredBranches = L1calc(mainArr,lat,lng);
var payload1=JSON.parse(JSON.stringify(L1filteredBranches));
console.log("===============Level 1 ===========Layer")
console.log("\n");
console.log(payload1)
console.log("=========================")
let cacheFlag;
if ((cacheFlag = check(lat, lng))) {
console.log("cache hit !!!! Evading API call");
//Cache HIT
cacheFlag.data.forEach(ele => {
L1filteredBranches.forEach(sub => {
if (sub.name == ele.name) {
ele.load = sub.load;
ele.delB = sub.delB;
return;
}
})
});
L2calc(cacheFlag.data, lat, lng,payload1);
} else { // Cache MISS
console.log("Cache MISSSS");
await ETAcalc(L1filteredBranches, [{
lat,
lng
}]);
dataDict.push({
data: L1filteredBranches,
lat,
lng
})
//update cache
return L2calc(L1filteredBranches, lat, lng,payload1)
}
}
function check(lt, ln) { // Function which checks if result exists in cache
let least = 500;
let filt = dataDict.filter(x => {
let imp = convertToM(x.lat, x.lng, lt, ln);
if (least >= imp) {
least = imp;
return true;
}
});
if (filt.length == 0) {
return false;
} else {
return filt[filt.length - 1];
}
}
function L1calc(mainArr,lat,lng) { // 1st layer filtering algorithm ( Pure function -- Functional programming)
let L1filteredBranches = _.chain(mainArr)
.filter(x => x.limit != x.load["05/01/2020"] && x.delB > 0)
.map((branch) => (branch.distance = convertToM(lat, lng, branch.lat, branch.lng), branch)) //Distance calc
.sortBy(branch => branch.distance)
.filter((x, i, a) => i <= Math.ceil(a.length * 0.5) - 1)
.value();
return L1filteredBranches;
}
function L2calc(L1filteredBranches,lat,lng,payload1) { // 2nd Layer decision Tree To judge,assert and allocate
console.log("After ETA Calculation - Layer 2");
console.log("=====================")
console.log(L1filteredBranches);
console.log("============================");
var L2filteredBranches = _.chain(L1filteredBranches)
.sortBy(branch => branch["ETA"])
.filter(x => x["ETA"] <= (t - 300))
.value();
var finalFiltered;
var payload=JSON.parse(JSON.stringify(L2filteredBranches));
console.log("Sorting ETA with demand-delivery")
console.log("===============Level 3 ===========Layer");
console.log(payload);
console.log("=======================================")
if (L2filteredBranches.length >= 2) {
L2filteredBranches.forEach((l) => {
l.magic = l.limit - l.load["05/01/2020"]
});
console.log("Final layer=========================")
finalFiltered = _.chain(L2filteredBranches).sortBy(branch => branch.magic).value().reverse();
console.log(finalFiltered);
console.log("=================================")
if (finalFiltered[0].magic > finalFiltered[1].magic) {
console.log(finalFiltered[0].load,"====?",finalFiltered[0].load["05/01/2020"]+1);
incLoad(finalFiltered[0].name,finalFiltered[0].load["05/01/2020"]+1);
decDelB(finalFiltered[0].name, finalFiltered[0].lat, finalFiltered[0].lng,lat,lng,payload,payload1,finalFiltered);
return finalFiltered[0]
} else {
let needed = finalFiltered[0].magic
finalFiltered = finalFiltered.filter(x => x.magic == needed)
let assignedBatch = _.chain(finalFiltered).sortBy(x => x.limit).value().reverse()[0];
incLoad(assignedBatch.name,assignedBatch.load["05/01/2020"]+1);
decDelB(assignedBatch.name, assignedBatch.lat, assignedBatch.lng,lat,lng,payload,payload1,finalFiltered);
return assignedBatch;
}
} else {
console.log("Final layer=========================")
console.log(L2filteredBranches[0]);
console.log("======================================");
incLoad(L2filteredBranches[0].name,L2filteredBranches[0].load["05/01/2020"]+1);
decDelB(L2filteredBranches[0].name, L2filteredBranches[0].lat, L2filteredBranches[0].lng,lat,lng,payload,payload1,L2filteredBranches[0]);
return L2filteredBranches[0]
}
}
function incLoad(branch,nos) { //util function that increases the load
upLoads("McDonalds", [
[branch,nos]
]);
dataDict.forEach((x)=>{
x['data'].forEach((l)=>{
if(l.name==branch){
l.load=nos
}
})
})
}
function decDelB(branch,rlat,rlng,lat, lng,payload,payload1,finalFiltered) {
updelB("McDonalds", [
[branch]
]);
dataDict.forEach((x)=>{
x['data'].forEach((l)=>{
if(l.name==branch){
l.delB=l.delB-1
}
})
})
db.collection("live").doc(lat + "_" + lng).update({
serve: true,
rlat,
rlng,
payload,
payload1,
finalFiltered
});
}
function convertToM(lat, long, lat1, long1) { //Math function to calculate Distance between two nodes in metres
function toRadians(l) {
return (Math.PI * l) / 180;
}
var R = 6371e3;
var φ1 = toRadians(lat);
var φ2 = toRadians(lat1);
var Δφ = toRadians((lat1 - lat));
var Δλ = toRadians((long1 - long));
var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
};
async function ETAcalc(source, dest) { //Api call for Distance Matrix
var url = `https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=${strUtils(source)}&destinations=${strUtils(dest)}&mode=driving&departure_time=${Math.floor(+new Date()/1000)}&traffic_model=pessimistic&key=AIzaSyC-9MbkfsOTr2OKxote-YJzqIsq-FnBUdU`;
let response = await axios.get(url)
response.data["rows"].forEach((x, i) => {
source[i]["ETA"] = x["elements"][0]["duration_in_traffic"]["value"];
source[i]["realDistance"] = x["elements"][0]["distance"]["value"];
});
return true;
}
function strUtils(arr) { //Utils function for preparing api hit
var target = "";
arr.forEach((x, i, a) => {
target += x.lat + ',' + x.lng;
if (i != a.length - 1) {
target += "|"
};
});
return target;
}
async function upLoads(store, obj, i,nos) { //Change Load api
obj.forEach((x) => {
if (x[1] == undefined) {
if (i != undefined) {
console.log("in");
db.collection(store).doc(x[0]).update({
load: {
"05/01/2020": nos
}
});
return;
}
db.collection(store).doc(x[0]).update({
load: {
"05/01/2020": nos
}
})
} else {
db.collection(store).doc(x[0]).update({
load: {
"05/01/2020": x[1]
}
})
}
});
}
function updelB(store, obj, i) { // Change delivery boy count
obj.forEach((x) => {
if (x[1] == undefined) {
if (i != undefined) {
db.collection(store).doc(x[0]).update({
"delB": firebase.firestore.FieldValue.increment(1)
});
return;
}
console.log(store, x[0]);
db.collection(store).doc(x[0]).update({
"delB": firebase.firestore.FieldValue.increment(-1)
})
} else {
db.collection(store).doc(x[0]).update({
"delB": x[1]
})
}
});
}
module.exports = {
updelB,
upLoads,
getLogs,
storeInit,
brain
}