forked from parse-community/parse-server-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTotals.js
More file actions
297 lines (257 loc) · 9.71 KB
/
Totals.js
File metadata and controls
297 lines (257 loc) · 9.71 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var dateUtil = require('./DateUtil.js');
exports.getGlobalTotals = function(callback) {
//retrieve the globalTotals object, create with defaults if necessary
var GlobalTotals = Parse.Object.extend("GlobalTotals");
var totalQuery = new Parse.Query(GlobalTotals);
totalQuery.first({
success: function(object) {
var globalTotals = object;
if (!globalTotals) {
console.log("GlobalTotals not found, creating");
globalTotals = new GlobalTotals();
globalTotals.set("users", 0);
globalTotals.set("trips", 0);
globalTotals.set("distanceTravelled", 0);
globalTotals.set("minutesTravelled", 0);
globalTotals.set("missedSMSCount", 0);
globalTotals.set("missedCallCount", 0);
globalTotals.set("missedOtherCount", 0);
globalTotals.save();
}
callback(globalTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
}
exports.getDailyTotals = function(date, callback, failCallback) {
//retrieve the dailyTotals object, create with defaults if necessary
var DailyTotals = Parse.Object.extend("DailyTotals");
var totalQuery = new Parse.Query(DailyTotals);
var queryDate = new Date(date);
queryDate.setHours(0, 0, 0, 0); //only year, month, day remain
totalQuery.equalTo("date", queryDate);
totalQuery.first({
success: function(object) {
var dailyTotals = object;
if (!dailyTotals) {
console.log("DailyTotals not found, creating");
dailyTotals = new DailyTotals();
dailyTotals.set("date", queryDate);
dailyTotals.set("users", 0);
dailyTotals.set("trips", 0);
dailyTotals.set("distanceTravelled", 0);
dailyTotals.set("minutesTravelled", 0);
dailyTotals.set("missedSMSCount", 0);
dailyTotals.set("missedCallCount", 0);
dailyTotals.set("missedOtherCount", 0);
dailyTotals.save();
}
callback(dailyTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
if (failCallback) {
failCallback(error);
}
}
});
}
exports.getMonthlyTotals = function(date, callback, failCallback) {
//retrieve the monthlyTotals object, create with defaults if necessary
var MonthlyTotals = Parse.Object.extend("MonthlyTotals");
var totalQuery = new Parse.Query(MonthlyTotals);
var queryDate = new Date(date);
queryDate.setHours(0, 0, 0, 0);
queryDate.setDate(1); //only year, month remain
totalQuery.equalTo("date", queryDate);
totalQuery.first({
success: function(object) {
var monthlyTotals = object;
if (!monthlyTotals) {
console.log("MonthlyTotals not found, creating");
monthlyTotals = new MonthlyTotals();
monthlyTotals.set("date", queryDate);
monthlyTotals.set("users", 0);
monthlyTotals.set("trips", 0);
monthlyTotals.set("distanceTravelled", 0);
monthlyTotals.set("minutesTravelled", 0);
monthlyTotals.set("missedSMSCount", 0);
monthlyTotals.set("missedCallCount", 0);
monthlyTotals.set("missedOtherCount", 0);
monthlyTotals.save();
}
callback(monthlyTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
if (failCallback) {
failCallback(error);
}
}
});
}
exports.getUserTotals = function(userId, callback, failCallback) {
//retrieve the userTotals object, create with defaults if necessary
var UserTotals = Parse.Object.extend("UserTotals");
var totalQuery = new Parse.Query(UserTotals);
var user = new Parse.User({
id: userId
});
totalQuery.equalTo("user", user);
totalQuery.first({
success: function(object) {
var userTotals = object;
if (!userTotals) {
var dayDate = new Date();
dayDate.setHours(0, 0, 0, 0);
var monthDate = new Date(dayDate);
monthDate.setDate(1); //only year, month remain
console.log("UserTotals not found, creating");
userTotals = new UserTotals();
userTotals.set("dayDistanceTravelled", [0,0,0,0,0,0,0]);
userTotals.set("dayMinutesTravelled", [0,0,0,0,0,0,0]);
userTotals.set("lastDay", dayDate);
userTotals.set("monthDistanceTravelled", [0,0,0,0,0,0,0,0,0,0,0,0]);
userTotals.set("monthMinutesTravelled", [0,0,0,0,0,0,0,0,0,0,0,0]);
userTotals.set("lastMonth", monthDate);
userTotals.set("missedCallCount", 0);
userTotals.set("missedSMSCount", 0);
userTotals.set("missedOtherCount", 0);
userTotals.set("trips", 0);
userTotals.set("distanceTravelled", 0);
userTotals.set("minutesTravelled", 0);
userTotals.set("user", user);
userTotals.save();
// console.log("UserTotals creating:"+JSON.stringify(userTotals));
}
callback(userTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
if (failCallback) {
failCallback(error);
}
}
});
}
exports.updateTotals = function(trip, totals) {
if (!trip.get("hidden") && trip.get("valid")) {
totals.increment("trips");
var distanceTravelled = trip.get("distanceTravelled");
if (distanceTravelled) {
totals.increment("distanceTravelled", distanceTravelled);
}
var startTime = trip.get("startTime");
var endTime = trip.get("endTime");
if (startTime && endTime) {
totals.increment("minutesTravelled", dateUtil.getMinutesBetweenDates(startTime, endTime));
}
var missedSMSCount = trip.get("missedSMSCount");
if (missedSMSCount) {
totals.increment("missedSMSCount", missedSMSCount);
}
var missedCallCount = trip.get("missedCallCount");
if (missedCallCount) {
totals.increment("missedCallCount", missedCallCount);
}
var missedOtherCount = trip.get("missedOtherCount");
if (missedOtherCount) {
totals.increment("missedOtherCount", missedOtherCount);
}
}
}
exports.updateUserTotals = function(trip, totals) {
if (!trip.get("hidden") && trip.get("valid")) {
var distanceTravelled = trip.get("distanceTravelled");
var startTime = trip.get("startTime");
var endTime = trip.get("endTime");
if (startTime && endTime && distanceTravelled) {
exports.pushArraysIfnecessary(totals);
var minutesTravelled = dateUtil.getMinutesBetweenDates(startTime, endTime);
var tripDayDate = new Date(startTime);
tripDayDate.setHours(0, 0, 0, 0); //only year, month, day remain
var dayDistanceTravelled = totals.get("dayDistanceTravelled");
var dayMinutesTravelled = totals.get("dayMinutesTravelled");
var thisDay = new Date();
thisDay.setHours(0, 0, 0, 0);
var decrementDayFunction = function(date) {date.setDate(date.getDate() - 1);};
incrementExistingPosition(7, thisDay, tripDayDate, distanceTravelled, dayDistanceTravelled, decrementDayFunction);
totals.set("dayDistanceTravelled", dayDistanceTravelled);
incrementExistingPosition(7, thisDay, tripDayDate, minutesTravelled, dayMinutesTravelled, decrementDayFunction);
totals.set("dayMinutesTravelled", dayMinutesTravelled);
//update the arrays representing the months of the year
var tripMonthDate = new Date(tripDayDate);
tripMonthDate.setDate(1); //only year, month remain
var monthDistanceTravelled = totals.get("monthDistanceTravelled");
var monthMinutesTravelled = totals.get("monthMinutesTravelled");
var thisMonth = new Date(thisDay);
thisMonth.setDate(1);
var decrementMonthFunction = function(date) {
date.setMonth(date.getMonth() - 1);
date.setDate(1);
};
incrementExistingPosition(12, thisMonth, tripMonthDate, distanceTravelled, monthDistanceTravelled, decrementMonthFunction);
totals.set("monthDistanceTravelled", monthDistanceTravelled);
incrementExistingPosition(12, thisMonth, tripMonthDate, minutesTravelled, monthMinutesTravelled, decrementMonthFunction);
totals.set("monthMinutesTravelled", monthMinutesTravelled);
// console.log("UserTotals updating:"+JSON.stringify(totals));
}
exports.updateTotals(trip, totals);
}
}
/*
If the arrays need to be shifted, then shift them
*/
exports.pushArraysIfnecessary = function(totals) {
var dayDistanceTravelled = totals.get("dayDistanceTravelled");
var dayMinutesTravelled = totals.get("dayMinutesTravelled");
var lastDay = totals.get("lastDay");
var thisDay = new Date();
thisDay.setHours(0, 0, 0, 0);
//if the arrays aren't currently set for this day, push them until they are
if (thisDay > lastDay) {
var daysBetween = dateUtil.getDaysBetweenDates(thisDay, lastDay);
pushArrayToNewDate(7, daysBetween, dayDistanceTravelled);
pushArrayToNewDate(7, daysBetween, dayMinutesTravelled);
totals.set("lastDay", thisDay);
}
var monthDistanceTravelled = totals.get("monthDistanceTravelled");
var monthMinutesTravelled = totals.get("monthMinutesTravelled");
var lastMonth = totals.get("lastMonth");
var thisMonth = new Date(thisDay);
thisMonth.setDate(1);
//if the arrays aren't currently set for thisMonth, push them until they are
if (thisMonth > lastMonth) {
var monthsBetween = dateUtil.getMonthsBetweenDates(thisMonth, lastMonth);
pushArrayToNewDate(12, monthsBetween, monthDistanceTravelled);
pushArrayToNewDate(12, monthsBetween, monthMinutesTravelled);
totals.set("lastMonth", thisMonth);
}
}
/*
adds the new value to the array and pads out with the necessary number of 0's
*/
function pushArrayToNewDate(count, unitsBetween, travelledArray) {
var i = 0;
var numShifts = (unitsBetween >= count)?count:unitsBetween;
for (i = 0; i < numShifts; i++) {
travelledArray.unshift(0);//add to start of array
travelledArray.pop();//remove last element in array
}
}
/*
finds the existing position of a trip in an array and increments the value at that position
*/
function incrementExistingPosition(count, thisDate, tripDate, travelled, travelledArray, decrementDateFunction) {
var i = 0;
var dateForPosition = new Date(thisDate);
for (i = 0; i < count; i++) {
if (dateForPosition.valueOf() == tripDate.valueOf()) {
travelledArray[i] = travelledArray[i] + travelled;
break;
}
decrementDateFunction(dateForPosition);
}
}