-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.txt
150 lines (138 loc) · 5.98 KB
/
mongo.txt
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
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/accesslogs');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log('MONGO connection success');
});
var Sequelize = require('sequelize');
var sequelize = new Sequelize('accesslogs', 'root', 'dheeraj', {
dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
port: 3306 // or 5432 (for postgres)
});
sequelize
.authenticate()
.complete(function (err) {
if (err) {
console.log('SQL Unable to connect to the database:', err)
} else {
console.log('SQL Connection has been established successfully.')
}
});
var AccessLog = sequelize.define('Region', {
id: {type: Sequelize.INTEGER, primaryKey: true},
client_id: {type: Sequelize.INTEGER},
client_name: {type: Sequelize.CHAR},
user_id: {type: Sequelize.CHAR},
username: {type: Sequelize.CHAR},
fname: {type: Sequelize.CHAR},
email: {type: Sequelize.CHAR},
lecture_id: {type: Sequelize.INTEGER},
lecture_name: {type: Sequelize.CHAR},
course_id: {type: Sequelize.INTEGER},
course_name: {type: Sequelize.CHAR},
course_code: {type: Sequelize.CHAR},
visibility: {type: Sequelize.INTEGER},
lecture_no: {type: Sequelize.INTEGER},
lecture_state: {type: Sequelize.INTEGER},
application: {type: Sequelize.CHAR},
start_time: {type: Sequelize.DATE},
stop_time: {type: Sequelize.DATE},
ip_address: {type: Sequelize.CHAR},
elapsed_time: {type: Sequelize.FLOAT},
country_code: {type: Sequelize.CHAR},
country_name: {type: Sequelize.CHAR},
city: {type: Sequelize.CHAR},
embed_token: {type: Sequelize.CHAR},
token_uid: {type: Sequelize.CHAR},
is_lti_user: {type: Sequelize.INTEGER}
},
{
tableName: 'tbl_access_log', // this will define the table's name
timestamps: false // this will deactivate the timestamp columns
});
var logSchema = new mongoose.Schema({
userId: Number,
userName: String,
firstName: String,
email: String,
lectureId: Number,
lectureName: String,
courseId: Number,
courseName: String,
courseCode: String,
consumed: []
});
var Log = mongoose.model('Log', logSchema);
var id = 72155;
setInterval(function(){
id = id + 1;
/// call your function here
console.log("============================================");
console.log("RUNNING FOR ID = " + id);
console.log("============================================");
AccessLog.find({where: {id: id}})
.complete(function (err, test) {
if (err) {
console.log('An error occurred while searching for RegionID', err)
} else if (!test) {
console.log('No user with the username "john-doe" has been found.')
} else {
console.log("============================================");
console.log(JSON.stringify(test));
console.log("============================================");
var etime = test.stop_time - test.start_time;
etime = etime / 1000;
//console.log("============================================");
//console.log(etime);
//console.log("============================================");
var query = {userId: test.user_id, lectureId: test.lecture_id};
Log.findOne(query, function (err, person) {
if (err) {
console.log('MONGO got an error');
} else if (person) {
console.log("record found");
person.consumed.push({
start: test.start_time,
end: test.stop_time,
ip: test.ip_address,
elapsedTime: etime,
countryCode: test.country_code,
city: test.city,
application: test.application
});
person.save();
} else {
console.log("record not found");
var log = new Log({
userId: test.user_id,
userName: test.username,
firstName: test.fname,
email: test.email,
lectureId: test.lecture_id,
lectureName: test.lecture_name,
courseId: test.course_id,
courseName: test.course_name,
courseCode: test.course_code,
consumed: [{
start: test.start_time,
end: test.stop_time,
ip: test.ip_address,
elapsedTime: etime,
countryCode: test.country_code,
city: test.city,
application: test.application
}]
});
log.save(function (err, savedData) {
if (err) {
return console.log(err);
} else {
console.log("SAVED IN MONGO DB");
}
});
}
});
}
});
}, 50);