Passed
Push — dev ( 936fc6...aba162 )
by Kasper
51s queued 16s
created

scooter_hive.js   A

Complexity

Total Complexity 20
Complexity/F 2.22

Size

Lines of Code 122
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 20
eloc 59
mnd 11
bc 11
fnc 9
dl 0
loc 122
rs 10
bpm 1.2222
cpm 2.2222
noi 6
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A ➔ main 0 7 1
A ➔ startUpdate 0 7 2
A ➔ removeScooter 0 13 4
A ➔ loadNewScooters 0 9 2
A ➔ startUpdateScooters 0 5 2
A ➔ dropCallback 0 18 2
B ➔ printScooters 0 16 6
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
const scooter = require("./scooter");
14
15
const numberOfScooters = process.env.NUMBER_OF_SCOOTERS;
16
let scooters = [];
17
18
/**
19
  * Loads x number of scooters and returns them
20
  * @return list of scooter
21
  */
22
async function loadNewScooters() {
23
    const scooterArray = [];
24
    for (let i = 0; i < numberOfScooters; i++) {
25
        const scooter = new Scooter();
26
        await scooter.load(); // Load up new scooter, can pass id if custom generation is wanted
27
        scooterArray.push(scooter);
28
    }
29
    return scooterArray;
30
}
31
32
/**
33
  * Starts update function on scooter
34
  * @param mixed scooter
35
  * 
36
  * @return void
37
  */
38
async function startUpdate(scooter) {
39
    try {
40
        scooter.update();
41
    } catch (e) {
42
        console.log(e);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
43
    }
44
}
45
46
/**
47
  * @param mixed scooterArray
48
  * 
49
  * @return [type]
50
  */
51
async function startUpdateScooters() {
52
    scooters.forEach(scooter => {
53
        startUpdate(scooter);
54
    });
55
}
56
57
/**
58
 * Prints useful information about the current state of the scooters
59
  * @param mixed scooters
60
  * @param bool repeat
61
  * 
62
  * @return [type]
63
  */
64
async function printScooters(repeat) {
65
    const totalScooters = scooters.length;
66
    const scootersStateCount = {};
67
    for (let i = 0; i < totalScooters; i++) {
68
        const scooter = scooters[i];
69
        // console.log(scooter);
70
        scootersStateCount[scooter.status] = scootersStateCount[scooter.status] ? scootersStateCount[scooter.status] + 1 : 1;
71
    }
72
    for (const state in scootersStateCount) {
73
        console.log(state, scootersStateCount[state]);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
74
    }
75
    console.log("============================");
76
    if (repeat) {
77
        setTimeout(() => printScooters(repeat), 5000);
78
    }
79
}
80
81
/**
82
 * @param string id
83
 * 
84
 * @return void
85
 */
86
function removeScooter(id) {
87
    let index = -1;
88
    for (let i = 0; i < scooters.length; i++) {
89
        if (id === scooters[i]._id.toString()) {
90
            index = i;
91
            break;
92
        }
93
    }
94
    if (index !== -1) {
95
        console.log(`${scooters[index].name}: removed from database`);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
96
        scooters.splice(index, 1);
97
    }
98
}
99
100
/**
101
  * @return [type]
102
  */
103
async function dropCallback() {
104
    console.log("Loading up new scooters:", numberOfScooters);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
105
    const existingScooters = await db.getAllScooters();
106
    const oldScooters = [];
107
    for (let i = 0; i < existingScooters.length; i++) {
108
        const scooter = new Scooter(errorCallback=removeScooter);
0 ignored issues
show
Bug introduced by
The variable errorCallback seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.errorCallback.
Loading history...
109
        await scooter.load(existingScooters[i]._id.toString());
110
        oldScooters.push(scooter);
111
    }
112
    scooters = await loadNewScooters();
113
    scooters = scooters.concat(oldScooters);
114
    console.log("Starting scooters")
115
    startUpdateScooters(scooters);
0 ignored issues
show
Bug introduced by
The call to startUpdateScooters seems to have too many arguments starting with scooters.
Loading history...
116
    printScooters(true);
117
    //TODO: Add watch on mongoDB database
118
    //IF NEW SCOOTER, ADD IT
119
    //IF SCOOTER IS REMOVED, REMOVES IT FROM ARRAY
120
}
121
122
async function main() {
123
    db.setMongoURI(process.env.DBURI);
124
    db.connect();
125
    // console.log("Dropping scooter collection...");
126
    // db.dropScooters(dropCallback);
127
    dropCallback();
128
}
129
130
if (require.main === module) {
131
    main();
132
}
133