-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
72 lines (62 loc) · 1.97 KB
/
handler.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
import * as dynamoDbLib from "./libs/dynamo-lib";
import { success, failure } from "./libs/response-lib";
import { getDistance } from 'geolib';
export const hello = async (event, context) => {
const data = JSON.parse(event.body);
console.log(data.country);
const params = {
TableName: process.env.tableName,
FilterExpression: "runway_length >= :max_length and type_airport = :type and country = :country",
ExpressionAttributeValues: {
":max_length": 7000,
":type": "Airports",
":country": data.country,
}
};
let airports;
try {
const results = await dynamoDbLib.call("scan", params);
if (results.Items) {
airports = results.Items;
} else {
return failure({ status: false, error: "No airports found" });
}
} catch (e) {
console.log(e);
return failure({ status: false });
}
let i, j;
let lst = [];
let backup_lst = [];
for (i = 0; i < airports.length; i++) {
for (j = 0; j < airports.length; j++) {
let distance = getDistance(
{ latitude: parseFloat(airports[i].lat), longitude: parseFloat(airports[i].lon) },
{ latitude: parseFloat(airports[j].lat), longitude: parseFloat(airports[j].lon) }
);
if (distance/1000 >= 200) {
let pair = { airport1: i, airport2: j, distance: distance };
backup_lst.push(pair);
}
if (distance/1000 >= 0.8*data.distance && distance/1000 <= data.distance ) {
let pair = { airport1: i, airport2: j, distance: distance };
lst.push(pair);
}
}
}
if (lst.length == 0) {
if (backup_lst.length == 0) {
return failure({ status: false, error: "No airports found" });
} else {
lst = backup_lst;
}
}
let randomNum = Math.floor(Math.random() * lst.length);
let airport_dic = lst[randomNum];
let resp = {
'distance': airport_dic.distance / 1000,
'airport1': airports[airport_dic.airport1],
'airport2': airports[airport_dic.airport2]
};
return success(resp);
};