-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
175 lines (156 loc) · 4.41 KB
/
app.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
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
var mongoose = require('mongoose');
const readline = require('readline')
var dbPath = "mongodb://localhost/medi";
// command to connect with database
db = mongoose.connect(dbPath, {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true});
// inilialise the models
require('./models/userView');
var userModel = mongoose.model('userView')
// declare the readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// question 1
const question1 = () => {
return new Promise((resolve, reject) => {
let q = `\n\n
Please select:\n
1) Daily
2) Weekly
3) Monthly
4) Yearly
5) Custom
6) Exit\n`
rl.question(q, (answer) => {
resolve(answer)
})
})
}
// question 2
const question2 = () => {
return new Promise((resolve, reject) => {
rl.question('Enter custom date (Format: yyyy-mm-dd)\n', (answer) => {
resolve(answer)
})
})
}
// Handle db connection error
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
// connect to database and execute the function
mongoose.connection.once('open', function() {
console.log("database connection open success");
takeInputAndCalculate()
});
// function to take input and do db operations
async function takeInputAndCalculate() {
let d = await question1()
let opt = d.toString().trim();
let aggregate
if(opt=='1'){
aggregate = [
{
$group: {
_id: { day: { $dayOfMonth: "$ViewDate" }},views: { $sum: 1 },uniqueValues: {$addToSet: "$UserId"}
}
},
{
$project:{
_id: 0,
day: "$_id.day",
views: 1,
uniqueViews:{$size:"$uniqueValues"}
}
}
]
}else if(opt=='2'){
aggregate = [
{
$group: {
_id: { week: { $week : "$ViewDate" }},views: { $sum: 1 },uniqueValues: {$addToSet: "$UserId"}
}
},
{
$project:{
_id: 0,
week: "$_id.week",
views: 1,
uniqueViews:{$size:"$uniqueValues"}
}
}
]
}else if(opt=='3'){
aggregate = [
{
$group: {
_id: { month: { $month : "$ViewDate" }},views: { $sum: 1 },uniqueValues: {$addToSet: "$UserId"}
}
},
{
$project:{
_id: 0,
month: "$_id.month",
views: 1,
uniqueViews:{$size:"$uniqueValues"}
}
}
]
}else if(opt=='4'){
aggregate = [
{
$group: {
_id: { year: { $year : "$ViewDate" }},views: { $sum: 1 },uniqueValues: {$addToSet: "$UserId"}
}
},
{
$project:{
_id: 0,
year: "$_id.year",
views: 1,
uniqueViews:{$size:"$uniqueValues"}
}
}
]
}else if(opt=='5'){
let input2 = await question2()
let date = input2.toString().trim();
aggregate = [
{"$match":{"ViewDate":{
"$gte": new Date(date+"T00:00:00.000Z"),
"$lte": new Date(date+"T23:59:59.999Z")
}}},
{
$group: {
_id: { day: { $dayOfMonth: "$ViewDate" }},views: { $sum: 1 },uniqueValues: {$addToSet: "$UserId"}
}
},
{
$project:{
_id: 0,
day: "$_id.day",
views: 1,
uniqueViews:{$size:"$uniqueValues"}
}
}
]
}else if(opt=='6'){
console.log("Bye Bye")
process.exit(0);
}else{
console.log('invalid option')
takeInputAndCalculate()
}
if(opt){
userModel.aggregate(aggregate,(err,foundUser)=>{
if(err){
console.log(err)
process.exit(1);
}
console.log(foundUser)
takeInputAndCalculate()
})
}else{
console.log('No option selected')
takeInputAndCalculate()
}
}