-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-dummy-data.js
154 lines (127 loc) · 5.83 KB
/
generate-dummy-data.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
var mongoose = require('mongoose');
const config = require('./config');
const Excel = require('exceljs');
const Activity = require('./models/activity');
const Keystroke = require('./models/keystroke');
const Machine = require('./models/machine');
const Organization = require('./models/organization');
const TypingProfile = require('./models/typingProfile');
const User = require('./models/user');
const confirm = require('confirm-cli');
const testGaussian = require('./test-gaussian');
let testModel = require('./ensemble-c-2.json');
testModel.model = JSON.stringify(testModel.model)
testModel.weights = JSON.stringify(testModel.weights)
testModel.reset = JSON.stringify(testModel.reset)
// read from a file
var workbook = new Excel.Workbook();
workbook.xlsx.readFile('dummy-data.xlsx')
.then(() => {
// Parse Data
let data = {}
workbook.eachSheet((worksheet, sheetId) => {
data[worksheet.name] = [];
let headers = [];
worksheet.eachRow((row, rowNumber) => {
if (rowNumber == 1) headers = row.values;
else {
let newVal = {};
row.values.forEach((element, i) => {
let h = headers[i];
if (h != null) {
newVal[h] = element;
}
});
data[worksheet.name].push(newVal);
}
});
});
});
confirm("This will delete all current data, do you want to proceed?", function() {
var workbook = new Excel.Workbook();
workbook.xlsx.readFile('dummy-data.xlsx')
.then(() => {
// Parse Data
let data = {}
workbook.eachSheet((worksheet, sheetId) => {
data[worksheet.name] = [];
let headers = [];
worksheet.eachRow((row, rowNumber) => {
if (rowNumber == 1) headers = row.values;
else {
let newVal = {};
row.values.forEach((element, i) => {
let h = headers[i];
if (h != null) {
if (h == 'challengeStrategies') element = element.split(',');
newVal[h] = element;
}
});
data[worksheet.name].push(newVal);
}
});
});
// Insert data
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URI || config.mongoURI[process.env.NODE_ENV || 'development'], { useMongoClient: true })
.then(db => {
db.dropDatabase();
let organizations = data['organizations'];
let machines = data['machines'];
let users = data['users'];
let typingProfiles = data['typingProfiles']
let activities = data['activities']
Organization.create(organizations, function(err, newOrganizations) {
if (err) throw err;
organizations = newOrganizations;
console.log("Created " + organizations.length + " Organizations");
machines = machines.map(m => {
m.organization = organizations.find(o => o.name == m.organization)._id;
return m;
});
Machine.create(machines, function(err, newMachines) {
if (err) throw err;
machines = newMachines;
console.log("Created " + machines.length + " Machines");
users = users.map(u => {
u.organization = organizations.find(o => o.name == u.organization)._id;
return u;
})
User.create(users, function(err, newUsers) {
if (err) throw err;
users = newUsers;
console.log("Created " + users.length + " Users");
typingProfiles = typingProfiles.map(tp => {
tp.user = users.find(u => u.name == tp.user)._id;
return tp;
});
typingProfiles = typingProfiles.map(tp => {
tp.machine = machines.find(m => m.mac == tp.machine)._id;
return tp;
});
TypingProfile.create(typingProfiles, function(err, newTypingProfiles) {
if (err) throw err;
typingProfiles = newTypingProfiles;
console.log("Created " + typingProfiles.length + " TypingProfiles");
activities = activities.map(a => {
let params = a.typingProfile.split(':');
let user = users.find(u => u.name == params[0])._id;
let machine = machines.find(m => m.mac == params[1])._id;
a.typingProfile = typingProfiles.find(tp => tp.user == user && tp.machine == machine)._id;
return a;
});
Activity.create(activities, function(err, newActivities) {
if (err) throw err;
activities = newActivities;
console.log("Created " + activities.length + " Activities");
mongoose.connection.close()
});
});
});
});
});
}).catch(err => {
console.error('Error connecting to the database. ' + err);
});
});
});