-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stat.js
104 lines (86 loc) · 2.73 KB
/
Stat.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
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}
var schema = new Schema({
date : {type : Date,default: Date.now,index : true},
lDate : {type : Date,default: Date.now,index : true},
dt : {type : Number,default: 10},
mountain : String,
points : {type : Number, default : 0},
pointsDx : {type : Number, default : 0},
pointsPm : {type : Number, default : 0},
pins : {type : Number, default : 0},
pinsDx : {type : Number, default : 0},
pinsPm : {type : Number, default : 0},
lifts : {type : Number, default : 0},
liftsDx : {type : Number, default : 0},
liftsPm : {type : Number, default : 0},
photos : {type : Number, default : 0},
photosDx : {type : Number, default : 0},
photosPm : {type : Number, default : 0},
DoW : Number,
DoM : Number,
HoD : {type : Number,index : true},
DoY : Number,
lDoW : Number,
lDoM : Number,
lHoD : Number,
lDoY : Number
});
schema.statics.StatNames = [
'points',
'pins',
'lifts',
'photos',
];
schema.statics.Mountains = ['MtBrighton',
'Vail',
'BeaverCreek',
'Breck',
'Keystone',
'Canyons',
'Heavenly',
'Northstar',
'Kirkwood',
'AftonAlps'];
schema.statics.UtcOffsets = {'MtBrighton' : -5,
'Vail' : -7,
'BeaverCreek' : -7,
'Breck' : -7,
'Keystone' : -7,
'Canyons' : -7,
'Heavenly' : -8,
'Northstar' : -8,
'Kirkwood' : -7,
'AftonAlps' : -7};
var Stat = mongoose.model('EpicStat',schema);
schema.pre('save', function (next) {
this.lDate = new Date((this.date.getTime() + ((Stat.UtcOffsets[this.mountain])*60*60*1000)));
this.lDoW = this.lDate.getDay();
this.lDoM = this.lDate.getDate();
this.lHoD = this.lDate.getHours();
this.lDoY = this.lDate.getDOY();
this.DoW = this.date.getDay();
this.DoM = this.date.getDate();
this.HoD = this.date.getHours();
this.DoY = this.date.getDOY();
var self = this;
Stat.find({ mountain : this.mountain, date : {$lt : this.date} })
.sort({date : -1})
.limit(1)
.exec(function(err,docs){
if(err || docs.length < 1)
return next();
var s2 = docs[0];
self.dt = Math.round((self.date.getTime()-s2.date.getTime())/1000/60);
Stat.StatNames.forEach(function(k){
self[k+'Dx'] = (self[k]-s2[k]);
self[k+'Pm'] = Math.round(self[k+'Dx']/self.dt);
});
next();
});
});
module.exports = Stat;