1 | /** |
||
2 | * A node application to start a X amount of scooters. |
||
3 | * Used to simulate a system of X scooters. |
||
4 | * scooter.js will load .env file |
||
5 | * This will later be removed, since docker will handle loading environment variables |
||
6 | * Environment variables: |
||
7 | * DBURI |
||
8 | * NUMBER_OF_SCOOTERS |
||
9 | */ |
||
10 | |||
11 | const { Scooter } = require("./scooter"); |
||
12 | const db = require("./modules/sparkdb"); |
||
13 | |||
14 | const numberOfScooters = process.env.NUMBER_OF_SCOOTERS; |
||
15 | let scooters = []; |
||
16 | |||
17 | /** |
||
18 | * Loads x number of scooters and returns them |
||
19 | * @return list of scooter |
||
20 | */ |
||
21 | async function loadNewScooters() { |
||
22 | const scooterArray = []; |
||
23 | for (let i = 0; i < numberOfScooters; i++) { |
||
24 | const scooter = new Scooter(removeScooter); |
||
25 | await scooter.load(); // Load up new scooter, can pass id if custom generation is wanted |
||
26 | scooterArray.push(scooter); |
||
27 | } |
||
28 | return scooterArray; |
||
29 | } |
||
30 | |||
31 | |||
32 | /** |
||
33 | * @param mixed scooterArray |
||
34 | * |
||
35 | * @return [type] |
||
36 | */ |
||
37 | async function startUpdateScooters() { |
||
38 | scooters.forEach(scooter => { |
||
39 | scooter.update(); |
||
40 | }); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Prints useful information about the current state of the scooters |
||
45 | * @param mixed scooters |
||
46 | * @param bool repeat |
||
47 | * |
||
48 | * @return [type] |
||
49 | */ |
||
50 | async function printScooters(repeat) { |
||
51 | const totalScooters = scooters.length; |
||
52 | const scootersStateCount = {}; |
||
53 | for (let i = 0; i < totalScooters; i++) { |
||
54 | const scooter = scooters[i]; |
||
55 | // console.log(scooter); |
||
56 | scootersStateCount[scooter.status] = scootersStateCount[scooter.status] ? scootersStateCount[scooter.status] + 1 : 1; |
||
57 | } |
||
58 | for (const state in scootersStateCount) { |
||
59 | console.log(state, scootersStateCount[state]); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
60 | } |
||
61 | console.log("============================"); |
||
62 | if (repeat) { |
||
63 | setTimeout(() => printScooters(repeat), 5000); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string id |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | function removeScooter(id) { |
||
73 | let index = -1; |
||
74 | for (let i = 0; i < scooters.length; i++) { |
||
75 | if (id === scooters[i]._id.toString()) { |
||
76 | index = i; |
||
77 | break; |
||
78 | } |
||
79 | } |
||
80 | if (index !== -1) { |
||
81 | console.log(`${scooters[index].name}: removed from database`); |
||
0 ignored issues
–
show
|
|||
82 | scooters.splice(index, 1); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string id |
||
88 | * |
||
89 | * @return [type] |
||
90 | */ |
||
91 | async function newScooterCallback(id) { |
||
92 | const scooter = new Scooter(removeScooter); |
||
93 | await scooter.load(id); |
||
94 | scooter.update(); |
||
95 | scooters.push(scooter); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return void |
||
100 | */ |
||
101 | async function watchForInsert() { |
||
102 | const dbScooters = await db.getAllScooters(); |
||
103 | const newScooters = dbScooters.filter(newScooter => { |
||
104 | for (let i = 0; i < scooters.length; i++) { |
||
105 | const oldScooter = scooters[i]; |
||
106 | if (oldScooter._id.toString() === newScooter._id.toString()) { |
||
107 | return false; |
||
108 | } |
||
109 | } |
||
110 | return true; |
||
111 | }) |
||
112 | for (let i = 0; i < newScooters.length; i++) { |
||
113 | const scooter = new Scooter(removeScooter); |
||
114 | await scooter.load(newScooters[i]._id.toString()); |
||
115 | console.log("New scooter added:", scooter._id.toString()); |
||
0 ignored issues
–
show
|
|||
116 | scooter.update(); |
||
117 | scooters.push(scooter); |
||
118 | } |
||
119 | setTimeout(watchForInsert, 20000); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return [type] |
||
124 | */ |
||
125 | async function dropCallback() { |
||
126 | console.log("Loading up new scooters:", numberOfScooters); |
||
0 ignored issues
–
show
|
|||
127 | const existingScooters = await db.getAllScooters(); |
||
128 | const oldScooters = []; |
||
129 | for (let i = 0; i < existingScooters.length; i++) { |
||
130 | const scooter = new Scooter(removeScooter); |
||
131 | await scooter.load(existingScooters[i]._id.toString()); |
||
132 | oldScooters.push(scooter); |
||
133 | } |
||
134 | scooters = await loadNewScooters(); |
||
135 | scooters = scooters.concat(oldScooters); |
||
136 | console.log("Starting scooters") |
||
137 | startUpdateScooters(); |
||
138 | watchForInsert(); |
||
139 | printScooters(true); |
||
140 | } |
||
141 | |||
142 | async function main() { |
||
143 | db.setMongoURI(process.env.DBURI); |
||
144 | db.connect(); |
||
145 | dropCallback(); |
||
146 | } |
||
147 | |||
148 | if (require.main === module) { |
||
149 | main(); |
||
150 | } |
||
151 |