-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
143 lines (121 loc) · 3.76 KB
/
server.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Generated by CoffeeScript 1.4.0
(function() {
var app, connection, count, data, express, getAllData, getLastData, http_server, io, mysql, sys,
_this = this;
express = require('express');
this.io = require('socket.io');
app = module.exports = express();
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({
src: __dirname + '/public'
}));
app.use(app.router);
return app.use(express["static"](__dirname + '/public'));
});
app.configure('development', function() {
return app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function() {
return app.use(express.errorHandler());
});
count = 6;
data = [];
sys = require('util');
mysql = require('mysql');
connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'realtime'
});
connection.connect();
this.inserts = function(connection, pcId, date, cpu) {
var _this = this;
return connection.query('insert into data (pc, dat, cpu ) values (?, ?, ?)', [pcId, date, cpu], function(err, rows, fields) {
if (err) {
throw err;
}
return console.log("inserted " + rows.insertId);
});
};
setInterval((function() {
return _this.inserts(connection, 'pc1', new Date(), Math.random());
}), 10000);
getLastData = function(connection, pcId, callback) {
var _this = this;
console.log("querying for last insert");
return connection.query('SELECT m.* from data m where m.pc=? and m.dat=(select max(dat) from data m2 where m2.pc=?)', [pcId, pcId], function(err, rows, fields) {
if (err) {
throw err;
}
return callback(rows);
});
};
getAllData = function(connection, pcId, callback) {
console.log("querying for all data");
return connection.query('SELECT m.* from data m where m.pc=? order by dat asc', [pcId], function(err, rows, fields) {
if (err) {
throw err;
}
return callback(rows);
});
};
this.getAllDataWrapper = function(connection, pcId) {
return getAllData(connection, pcId, function(result) {
var item, _i, _len;
for (_i = 0, _len = result.length; _i < _len; _i++) {
item = result[_i];
if (typeof io !== "undefined" && io !== null) {
io.sockets.emit('chart', {
chartData: item
});
}
}
return setInterval((function() {
return _this.getLastDataWrapper(connection, 'pc1');
}), 10000);
});
};
this.getLastDataWrapper = function(connection, pcId) {
return getLastData(connection, pcId, function(result) {
var item, _i, _len, _results;
_results = [];
for (_i = 0, _len = result.length; _i < _len; _i++) {
item = result[_i];
_results.push(typeof io !== "undefined" && io !== null ? io.sockets.emit('chart', {
chartData: item
}) : void 0);
}
return _results;
});
};
if (!module.parent) {
http_server = app.listen(10927);
io = this.io.listen(http_server);
console.log("Express server listening on port %d", 10927);
}
io.sockets.on('connection', function(socket) {
var mys;
mys = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'realtime'
});
mys.connect();
_this.getAllDataWrapper(mys, 'pc1');
return socket.on('disconnect', function() {});
});
app.get('/', function(req, res) {
return res.render('index', {
title: 'node.js express socket.io realtime charts'
});
});
}).call(this);