-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (123 loc) · 3.7 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
// const model = require('./model')
const moment = require('moment');
const twix = require('twix');
// const Promise = require('bluebird');
let AnalyticsModel;
class Analytics{
init (mongoose){
const Schema = mongoose.Schema;
//TODO allow developer extend default schema
const AnalyticSchema = new Schema({
category: { type: String, required: true }, //pageView, videoView, imageView, productView, purchase, likes
user_id: {type: String, requried: true},
action: { type: String, required: true }, //login, click
target_id: { type: String}, //pageUrl, videoId, imageId, productId, invoiceNumber, postId
from_url: { type: String},
to_url: { type: String},
value: { type: Number}, //pageCount, purchaseAmount, likeCount
createdAt: { type: Date, default: Date.now},
});
AnalyticsModel = mongoose.model('Analytic', AnalyticSchema);
};
//TODO open new collection by category
send(entry) {
let defaultEntry = {
eventValue: 1,
createAt: Date.now(),
};
const mergedEntry = Object.assign(defaultEntry, entry);
console.log('merged entry is :');
console.log(mergedEntry);
return new AnalyticsModel(mergedEntry).save();
};
//return timeline array from, to DATE obj
getTimeLine(steps, from, to) {
let arr = moment(from).twix(to).toArray(steps);
let timeLine = [];
arr.forEach( (element) => {
timeLine.push(element.toDate());
});
return timeLine;
};
// group by steps
//TODO code conditions, for now group by DAY
groupBy (query, steps, from, to) {
let timeframe = {};
if (from != null){
timeframe = {"createdAt": {"$gte": from, "$lt": to}};
}
let filter = Object.assign(timeframe, query);
console.log(filter);
return AnalyticsModel
// .find()
.aggregate([
{
$match: filter,
},
// what if user didn't specify granularity till hour min sec?
{$project:
{
year: { $year: "$createdAt" },
month: { $month: "$createdAt" },
day: { $dayOfMonth: "$createdAt" },
hour: { $hour: "$createdAt" },
minutes: { $minute: "$createdAt" },
seconds: { $second: "$createdAt" },
user_id: 1,
// milliseconds: { $millisecond: "$date" },
// dayOfYear: { $dayOfYear: "$date" },
// dayOfWeek: { $dayOfWeek: "$date" },
// week: { $week: "$date" }
}
},
{
$group: {
_id: {
'year': '$year',
'month': '$month',
'day': '$day',
// 'hour': '$hour',
// 'minutes': '$minutes'
} ,
'user_ids': { $push: "$user_id" },
}
}
])
.exec()
.catch((e) => {
console.log('Failed to filter');
throw e;
});
};
getLogFilter(query, steps, from, to) {
let fromDate = new Date('1970-01-01T00:00:00');
let toDate = Date.now();
//parse user inpute time range
if (from !== null) {
let from_string = from.split('-');
fromDate = new Date(from_string[0], from_string[1]-1, from_string[2]);
}
if (to !== null) {
let to_string = to.split('-');
toDate = new Date(to_string[0], to_string[1]-1, to_string[2]);
}
return this.groupBy(query, steps, fromDate, toDate)
.then((data) => {
var time_series = {
x_axis: this.getTimeLine(steps, from, to),
y_axis: data,
};
console.log();
console.log('printing y-axis')
console.log(data);
// console.log(time_series);
return time_series;
})
};
}
module.exports = new Analytics();
// 1. filter by time range and steps (minute, hour, days...)
// 2. group counts (by same step min, hour, day)
// 3. pad specified steps(min hour day) without count with 0
// 4 return array [count, timeline within range]