-
Notifications
You must be signed in to change notification settings - Fork 0
/
testEndpoint.js
92 lines (82 loc) · 2.91 KB
/
testEndpoint.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
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const fetch = require("node-fetch");
const stationInfo = require('./stationInfo');
var hrstart = process.hrtime()
function exportNewData(newData) {
fs.writeFile("./result.txt", JSON.stringify(newData), err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
}
let fetchRequestForGettingData = function (departure, arrival) {
return fetch("http://192.168.1.54:8000/api/route", {
method: "POST",
body: JSON.stringify({
departure: {
details: departure
},
arrival: {
details: arrival
},
time: {
date: {
real: "20190902"
},
time: "10:00"
}
})
}).then(response => {
if (response.ok) {
return response.json();
} else {
return {error: true, msg: '', data: response};
}
}).then(json => {
return {error: false, msg: 'ok', data: json};
}).catch(function (error) {
console.log('Request failed ', error);
return {error: true, msg: '', data: error};
})
};
const startTesting = async function () {
let resultString = "";
for (let i = 0; i < stationInfo.stations.length; i++) {
let firstElement = stationInfo.stations[i];
for (let j = 0; j < stationInfo.stations.length; j++) {
if (firstElement !== stationInfo.stations[j]) {
// let result = await fetchRequestForGettingData(firstElement, stationInfo.stations[j]);
// resultString += checkResultOfRequest(result, firstElement, stationInfo.stations[j]);
resultString += await doRequest(firstElement, stationInfo.stations[j]);
}
}
exportNewData(resultString);
hrend = process.hrtime(hrstart);
let minutes = ~~(hrend[0] / 60);
let seconds = hrend[0] - minutes * 60;
console.info('Execution time (hr): %dm %ds %dms', minutes, seconds, hrend[1] / 1000000);
}
exportNewData(resultString);
};
async function doRequest(firstElement, secondStation) {
let result = await fetchRequestForGettingData(firstElement, secondStation);
if ("data" in result.data.data ){
return checkResultOfRequest(result, firstElement, secondStation)
} else {
setTimeout(async function () {
return await doRequest(firstElement, secondStation)
}, 1000)
}
}
function checkResultOfRequest(result, departure, arrival) {
// if ("data" in result.data.data ){
return`${departure.eng站名},${arrival.eng站名},${result.data.data.data.length} \n `;
// } else {
// return`${departure.eng站名},${arrival.eng站名},0\n `;
// }
}
startTesting();