-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
265 lines (201 loc) · 9.28 KB
/
server.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
var Fakerator = require("fakerator");
var fs = require('fs');
var md5 = require('md5');
var fakerator = Fakerator("en-US");
var seedrandom = require('seedrandom');
var axios = require('axios');
var dotenv = require('dotenv');
var utils = require('./lib/utils');
var randomdata = require('./lib/randomdata');
var nsapi = require('./lib/nsapi');
const path = require("path");
dotenv.config();
const SEED = process.env.SEED | 0;
const APIKEY = process.env.APIKEY //'';
fakerator.seed(SEED);
//random.seed = SEED;
const TARGET_SERVER = process.env.TARGET_SERVER;
const MAX_DOMAIN = process.env.MAX_DOMAIN || 1;
const NDP_SERVERNAME = process.env.NDP_SERVERNAME || "core1";
const RESELLER = process.env.RESELLER || "NetSapiens";
const RECORDING_DIVISER = process.env.RECORDING_DIVISER || 4;
if (!APIKEY) {
console.error("APIKEY is required. Please set the APIKEY environment variable.");
process.exit(1);
}
if (!TARGET_SERVER) {
console.error("TARGET_SERVER is required. Please set the TARGET_SERVER environment variable.");
process.exit(1);
}
//function to generate random data for the caller ids.
randomdata.buildRandomCallerData();
async function buildDomains() {
let domains_list = [];
for (var i = 0; i < MAX_DOMAIN; i++) { //Preload the domains list to get conistent results.
domains_list.push(fakerator.company.name());
}
for (var i = 0; i < domains_list.length; i++) {
var description = domains_list[i];
var domain = description.replace(/\s/g, '_').replace(/,/g, '_').replace(/\./g, '').replace(/\'/g, '_').toLowerCase();
domain = domain.replace(/-/g, '_').replace(/__/g, '_');
const domainSize = utils.getDomainSize(domain);
var area_random = seedrandom(domain + "area_code")();
var last_four_random = seedrandom(domain + "last_four")();
const area_code = Math.floor(area_random * (900 - 200) + 200);
const last_four = Math.floor(last_four_random * (9990 - 1000) + 1000);
const number = area_code + "555" + (last_four + i);
const time_zone = randomdata.timeZones[i % randomdata.timeZones.length];
let sites = [];
for (var s = 0; s <= Math.floor(domainSize / 30); s++) sites.push(fakerator.address.city());
console.log("[" + i + "]Creating domain " + domain + " with " + domainSize + " users in " + time_zone + " timezone and area code " + area_code + " and main number " + number);
await createDomain({ description, domain, domainSize, area_code, number, time_zone });
//Domain should be created by now.
for (let u = 0; u < domainSize; u++) {
let userArgs = {
domain: domain,
user: 1000 + u,
"name-first-name": fakerator.names.firstName(),
"name-last-name": fakerator.names.lastName(),
"email-address": 1000 + u + "@" + domain + ".com",
"user-scope": u == 0 ? "Office Manager" : u == 1 ? "Call Center Supervisor": "Basic User",
site: sites[u % sites.length],
}
let deviceArgs = {
domain: domain,
user: 1000 + u,
device: 1000 + u,
displayName: userArgs["name-first-name"] + " " + userArgs["name-last-name"],
'device-sip-registration-password': md5(1000 + u + "@" + domain).substring(0, 12), //pysdo random password here.
}
if (u % RECORDING_DIVISER == 0) { // 25% of users will use call recording.
userArgs['recording-configuration'] = "yes";
}
let macArgs = {
domain: domain,
device1: "sip:" + (1000 + u) + "@" + domain,
'device-provisioning-mac-address': md5("mac" + 1000 + u + "@" + domain).replace(/[^0-9a-fA-F]/g, '').substring(0, 12),
'model': randomdata.phoneModels[u % randomdata.phoneModels.length],
'server': NDP_SERVERNAME,
}
await createUser(userArgs);
createDevice(deviceArgs); // Not waiting for this to complete
if (u % 10 < 5) { // 50% of users will have a phone
createMac(macArgs);
}
}
for (let h = 0; h * 10 < domainSize; h++) {
if (h > 8) continue;
const queueName = randomdata.queueNames[(domainSize + h) % randomdata.queueNames.length];
const queue_index = h;
let queueArgs = {
domain: domain,
callqueue: 4000 + queue_index,
description: queueName,
"callqueue-agent-dispatch-timeout-seconds": 30,
"callqueue-dispatch-type": "round-robin",
}
let queueUser = {
domain: domain,
user: 4000 + queue_index,
"name-first-name": queueName,
"name-last-name": "Queue",
"email-address": 4000 + queue_index + "@" + domain + ".com",
"service-code": "system-queue",
"user-scope": "No Portal",
"ring-no-answer-timeout-seconds": 120,
"callqueue-max-wait-timeout-minutes": 30, // the sipp should exit well before this, but prevents issues if sipp dies.
"callqueue-calculate-statistics": "yes",
}
let phonenumberArgs = {
domain: domain,
"phonenumber": "1" + area_code + "555" + (last_four + queue_index),
"dial-rule-description": "DID for " + queueName,
"dial-rule-application": "to-callqueue",
"dial-rule-translation-destination-user": 4000 + queue_index,
"dial-rule-translation-destination-host": domain,
"phone-number-description": queueName,
"time_zone": time_zone //just for scheudling calls.
}
await createQueue(queue_index, queueArgs, () => { }, updateQueue);
await createUser(queueUser); // user for the queue
createPhonenumber(phonenumberArgs);
for (var a = 0; a < Math.floor(domainSize * .1) + 3; a++) { // 10% of domain users will be in each queue
//get random user between 0 and domainSize
const random_agent_index = utils.randomIntFromInterval(0, domainSize);
let agentArgs = {
domain: domain,
"callqueue-agent-id": (1000 + random_agent_index) + "@" + domain,
callqueue: 4000 + queue_index,
"callqueue-agent-priority": random_agent_index > domain / 2 ? "1" : "2" // ~50% of agents will have priority 1
}
createAgent(JSON.parse(JSON.stringify(agentArgs))); // Not waiting for this to complete
}
}
}
}
randomdata.buildRandomCallerData();
buildDomains();
async function createDomain(args) {
//Add some default values to the data object to make sure we have all the required fields.
const data = {
synchronous: 'yes',
domain: args.domain,
description: args.description,
'recording-configuration': 'no',
'language-token': 'en_US',
reseller: RESELLER,
'caller-id-name': args.description.substring(0, 15),
'area-code': args.area_code,
'caller-id-number': args.number,
'caller-id-number-emergency': args.number,
'time-zone': args['time-zone'],
'voicemail-enabled': 'yes',
'domain-type': 'Standard',
'dial-policy': 'US and Canada',
}
const path = `domains`;
nsapi.apiCreateSync(path, data);
}
async function createUser(data) {
data.synchronous = 'yes';
const path = `domains/` + data.domain + '/users';
nsapi.apiCreateSync(path, data, () => { }, updateUser);
}
async function createDevice(data) {
const path = `domains/` + data.domain + '/users/' + data.user + '/devices';
nsapi.apiCreate(path, data, utils.addToCsv, updateDevice);
}
async function createMac(data) {
const path = `domains/` + data.domain + '/phones';
nsapi.apiCreate(path, data);
}
async function updateUser(data) {
const path = `domains/` + data.domain + '/users/' + data.user;
nsapi.apiUpdate(path, data);
}
async function updateDevice(data) {
const path = `domains/` + data.domain + '/users/' + data.user + '/devices/' + data.device;
nsapi.apiUpdate(path, data);
}
async function createPhonenumber(data) {
const path = `domains/` + data.domain + '/phonenumbers';
nsapi.apiCreate(path, data, utils.addToCsvNumber, updatePhonenumber);
}
async function updatePhonenumber(data) {
const path = `domains/` + data.domain + '/phonenumbers/' + data.phonenumber;
nsapi.apiUpdate(path, data);
}
async function createQueue(i, data) {
data.synchronous = 'yes';
const path = `domains/` + data.domain + '/callqueues';
nsapi.apiCreateSync(path, data);
}
async function updateQueue(i, data) {
const path = `domains/` + data.domain + '/callqueues/'+ data.callqueue;
nsapi.apiUpdate(path, data);
}
async function createAgent(data) {
await new Promise(resolve => setTimeout(resolve, 3000)); //wait 3 seconds before sending in this API calls to allow the Queue to properly get into memory.
const path = `domains/` + data.domain + '/callqueues/' + data.callqueue + '/agents';
nsapi.apiCreate(path, data);
}