Passed
Push — master ( 4ca7e8...6fbad5 )
by Dave
01:19
created

src/api/src/server.js   A

Complexity

Total Complexity 42
Complexity/F 1.83

Size

Lines of Code 210
Function Count 23

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
cc 0
wmc 42
eloc 106
c 7
b 1
f 1
nc 2
mnd 1
bc 30
fnc 23
dl 0
loc 210
rs 9.0399
bpm 1.3043
cpm 1.826
noi 27

5 Functions

Rating   Name   Duplication   Size   Complexity  
A server.js ➔ bail 0 8 2
A server.js ➔ ??? 0 1 1
A server.js ➔ on_connect 0 7 2
A server.js ➔ onWebError 0 22 5
B sse.connection 0 110 2

How to fix   Complexity   

Complexity

Complex classes like src/api/src/server.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"use strict";
2
3
const express = require("express");
4
const serveStatic = require("serve-static");
5
const path = require("path");
0 ignored issues
show
Unused Code introduced by
The constant path seems to be never used. Consider removing it.
Loading history...
6
const SSE = require("sse");
7
const redis = require("redis");
0 ignored issues
show
Unused Code introduced by
The constant redis seems to be never used. Consider removing it.
Loading history...
8
const amqp = require("amqplib/callback_api");
9
10
const app = express();
11
12
// const passport = require('passport')    
13
// const BasicStrategy = require('passport-http').BasicStrategy
14
// passport.use(new BasicStrategy(
15
// 	function(username, password, done) {
16
// 		//todo: obviously needs to change
17
// 	  	if (username.valueOf() === "fullcycle" && password.valueOf() === "mining")
18
// 			return done(null, true);
19
// 		  else
20
// 		  {
21
// 			console.log("rejected!")
22
// 			return done(null, false);
23
// 		  }
24
// 	}
25
//   ));
26
27
const services = require("./services");
28
const messages = require("./messages");
0 ignored issues
show
Unused Code introduced by
The constant messages seems to be never used. Consider removing it.
Loading history...
29
30
var api = require("./api.js");
31
32
function bail(err, conn) {
33
  console.error("bailing...");
34
  console.error(err);
35
  if (conn) conn.close(function() {
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
36
		// if (doexit)
37
		// 	process.exit(1); 
38
	});
39
}
40
41
42
//route all other calls to the home page. this is causing "path is not defined" in line 179
43
// app.get("/*", function(req, res) {
44
//   res.sendFile(path.join(__dirname, "/index.html"), function(err) {
45
//     if (err) {
46
//       res.status(500).send(err)
47
//     }
48
//   })
49
// });
50
51
//in production this serves up the react bundle
52
app.use(serveStatic("../web/build")
53
//,
54
//	passport.authenticate("basic", { session: false })
55
);
56
app.use("/api", api);
57
var server = app.listen(services.web.port, () => console.log(`Listening on port ${services.web.port}`));
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...
58
function onWebError(error) {
59
	if (error.syscall !== "listen") {
60
	  throw error;
61
	}
62
  
63
	//var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
64
	var bind = services.web.port;
65
  
66
	// handle specific listen errors with friendly messages
67
	switch (error.code) {
68
		case "EACCES":
69
			console.error(error.code + ":" + bind + " requires elevated privileges");
70
			process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
71
			break;
72
		case "EADDRINUSE":
73
			console.error(error.code + ":" + bind + " is already in use. Close the other app and try again");
74
			process.exit(1);
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
75
			break;
76
		default:
77
			throw error;
78
	}
79
  }
80
  server.on("error", onWebError);
