-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
65 lines (56 loc) · 1.89 KB
/
models.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
var Sequelize = require("sequelize"),
config = require("./config.json");
var dbOptions = {};
dbOptions.name = config.db.name;
dbOptions.user = config.db.user;
dbOptions.pass = config.db.password;
dbOptions.host = config.db.host;
dbOptions.port = config.db.port;
dbOptions.dialect = config.db.dialect;
dbOptions.protocol = config.db.protocol;
dbOptions.storage = config.db.storage;
var sequelize = new Sequelize(dbOptions.name, dbOptions.user, dbOptions.pass, {
host: dbOptions.host,
port: dbOptions.port,
dialect: dbOptions.dialect,
protocol: dbOptions.protocol,
storage: dbOptions.storage
});
exports.Digit = sequelize.define("PiDigit", {
digitIndex: Sequelize.INTEGER,
digitValue: Sequelize.STRING,
userIP: Sequelize.STRING
});
exports.Digit.sync();
exports.getUserAndDigitAmount = function(callback) {
exports.Digit.all().success(function(digits) {
var ips = [];
var digitAmount = 0;
digits.forEach(function(digit) {
if(digit.digitValue != null && digit.digitValue.length > 0)
digitAmount++;
if(!(ips.indexOf(digit.userIP) > -1))
if(digit.userIP != "")
ips.push(digit.userIP);
});
callback({peopleAmount: ips.length - 1, digitAmount: digitAmount});
});
}
exports.getDayGraphData = function(callback) {
var date = new Date(new Date().getTime() - 604800000);
exports.Digit.findAll({where: ["updatedAt >= ?", date], order: "updatedAt ASC"}).success(function(digits) {
var data = {};
digits.forEach(function(digit) {
if(digit.digitValue == null) return;
var digitDate = digit.updatedAt;
digitDate.setHours(0);
digitDate.setMinutes(0);
digitDate.setSeconds(0);
digitDate.setMilliseconds(0);
var digitTime = digitDate.getTime();
if(data[digitTime] == null) data[digitTime] = 0;
data[digitTime] = data[digitTime] + 1;
});
callback(data);
});
}