-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_simulation.js
118 lines (110 loc) · 3.63 KB
/
user_simulation.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
/**
* Used to simulate a system of X users towards a Sparkrentals API.
*/
const axios = require("axios");
const db = require("./modules/sparkdb");
require("dotenv").config();
const api = process.env.API_URL;
const adminLoginEndpoint = "/auth/login/server/admin";
const rentEndpoint = "/scooters/rent";
const stopEndpoint = "/scooters/stop";
const apiKey = process.env.REACT_APP_REST_API_KEY;
const rentWatchPollRate = 5000;
const minBalance = 50;
let token = null;
let users = [];
const admin = {
email: process.env.SIMULATION_EMAIL,
password: process.env.SIMULATION_PASSWORD
};
async function adminLogin(email, password) {
const response = await axios.post(`${api}${adminLoginEndpoint}`, {
email: email,
password: password,
api_key: apiKey
});
return response.data.data.token;
}
async function rentScooter(scooter_id, user_id, token) {
try {
const response = await axios.post(`${api}${rentEndpoint}`, {
scooter_id: scooter_id,
user_id: user_id,
api_key: apiKey
}, {
headers: {
"content-type": "application/json",
"x-access-token": token
},
timeout: 5000
});
return response;
} catch (e) {
return false;
}
}
async function stopTrip(scooter_id, user_id, token) {
try {
const response = await axios.post(`${api}${stopEndpoint}`, {
scooter_id: scooter_id,
user_id: user_id,
api_key: apiKey
}, {
headers: {
"content-type": "application/json",
"x-access-token": token
},
timeout: 5000
});
return response;
} catch (e) {
return false;
}
}
async function rentWatch() {
const scooters = (await db.getScootersInUse(process.env.SIMULATION_CITY)).filter(scooter => {
// Filter out scooters that arent in use by simulation accounts
for (let i = 0; i < users.length; i++) {
if (scooter.trip.userId === users[i]._id.toString()) {
return true;
}
}
return false;
});
console.log("scooters in use:", scooters.length);
scooters.forEach(scooter => {
//Stop the trip if speed is 0 or if battery is entering the danger zone
if (scooter.trip && scooter.trip.userId && (scooter.speed === 0 || scooter.battery <= 10)) {
// Scooter has reached destination probably
console.log("Stopping trip for scooter:", scooter.name);
stopTrip(scooter._id.toString(), scooter.trip.userId, token);
}
});
if (scooters.length > 0) {
setTimeout(rentWatch, rentWatchPollRate);
} else {
console.log("Simulation complete, shutting down");
db.close();
process.exit(0);
}
}
async function main() {
db.setMongoURI(process.env.DBURI);
db.connect();
// filter out real users
users = await db.getAllFakeUsers();
const scooters = await db.getAllScooters();
console.log("Logging in as admin...");
token = await adminLogin(admin.email, admin.password);
console.log("Login successful");
// Rent as many scooters as possible
for (let i = 0; i < users.length && i < scooters.length; i++) {
const user = users[i];
const scooter = scooters[i];
if (scooter.status === "Available" && user.balance > minBalance && scooter.owner === process.env.SIMULATION_CITY) {
const response = await rentScooter(scooter._id.toString(), user._id.toString(), token);
}
}
setTimeout(rentWatch, rentWatchPollRate)
}
setTimeout(main, 5000);