81
82
var busConnect = null;
83
84
function on_connect(err, conn) {
85
	if (err !== null) return bail(err);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
86
	process.once("SIGINT", function() { conn.close(); });
87
88
	busConnect = conn;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
89
90
}
91
92
//set up the full cycle alerts feed to send alerts to the browser
93
var sse = new SSE(server);
94
sse.on("connection", function (sseConnection) {
95
	//console.log("new sse connection");
96
	
97
	const qAlert = "alert";
98
	let alertChannel = null;
99
100
	function alertMessage(msg) {
101
		if (msg) {
102
			//msg.content.toString()
103
			//console.log(" [x] '%s'", "received alert message");
104
			sseConnection.send({
105
				event: "full-cycle-alert",
106
				data: msg.content.toString()
107
			});
108
		}
109
	}
110
111
	function on_channel_open_alert(err, ch) {
112
		if (err !== null) { return bail(err, busConnect); }
113
		alertChannel = ch;
114
		ch.on("error", function (err) {
0 ignored issues
show
Unused Code introduced by
The parameter err is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
115
			//console.error(err);
116
			//console.log("channel Closed");
117
		});
118
		ch.assertQueue("", {exclusive: true}, function(err, ok) {
119
			var q = ok.queue;
120
			ch.bindQueue(q, qAlert, "");
121
			ch.consume(q, alertMessage, {noAck: true}, function(err, ok) {
0 ignored issues
show
Unused Code introduced by
The parameter ok is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
122
				if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Complexity Best Practice introduced by
There is no return statement if err !== null is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
123
				//console.log(" [*] Waiting for alert. To exit press CTRL+C.");
124
			});
125
		});
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
126
	}
127
128
	const qMiner = "statisticsupdated";
129
	let miner_channel = null;
130
131
	function minerMessage(msg) {
132
		if (msg) {
133
			//msg.content.toString()
134
			//console.log(" [x] '%s'", "received miner message");
135
			sseConnection.send({
136
				event: "full-cycle-miner",
137
				data: msg.content.toString()
138
			});
139
		}
140
	}
141
142
	function on_channel_open_miner(err, ch) {
143
		if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
144
		miner_channel = ch;
145
		ch.on("error", function (err) {
146
			console.error(err);
147
			//console.log("miner channel Closed");
148
		});
149
		ch.assertQueue("", {exclusive: true}, function(err, ok) {
150
			var q = ok.queue;
151
			ch.bindQueue(q, qMiner, "");
152
			ch.consume(q, minerMessage, {noAck: true}, function(err, ok) {
0 ignored issues
show
Unused Code introduced by
The parameter ok is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
153
				if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Complexity Best Practice introduced by
There is no return statement if err !== null is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
154
					//console.log(" [*] Waiting for miner stats. To exit press CTRL+C.");
155
			});
156
		});
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
157
	}
158
159
	const qSensor = "sensor";
160
	let sensor_channel = null;
161
	function sensorMessage(msg) {
162
		if (msg) {
163
			//msg.content.toString()
164
			//console.log(" [x] '%s'", "received sensor message");
165
			sseConnection.send({
166
				event: "full-cycle-sensor",
167
				data: msg.content.toString()
168
			});
169
		}
170
	}
171
172
	function on_channel_open_sensor(err, ch) {
173
		if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
174
		sensor_channel = ch;
175
		ch.on("error", function (err) {
176
			console.error(err)
177
			//console.log("sensor channel Closed");
178
		});
179
		ch.assertQueue("", {exclusive: true}, function(err, ok) {
180
			var q = ok.queue;
181
			ch.bindQueue(q, qSensor, "");
182
			ch.consume(q, sensorMessage, {noAck: true}, function(err, ok) {
0 ignored issues
show
Unused Code introduced by
The parameter ok is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
183
				if (err !== null) return bail(err, busConnect);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Complexity Best Practice introduced by
There is no return statement if err !== null is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
184
					//console.log(" [*] Waiting for sensor. To exit press CTRL+C.");
185
			});
186
		});
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
187
	}
188
189
	if (busConnect)
190
	{
191
		busConnect.createChannel(on_channel_open_alert);
192
		busConnect.createChannel(on_channel_open_miner);
193
		busConnect.createChannel(on_channel_open_sensor);
194
	}
195
	
196
  sseConnection.on("close", function () {
197
    //console.log("lost sse connection");
198
		if (alertChannel) alertChannel.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
199
		if (miner_channel) miner_channel.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
200
		if (sensor_channel) sensor_channel.close();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
201
	});
202
	
203
});
204
205
try {
206
	amqp.connect(services.messagebus.connection, on_connect);
207
}
208
catch(error) {
209
	console.error(error);
210
}
211