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}}