Skip to content

Commit

Permalink
minor improvements and removed logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Hoenisch committed Jun 7, 2012
1 parent 1431b92 commit 02afcad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 59 deletions.
56 changes: 12 additions & 44 deletions server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ count = 6
data = []


######################### mysql part
######################### mysql start connection
sys = require('util')
mysql = require('mysql')
connection = mysql.createConnection {
Expand All @@ -33,67 +33,44 @@ connection = mysql.createConnection {
password : 'password',
database : 'pep'
}

connection.connect()

########test data#####

@inserts = (connection, pcId, date, cpu) ->
connection.query('insert into data (pc, dat, cpu ) values (?, ?, ?)',[pcId, date, cpu], (err, rows, fields) =>
throw err if (err)
console.log "inserted " + rows.insertId
)
#setInterval ( => @getLastDataWrapper('pc1')), 5000
setInterval ( => @inserts(connection, 'pc1', new Date(), Math.random())), 10000




#######method to retrieve last value
getLastData = (connection, pcId, callback) ->
console.log "querying for last insert"
connection.query('SELECT m.* from data m where m.pc=? and m.dat=(select max(dat) from data m2 where m2.pc=?)',[pcId,pcId], (err, rows, fields) =>
throw err if (err)
callback rows
)

#######method to retrieve all data per pc
getAllData = (connection, pcId, callback) ->
console.log "querying for all data"
connection.query('SELECT m.* from data m where m.pc=? order by dat',[pcId], (err, rows, fields) ->
connection.query('SELECT m.* from data m where m.pc=? order by dat asc',[pcId], (err, rows, fields) ->
throw err if (err)
callback rows
)

# uncomment if you want to see sum test output
#getLastData(connection, 'pc1')
#getLastData(connection, 'pc2')

#getAllData(connection, 'pc1')
#getAllData(connection, 'pc2')

@getAllDataWrapper = (connection, pcId) ->
getAllData connection, pcId, (result) ->
########wrapper methods for socket.io
@getAllDataWrapper = (connection, pcId) =>
getAllData connection, pcId, (result) =>
for item in result
io.sockets.emit 'chart', {chartData: item}
# console.log item
io?.sockets.emit 'chart', {chartData: item}
setInterval ( => @getLastDataWrapper(connection, 'pc1')), 10000

@getLastDataWrapper = (connection, pcId) ->
getLastData connection, pcId, (result) ->
for item in result
io.sockets.emit 'chart', {chartData: item}
# console.log item

#getAllData connection, 'pc1', (result) ->
# for item in result
# io.sockets.emit 'chart', {chartData: item}
# console.log item


#connection.end()
#@getLastDataWrapper('pc1')
#setInterval ( => @getLastDataWrapper(connection, 'pc1')), 5000
# setInterval ( => @getAllDataWrapper('pc1')), 5000
io?.sockets.emit 'chart', {chartData: item}

io.sockets.on 'connection', (socket) =>
############create a new mysql connection for each user
mys = mysql.createConnection {
host : 'localhost',
user : 'root',
Expand All @@ -102,19 +79,10 @@ io.sockets.on 'connection', (socket) =>
}

mys.connect()
setInterval ( => @getLastDataWrapper(mys, 'pc1')), 12000
@getAllDataWrapper(mys, 'pc1')
count++
# io.sockets.emit 'count', { date: new Date(), number: Math.random() }

# setInterval(() ->
# io.sockets.emit 'count', { date: new Date(), number: Math.random(), count: count++ , number2: Math.random()}
# , 5000)

socket.on 'disconnect', () ->
#mys.end()
count--
io.sockets.emit 'count', { number: count }

app.get '/', (req, res) ->
res.render 'index', {title: 'node.js express socket.io realtime charts'}
Expand Down
44 changes: 29 additions & 15 deletions views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ html
script(type='text/javascript')
var socket = io.connect();
var chart = null;
var actualDate = new Date();
function addChartData(chartData) {
if(chart != null && chartData.pc != null) {
var index = serieIndex(chartData.pc);
Expand All @@ -25,9 +26,25 @@ html
} else {
series = addNewSerie(chartData.pc);
}
var x = new Date(chartData.dat).getTime();
var y = chartData.cpu * 100;
series.addPoint([x, y], true, false);
// check if new entry is realy newer than last entry
var isFuture = true;
var length = series.data.length;
if(length == 1) {
// remove testdata
if(new Date(series.data[0].x).getTime()==actualDate.getTime()) {
series.data[0].remove();
}
length = series.data.length;
}
if(length > 0) {
isFuture = new Date(chartData.dat).getTime() > new Date(series.data[length-1].x).getTime();
}
if(isFuture) {
var x = new Date(chartData.dat).getTime();
var y = chartData.cpu * 100;
series.addPoint([x, y], true, false);
}

}
};

Expand Down Expand Up @@ -95,11 +112,9 @@ html
name: 'pc1',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
var data = [];
data.push({
x: time,
x: actualDate,
y: null
});
return data;
Expand All @@ -117,13 +132,10 @@ html
function addNewSerie(newName) {
return chart.addSeries({
name: newName,
data: (function() {

var data = [],
time = (new Date()).getTime(),
i;
data: (function() {
var data = [];
data.push({
x: time,
x: actualDate.getTime(),
y: null
});
return data;
Expand All @@ -132,8 +144,10 @@ html
});
chart.redraw();
}
socket.on('chart', function (chartdata) {
addChartData(chartdata.chartData);
socket.on('chart', function (data) {
if(!$.isArray(data.chartData)) {
addChartData(data.chartData);
}
});


0 comments on commit 02afcad

Please sign in to comment.