/
dashorm.js
119 lines (119 loc) · 5.22 KB
/
dashorm.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
(function() {
var DashORM, HWState, MYSQL_DATABASE, MYSQL_HOST, MYSQL_PASSWORD, MYSQL_PORT, MYSQL_USERNAME, models, mysqlLib;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
MYSQL_HOST = 'localhost';
MYSQL_PORT = 3306;
MYSQL_USERNAME = 'homework';
MYSQL_PASSWORD = 'whatever';
MYSQL_DATABASE = 'bandwidth_data';
models = require('../public/scripts/models').models;
mysqlLib = require('mysql');
HWState = require('./hwstate').hwstate;
DashORM = (function() {
function DashORM() {
this.query = __bind(this.query, this); this.dashboardModel = new models.DashboardModel();
this.mysql = mysqlLib.createClient({
host: MYSQL_HOST,
port: MYSQL_PORT,
user: MYSQL_USERNAME,
password: MYSQL_PASSWORD
});
this.mysql.useDatabase(MYSQL_DATABASE);
this.state_collector = new HWState(__bind(function(state) {
return this.dashboardModel.monthlyallowances.add(state);
}, this));
}
DashORM.prototype.query = function(model, parameters, response) {
var current_date, date_string, month_id, month_model, month_str, month_totals, state_model, this_month;
if (response == null) {
response = 0;
}
switch (model) {
case "allowances":
month_str = parameters[0] + "-" + parameters[1];
month_id = parameters[0] + "/" + parameters[1];
date_string = month_str + "-01";
current_date = new Date();
this_month = current_date.getFullYear() === parseInt(parameters[0]) && (current_date.getMonth() + 1) === parseInt(parameters[1]);
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;
if (mysql_row.ip !== '0.0.0.0') {
device = month_model.devices.get(mysql_row.ip);
if ((device != null) && device.has("user")) {
user_total_usage = parseInt(mysql_row['SUM(bytes)']);
username = device.get("user");
if (!month_model.users.get(username)) {
month_model.updateUser(username, parseInt(mysql_row['SUM(bytes)']));
}
}
if (username != null) {
return month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)']), 0, void 0, username);
} else {
return month_model.updateDevice(mysql_row.ip, parseInt(mysql_row['SUM(bytes)']));
}
}
}, this));
if (this_month) {
this.dashboardModel.monthlyallowances.remove(month_str);
this.dashboardModel.monthlyallowances.add(month_model);
}
if (response) {
return response.json(month_model.xport());
} else {
return month_model.xport();
}
break;
case "usage":
return console.log("No usage ORM");
}
};
DashORM.prototype.liveUpdate = function(package) {
var current_month, item, item_date, item_str, _i, _len;
item_str = "";
for (_i = 0, _len = package.length; _i < _len; _i++) {
item = package[_i];
item_date = new Date(parseInt(item.timestamp) * 100);
item_str = item_date.getFullYear() + "-" + (item_date.getMonth() + 1);
current_month = this.dashboardModel.monthlyallowances.get(item_str);
if (current_month === void 0) {
current_month = this.dashboardModel.monthlyallowances.get("STATE").clone();
current_month.set({
id: item_str
});
}
current_month.updateHousehold(item.bytes);
current_month.updateDevice(item.ipaddr, item.bytes);
if (current_month.devices.get(item.ipaddr).has("user")) {
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);
}
}
return this.dashboardModel.monthlyallowances.get(item_str).xport();
};
return DashORM;
})();
exports.dashorm = DashORM;
}).call(this);