-
Notifications
You must be signed in to change notification settings - Fork 23
/
generate-test-data.js
258 lines (230 loc) · 8.92 KB
/
generate-test-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
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
/*
* Copyright (C) 2016 TopCoder Inc., All Rights Reserved.
*/
'use strict';
/**
* Module to generate test data
*
* @author TSCCODER
* @version 1.0
*/
require('./bootstrap');
const config = require('config');
const _ = require('lodash');
const logger = require('./common/logger');
const helper = require('./common/helper');
const models = require('./models');
const moment = require('moment');
const Drone = models.Drone;
const User = models.User;
const Category = models.Category;
const Provider = models.Provider;
const Package = models.Package;
const Mission = models.Mission;
const Review = models.Review;
const PackageRequest = models.PackageRequest;
const Notification = models.Notification;
const Service = models.Service;
const DronePosition = models.DronePosition;
const NoFlyZone = models.NoFlyZone;
const Question = models.Question;
let drones;
if (process.argv[2] === 'drones-within-area') {
drones = require('./data/drones-within-area.json');
} else {
drones = require('./data/drones.json');
}
const users = require('./data/users.json');
const categories = require('./data/categories.json');
const providers = require('./data/providers.json');
const packages = require('./data/packages.json');
const missions = require('./data/missions.json');
const requests = require('./data/packageRequests.json');
const notifications = require('./data/notifications.json');
const providerUsers = require('./data/provider-users.json');
const positions = require('./data/dronePositions.json');
const noFlyZones = require('./data/no-fly-zones.json');
const questions = require('./data/questions.json');
const waypoints = require('./data/waypoints.json');
const MissionStatus = require('./enum').MissionStatus;
const RequestStatus = require('./enum').RequestStatus;
for (let i = 0; i < requests.length; i++) {
requests[i].whatToBeDelivered = 'things';
requests[i].weight = Math.random() * 10;
requests[i].payout = Math.random() * 10;
}
// players json data
const co = require('co');
co(function*() {
logger.info('deleting previous data');
yield Drone.remove({});
yield User.remove({});
yield Category.remove({});
yield Provider.remove({});
yield Package.remove({});
yield Mission.remove({});
yield Review.remove({});
yield PackageRequest.remove({});
yield Notification.remove({});
yield DronePosition.remove({});
yield NoFlyZone.remove({});
yield Question.remove({});
// encrypt password
yield _.map(users, (u) => function* () {
if (u.password) {
u.password = yield helper.hashString(u.password, config.SALT_WORK_FACTOR);
}
return;
});
logger.info(`creating ${users.length} users`);
const userDocs = yield User.create(users);
logger.info(`creating ${categories.length} categories`);
const categoryDocs = yield Category.create(categories);
logger.info(`creating ${providers.length} providers`);
const providerDocs = yield Provider.create(providers);
yield _.map(providerUsers, (u) => function*() {
if (u.password) {
u.password = yield helper.hashString(u.password, config.SALT_WORK_FACTOR);
}
});
_.each(providerUsers, (u, i) => {
u.provider = providerDocs[i % providerDocs.length].id;
});
logger.info(`creating provider ${providerUsers.length} users`);
const providerUserDocs = yield User.create(providerUsers);
logger.info('creating 100 services');
const serviceDcos = [];
for (let i = 0; i < 100; i++) {
serviceDcos.push(yield Service.create({
name: 'service ' + i,
pricing: i,
description: 'hello ,i am service ' + i,
provider: providerDocs[i % providerDocs.length].id,
category: categoryDocs[i % categoryDocs.length].id,
}));
}
_.each(packages, (p, i) => {
p.provider = providerDocs[i % providerDocs.length].id;
p.service = serviceDcos[i % serviceDcos.length].id;
p.discount = 95;
p.discountPrice = 95;
p.insuranceClaim = Math.random() * 100;
p.costPerMile = Math.random() * 10;
_.each(p.location, (a) => {
a.state = 'state';
});
});
logger.info(`creating ${drones.length} drones`);
const droneDocs = [];
for (let i = 0; i < drones.length; i++) {
drones[i].provider = providerDocs[i % providerDocs.length].id;
drones[i].pilots = [providerUserDocs[i % providerUserDocs.length]];
drones[i].serialNumber = 'serialNumber' + i;
droneDocs.push(yield Drone.create(drones[i]));
}
// update drones to add accessURL's
for (let i = 0; i < droneDocs.length; i += 1) {
const drone = droneDocs[i];
drone.accessURL = `http://localhost:4040/${drone.id}`;
yield drone.save();
}
logger.info(`creating ${packages.length} packages`);
const packageDocs = yield Package.create(packages);
_.each(missions, (m, i) => {
m.package = packageDocs[i % packageDocs.length].id;
m.provider = packageDocs[i % packageDocs.length].provider;
if (!m.pilot) {
m.pilot = userDocs[0].id; // setting all to first user for testing convinience
}
m.drone = droneDocs[i % droneDocs.length].id;
m.status = _.values(MissionStatus)[Math.floor(Math.random() * _.values(MissionStatus).length)];
m.telemetry = {
lat: 6,
long: 7,
speed: 15,
distance: 12,
};
m.eta = 2;
m.frontCameraUrl = '/assets/front-camera.jpg';
m.backCameraUrl = '/assets/back-camera.jpg';
m.gallery = [{
thumbnailUrl: 'http://google.com',
videoUrl: 'http://google.com',
imageUrl: `/assets/mission-gallery-image-0${i % 4 + 1}.jpg`,
}];
m.plannedHomePosition = waypoints.plannedHomePosition;
m.missionItems = waypoints.missionItems;
});
logger.info(`creating ${missions.length} missions`);
const missionDocs = yield Mission.create(
_.map(missions, (mission, index) => _.assign(mission, {missionName: mission.missionName + ' ' + index}))
);
_.each(requests, (r, i) => {
r.user = userDocs[0].id; // setting all to first user for testing convenience
r.package = packageDocs[i % packageDocs.length].id;
r.provider = packageDocs[i % packageDocs.length].provider;
});
logger.info(`creating ${requests.length} requests`);
const requestDocs = yield PackageRequest.create(requests);
for (let i = 0; i < requestDocs.length; i++) {
const today = moment();
today.hour('10');
today.minute('9');
today.second('7');
today.millisecond('500');
// put missions +-15 days from today
// i + 10 is to make 'drone36' with id '583e6e9df4209e0aa5d94da6' to have missions in different months for testing
today.add((i + 10) % 31 - 15, 'd');
requestDocs[i].mission = missionDocs[i % missionDocs.length].id;
requestDocs[i].status = _.values(RequestStatus)[i % _.values(RequestStatus).length];
requestDocs[i].launchDate = new Date();
yield requestDocs[i].save();
const mindex = i % missionDocs.length;
missionDocs[mindex].packageRequest = requestDocs[i].id;
missionDocs[mindex].provider = requestDocs[i].provider;
// add some missions to the pilot user
if (i % (providerDocs.length + 1) !== providerDocs.length) {
missionDocs[mindex].pilot = providerUserDocs[i % (providerDocs.length + 1)].id;
// for provider user use same statuses as for providerRequests
missionDocs[mindex].status = i % 2 ? MissionStatus.IN_PROGRESS : MissionStatus.COMPLETED;
} else {
missionDocs[mindex].pilot = userDocs[2].id; // pilot user
// for pilot user use all possible statuses
missionDocs[mindex].status = MissionStatus[_.keys(MissionStatus)[Math.floor(_.keys(MissionStatus).length * Math.random())]];
if (missionDocs[mindex].status === MissionStatus.IN_PROGRESS) {
// for in progress mission status checklist needs to be completed.
missionDocs[mindex].status = MissionStatus.SCHEDULED;
}
}
missionDocs[mindex].scheduledAt = today.add(1, 'h').toDate(); // +1 hour
missionDocs[mindex].startedAt = today.add(2, 'h').toDate(); // +2 hours
missionDocs[mindex].launchDate = today.toDate();
if (missionDocs[mindex].status === MissionStatus.COMPLETED) {
missionDocs[mindex].completedAt = today.add(3, 'h').toDate(); // +3 hours
}
missionDocs[mindex].whatToBeDelivered = requestDocs[i].whatToBeDelivered;
missionDocs[mindex].weight = requestDocs[i].weight;
yield missionDocs[mindex].save();
}
_.each(notifications, (n) => {
n.user = userDocs[0].id; // setting all to first user for testing convenience
});
logger.info(`creating ${notifications.length} notifications`);
yield Notification.create(notifications);
_.each(positions, (p) => {
p.droneId = droneDocs[0].id;
});
logger.info(`creating ${positions.length} dronePositions`);
yield DronePosition.create(positions);
logger.info(`creating ${noFlyZones.length} no fly zones`);
yield NoFlyZone.create(noFlyZones);
logger.info(`creating ${questions.length} questions`);
const questionDocs = yield Question.create(questions);
logger.info('data created successfully');
}).then(() => {
logger.info('Done');
process.exit();
}).catch((e) => {
logger.error(e);
process.exit();
});