diff --git a/aggregate_dummy.sql b/aggregate_dummy.sql index 852953e..a595ba1 100644 --- a/aggregate_dummy.sql +++ b/aggregate_dummy.sql @@ -1,4 +1,4 @@ USE bandwidth_data; DELETE FROM bandwidth_hours WHERE 1; -INSERT INTO bandwidth_hours (date, hour, ip, bytes) VALUES ( DATE( DATE_SUB( NOW(), INTERVAL 3 HOUR)), HOUR(SUBTIME(NOW(), '0 3:0:0')), "1.1.1.4", 256000 ); -INSERT INTO bandwidth_hours (date, hour, ip, bytes) VALUES ( DATE( DATE_SUB( NOW(), INTERVAL 3 HOUR)), HOUR(SUBTIME(NOW(), '0 3:0:0')), "1.1.1.7", 256000 ); +INSERT INTO bandwidth_hours (date, hour, ip, bytes) VALUES ( DATE( DATE_SUB( NOW(), INTERVAL 1 HOUR)), HOUR(SUBTIME(NOW(), '0 1:0:0')), "1.1.1.4", 256000 ); +INSERT INTO bandwidth_hours (date, hour, ip, bytes) VALUES ( DATE( DATE_SUB( NOW(), INTERVAL 1 HOUR)), HOUR(SUBTIME(NOW(), '0 1:0:0')), "1.1.1.7", 256000 ); diff --git a/lib/dashorm.js b/lib/dashorm.js index 5b53e9b..3a0ccda 100644 --- a/lib/dashorm.js +++ b/lib/dashorm.js @@ -24,7 +24,7 @@ }, this)); } DashORM.prototype.query = function(model, parameters, response) { - var current_date, date_string, month_id, month_model, month_str, month_totals, this_month; + var current_date, date_string, month_id, month_model, month_str, month_totals, state_model, this_month; if (response == null) { response = 0; } @@ -38,18 +38,26 @@ if (this_month && this.dashboardModel.monthlyallowances.get(month_str) !== void 0) { if (response) { response.json(this.dashboardModel.monthlyallowances.get(month_str).xport()); + return; } else { return this.dashboardModel.monthlyallowances.get(month_str).xport(); } } + state_model = this.dashboardModel.monthlyallowances.get("STATE"); month_model = this.dashboardModel.monthlyallowances.get("STATE").clone(); + month_model.household = state_model.household.clone(); + state_model.devices.each(__bind(function(device) { + return month_model.devices.add(device.clone()); + }, this)); + state_model.users.each(__bind(function(user) { + return month_model.users.add(user.clone()); + }, this)); month_model.set({ id: month_str }); month_totals = this.mysql.query("SELECT ip, SUM(bytes) FROM bandwidth_hours WHERE date " + "BETWEEN ? AND DATE_ADD(?, INTERVAL 1 MONTH) GROUP BY ip", [date_string, date_string]); month_totals.on('row', __bind(function(mysql_row) { var device, user_total_usage, username; - console.log(mysql_row); if (mysql_row.ip !== '0.0.0.0') { device = month_model.devices.get(mysql_row.ip); if ((device != null) && device.has("user")) { @@ -60,13 +68,10 @@ } } if (username != null) { - month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), device.get("allowances"), username); - } else if (device != null) { - month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), device.get("allowances")); + return month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), 0, void 0, username); } else { - month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)'])); + return month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)'])); } - return month_model.updateHousehold(mysql_row['SUM(bytes)']); } }, this)); if (this_month) { @@ -86,7 +91,6 @@ DashORM.prototype.liveUpdate = function(package) { var current_month, item, item_date, item_str, _i, _len; item_str = ""; - console.log(this.dashboardModel.monthlyallowances.pluck("id")); for (_i = 0, _len = package.length; _i < _len; _i++) { item = package[_i]; item_date = new Date(parseInt(item.timestamp) * 100); @@ -99,16 +103,14 @@ }); } current_month.updateHousehold(item.bytes); - console.log(item); current_month.updateDevice(item.ipaddr, item.bytes); if (current_month.devices.get(item.ipaddr).has("user")) { - current_month.updateUser(device_state.get("user"), item.bytes); + current_month.updateUser(current_month.devices.get(item.ipaddr).get("user"), item.bytes); } if (!this.dashboardModel.monthlyallowances.get(item_str)) { this.dashboardModel.monthlyallowances.add(current_month); } } - console.log(item_str); return this.dashboardModel.monthlyallowances.get(item_str).xport(); }; return DashORM; diff --git a/lib/hwstate.js b/lib/hwstate.js index 34c0f3d..ede6962 100644 --- a/lib/hwstate.js +++ b/lib/hwstate.js @@ -24,26 +24,20 @@ return this.updateState(callback); }, this)); } - HWState.prototype.getState = function() { - return this.state; - }; HWState.prototype.updateState = function(callback) { return this.updateDeviceNames(__bind(function() { console.log("devices"); - setTimeout(__bind(function() { + return setTimeout(__bind(function() { return this.updateUsers(__bind(function() { console.log("users"); - console.log("TT " + this.state.devices.get("1.1.1.7").has("user")); return setTimeout(__bind(function() { return this.updateAllowances(__bind(function() { console.log("allowances"); - console.log(this.state.devices.get("1.1.1.7").has("user")); - return this.emit('update', this.state); + return callback(this.state); }, this)); - }, this), 2000); + }, this), 1000); }, this)); - }, this), 2000); - return callback(this.state); + }, this), 1000); }, this)); }; HWState.prototype.updateDeviceNames = function(callback) { @@ -53,17 +47,7 @@ _ref = device_rows.slice(1); for (_i = 0, _len = _ref.length; _i < _len; _i++) { device_row = _ref[_i]; - console.log(device_row); - if (this.state.devices[device_row.ip] != null) { - this.state.devices[device_row.ip].set({ - name: device_row.name - }); - } else { - this.state.devices.add(new models.Allowance({ - id: device_row.ip, - name: device_row.name - })); - } + this.state.updateDevice(device_row.ip, 0, 0, device_row.name); } } if (callback) { @@ -81,19 +65,7 @@ _ref = user_rows.slice(1); for (_i = 0, _len = _ref.length; _i < _len; _i++) { user_row = _ref[_i]; - if (this.state.devices.get(user_row.ip) != null) { - console.log("Device exists"); - this.state.devices.get(user_row.ip).set({ - user: user_row.name - }); - console.log(user_row.ip, this.state.devices.get(user_row.ip).has("user")); - } else { - console.log("Device does not exist, adding"); - this.state.devices.add(new models.Allowance({ - id: user_row.ip, - user: user_row.name - })); - } + this.state.updateDevice(user_row.ip, 0, 0, void 0, user_row.name); } } if (callback) { @@ -111,18 +83,10 @@ _ref = allowance_rows.slice(1); for (_i = 0, _len = _ref.length; _i < _len; _i++) { allowance_row = _ref[_i]; - console.log("Allowance IP: " + allowance_row.ip); - if (this.state.devices.get(allowance_row.ip) != null) { - console.log("Setting"); - this.state.devices.get(allowance_row.ip).set({ - allowance: parseInt(allowance_row.allowance) - }); + if (allowance_row.ip === "HOME") { + this.state.updateHousehold(0, allowance_row.allowance); } else { - console.log("Adding"); - this.state.devices.add(new models.Allowance({ - id: allowance_row.ip, - allowance: allowance_row.allowance - })); + this.state.updateDevice(allowance_row.ip, 0, allowance_row.allowance); } } } diff --git a/public/scripts/models.js b/public/scripts/models.js index ad8ddea..d71cad9 100644 --- a/public/scripts/models.js +++ b/public/scripts/models.js @@ -43,7 +43,7 @@ return this.devices = new models.Allowances(); }, updateHousehold: function(usage, allowance) { - var new_usage; + var current_usage, household_data, total_usage; if (usage == null) { usage = 0; } @@ -52,48 +52,56 @@ } usage = parseInt(usage); allowance = parseInt(allowance); - new_usage = parseInt(this.household.get("usage") || 0) + usage; - return this.household.set({ - usage: new_usage, - allowance: allowance - }); + current_usage = parseInt(this.household.get("usage")); + total_usage = current_usage + usage; + household_data = { + usage: total_usage + }; + if (allowance > 0) { + household_data['allowance'] = allowance; + } + return this.household.set(household_data); }, - updateDevice: function(ip, usage, allowance, user) { - var device_data, new_usage; + updateDevice: function(ip, usage, allowance, name, user) { + var device_data, device_exists, new_usage; if (usage == null) { usage = 0; } if (allowance == null) { allowance = 0; } + if (name == null) { + name = void 0; + } if (user == null) { user = void 0; } usage = parseInt(usage); allowance = parseInt(allowance); - console.log("Adding device " + ip + ", usage: " + usage + ", allowance: " + allowance + ", user: " + user); - new_usage = (parseInt(this.devices.get(ip) ? this.devices.get(ip).get("usage") : false) || 0) + usage; + device_exists = this.devices.get(ip) != null; + new_usage = parseInt((device_exists ? this.devices.get(ip).get("usage") : false) || 0) + usage; device_data = { id: ip, - usage: new_usage, - allowance: allowance + usage: new_usage }; + if (allowance > 0) { + device_data['allowance'] = allowance; + } + if (name != null) { + device_data['name'] = name; + } if (user != null) { device_data['user'] = user; + } else { + user = device_exists ? this.devices.get(ip).get("user") || void 0 : void 0; } if (this.devices.get(ip) != null) { - console.log("Device exists, setting"); this.devices.get(ip).set(device_data); - if (user != null) { - this.updateUser(user, usage); - } } else { - console.log("Device doesn't exist, creating"); this.devices.add(new models.Allowance(device_data)); - console.log(this.devices.get(ip)); - if (user != null) { - this.updateUser(user, usage, allowance); - } + } + if (user != null) { + this.updateUser(user, usage, allowance); } return this.updateHousehold(usage); }, @@ -107,19 +115,18 @@ } usage = parseInt(usage); allowance = parseInt(allowance); - console.log("Adding user " + name + ", usage: " + usage + ", allowance: " + allowance); - new_usage = parseInt(this.users.get(name).get("usage") || 0) + usage; - new_allowance = parseInt(this.users.get(name).get("allowance") || 0) + allowance; + new_usage = parseInt((this.users.get(name) ? this.users.get(name).get("usage") : false) || 0) + usage; + new_allowance = parseInt((this.users.get(name) ? this.users.get(name).get("allowance") : false) || 0) + allowance; user_data = { id: name, - usage: new_usage, - allowance: new_allowance + usage: new_usage }; + if (allowance > 0) { + user_data['allowance'] = new_allowance; + } if (this.users.get(name) != null) { - console.log("User exists, setting"); return this.users.get(name).set(user_data); } else { - console.log("User doesn't exist, creating"); return this.users.add(new models.Allowance(user_data)); } } diff --git a/public/scripts/views.js b/public/scripts/views.js index 77c93c1..1047d76 100644 --- a/public/scripts/views.js +++ b/public/scripts/views.js @@ -110,4 +110,11 @@ months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; return months[month_no - 1]; }); + HBS.registerHelper("deviceIdentifier", function(device) { + if (device.name != null) { + return device.name; + } else { + return device.id; + } + }); }).call(this); diff --git a/public/views/allowances.ejs b/public/views/allowances.ejs index 25733a3..e40bea6 100644 --- a/public/views/allowances.ejs +++ b/public/views/allowances.ejs @@ -23,7 +23,9 @@ {{#each devices}}
-
{{this.id}}
+
+ {{deviceIdentifier this}} +
{{/each}} diff --git a/public_src/models.coffee b/public_src/models.coffee index 56b6793..6d8a93d 100644 --- a/public_src/models.coffee +++ b/public_src/models.coffee @@ -51,40 +51,50 @@ models.MonthlyAllowance = BB.Model.extend({ usage = parseInt(usage) allowance = parseInt(allowance) - new_usage = parseInt(@household.get("usage") || 0) + usage + current_usage = parseInt(@household.get("usage")) + total_usage = current_usage + usage - @household.set { usage: new_usage, allowance: allowance } + household_data = { usage: total_usage } + + if allowance > 0 + household_data['allowance'] = allowance + + @household.set household_data # (ip, usage) -> Increment usage # (ip, usage || 0, allowance) -> Set allowance - # (ip, usage || 0, allowance || 0, user) -> Set name of user - updateDevice: (ip, usage=0, allowance=0, user=undefined) -> + # (ip, usage || 0, allowance || 0, name) -> Set name of user + # (ip, usage || 0, allowance || 0, name, user) -> Set name of user + updateDevice: (ip, usage=0, allowance=0, name=undefined, user=undefined) -> usage = parseInt(usage) allowance = parseInt(allowance) - console.log "Adding device " + ip + ", usage: " + usage + ", allowance: " + allowance + ", user: " + user + device_exists = @devices.get(ip)? - new_usage = ( parseInt(if @devices.get(ip) then @devices.get(ip).get("usage") else false) || 0) + usage + new_usage = parseInt( (if device_exists then @devices.get(ip).get("usage") else false) || 0) + usage device_data = { id: ip usage: new_usage - allowance: allowance } + if allowance > 0 + device_data['allowance'] = allowance + + if name? + device_data['name'] = name if user? device_data['user'] = user + else + user = if device_exists then (@devices.get(ip).get("user") || undefined) if @devices.get(ip)? - console.log "Device exists, setting" @devices.get(ip).set device_data - if user? then @updateUser(user, usage) else - console.log "Device doesn't exist, creating" @devices.add( new models.Allowance device_data ) - console.log @devices.get ip - if user? then @updateUser(user, usage, allowance) + + if user? then @updateUser(user, usage, allowance) @updateHousehold usage @@ -100,22 +110,20 @@ models.MonthlyAllowance = BB.Model.extend({ usage = parseInt(usage) allowance = parseInt(allowance) - console.log "Adding user " + name + ", usage: " + usage + ", allowance: " + allowance - - new_usage = parseInt(@users.get(name).get("usage") || 0) + usage - new_allowance = parseInt(@users.get(name).get("allowance") || 0) + allowance + new_usage = parseInt( (if @users.get(name) then @users.get(name).get("usage") else false) || 0) + usage + new_allowance = parseInt( (if @users.get(name) then @users.get(name).get("allowance") else false) || 0) + allowance user_data = { id: name usage: new_usage - allowance: new_allowance - } + } + + if allowance > 0 + user_data['allowance'] = new_allowance if @users.get(name)? - console.log "User exists, setting" @users.get(name).set user_data else - console.log "User doesn't exist, creating" @users.add( new models.Allowance user_data ) }) diff --git a/public_src/views.coffee b/public_src/views.coffee index a8b2d72..8c9fa83 100644 --- a/public_src/views.coffee +++ b/public_src/views.coffee @@ -97,3 +97,11 @@ HBS.registerHelper("getMonth", (month_no) -> "November", "December"] return months[month_no-1] ) + +HBS.registerHelper("deviceIdentifier", (device) -> + + if device.name? + return device.name + else + return device.id +) diff --git a/src/dashorm.coffee b/src/dashorm.coffee index 27a36b3..b312653 100644 --- a/src/dashorm.coffee +++ b/src/dashorm.coffee @@ -25,7 +25,9 @@ class DashORM @mysql.useDatabase MYSQL_DATABASE @state_collector = new HWState( (state) => + @dashboardModel.monthlyallowances.add(state) + ) query: (model, parameters, response=0) => @@ -48,11 +50,23 @@ class DashORM if response response.json @dashboardModel.monthlyallowances.get(month_str).xport() + return else return @dashboardModel.monthlyallowances.get(month_str).xport() + state_model = @dashboardModel.monthlyallowances.get("STATE") month_model = @dashboardModel.monthlyallowances.get("STATE").clone() + month_model.household = state_model.household.clone() + + state_model.devices.each( (device) => + month_model.devices.add(device.clone()) + ) + + state_model.users.each( (user) => + month_model.users.add(user.clone()) + ) + month_model.set( { id: month_str } ) month_totals = @mysql.query( @@ -63,8 +77,6 @@ class DashORM month_totals.on 'row', (mysql_row) => - console.log mysql_row - if mysql_row.ip isnt '0.0.0.0' device = month_model.devices.get mysql_row.ip @@ -78,14 +90,10 @@ class DashORM month_model.updateUser username, parseInt(mysql_row['SUM(bytes)']) if username? - month_model.updateDevice mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), device.get("allowances"), username - else if device? - month_model.updateDevice mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), device.get("allowances") + month_model.updateDevice mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), 0, undefined, username else month_model.updateDevice mysql_row.ip, parseInt(mysql_row['SUM(bytes)']) - month_model.updateHousehold mysql_row['SUM(bytes)'] - if this_month @dashboardModel.monthlyallowances.remove(month_str) @dashboardModel.monthlyallowances.add(month_model) @@ -100,8 +108,6 @@ class DashORM item_str = "" - console.log @dashboardModel.monthlyallowances.pluck("id") - for item in package item_date = new Date( parseInt(item.timestamp) * 100 ) @@ -115,18 +121,14 @@ class DashORM current_month.updateHousehold item.bytes - console.log item - current_month.updateDevice item.ipaddr, item.bytes if current_month.devices.get(item.ipaddr).has("user") - current_month.updateUser device_state.get("user"), item.bytes + current_month.updateUser current_month.devices.get(item.ipaddr).get("user"), item.bytes if not @dashboardModel.monthlyallowances.get(item_str) @dashboardModel.monthlyallowances.add(current_month) - console.log item_str - return @dashboardModel.monthlyallowances.get(item_str).xport() exports.dashorm = DashORM diff --git a/src/hwstate.coffee b/src/hwstate.coffee index ef5bdaa..adc14df 100644 --- a/src/hwstate.coffee +++ b/src/hwstate.coffee @@ -20,9 +20,6 @@ class HWState extends EventEmitter @hwdb.once 'connected', => @updateState(callback) - getState: -> - @state - updateState: (callback) -> @updateDeviceNames( => @@ -30,17 +27,14 @@ class HWState extends EventEmitter setTimeout( => @updateUsers( => console.log "users" - console.log "TT " + @state.devices.get("1.1.1.7").has("user") setTimeout( => @updateAllowances( => console.log "allowances" - console.log @state.devices.get("1.1.1.7").has("user") - @emit 'update', @state + callback(@state) ) - ,2000) + ,1000) ) - ,2000) - callback(@state) + ,1000) ) updateDeviceNames: (callback) -> @@ -49,19 +43,8 @@ class HWState extends EventEmitter if device_rows[0].status is "Success" and device_rows[0].rows > 0 for device_row in device_rows.slice(1) - console.log device_row - if @state.devices[device_row.ip]? - @state.devices[device_row.ip].set({ name: device_row.name }) - else - @state.devices.add( - new models.Allowance( - { - id: device_row.ip - name: device_row.name - } - ) - ) + @state.updateDevice device_row.ip, 0, 0, device_row.name if callback then callback() else @emit 'state', @state @@ -74,20 +57,7 @@ class HWState extends EventEmitter if user_rows[0].status is "Success" and user_rows[0].rows > 0 for user_row in user_rows.slice(1) - if @state.devices.get(user_row.ip)? - console.log "Device exists" - @state.devices.get(user_row.ip).set({ user: user_row.name }) - console.log user_row.ip, @state.devices.get(user_row.ip).has("user") - else - console.log "Device does not exist, adding" - @state.devices.add( - new models.Allowance( - { - id: user_row.ip - user: user_row.name - } - ) - ) + @state.updateDevice user_row.ip, 0, 0, undefined, user_row.name if callback then callback() else @emit 'state', @state @@ -100,21 +70,10 @@ class HWState extends EventEmitter if allowance_rows[0].status is "Success" and allowance_rows[0].rows > 0 for allowance_row in allowance_rows.slice(1) - console.log "Allowance IP: " + allowance_row.ip - - if @state.devices.get(allowance_row.ip)? - console.log "Setting" - @state.devices.get(allowance_row.ip).set({ allowance: parseInt(allowance_row.allowance) }) + if allowance_row.ip is "HOME" + @state.updateHousehold 0, allowance_row.allowance else - console.log "Adding" - @state.devices.add( - new models.Allowance( - { - id: allowance_row.ip - allowance: allowance_row.allowance - } - ) - ) + @state.updateDevice allowance_row.ip, 0, allowance_row.allowance if callback then callback() else @emit 'state', @state