-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
493 lines (392 loc) · 19.6 KB
/
server.js
File metadata and controls
493 lines (392 loc) · 19.6 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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 { loadConfig } = require('./lib/config');
const path = require("path");
dotenv.config();
// Load multi-server or single-server configuration
let appConfig;
let selectedServer;
let apiClient;
try {
appConfig = loadConfig();
selectedServer = appConfig.selectedServer;
// Create API client for the selected server
apiClient = new nsapi.ServerApiClient(selectedServer);
console.log(`\n======================================`);
console.log(`Configuration Mode: ${appConfig.mode}`);
console.log(`Target Server: ${selectedServer.hostname}`);
console.log(`Server ID: ${selectedServer.id}`);
console.log(`Max Domains: ${selectedServer.maxDomains}`);
console.log(`Peak CPS: ${selectedServer.peakCps}`);
console.log(`Registration %: ${selectedServer.registrationPct}`);
console.log(`SEED: ${selectedServer.seed}`);
console.log(`======================================\n`);
} catch (error) {
console.error(`Failed to load configuration: ${error.message}`);
process.exit(1);
}
// Configuration constants
const CONFIG = {
USER_EXTENSION_START: 1000,
QUEUE_EXTENSION_START: 4000,
MAC_ADDRESS_PERCENTAGE: 0.5, // 50% of users get MAC addresses
RECORDING_PERCENTAGE: 0.25, // 25% get recording (1/4)
AGENTS_PER_QUEUE_PERCENTAGE: 0.1, // 10% of domain users per queue
MIN_AGENTS_PER_QUEUE: 3,
LARGE_DOMAIN_THRESHOLD: 100,
USERS_PER_SITE: 30,
QUEUES_PER_USERS_RATIO: 10, // 1 queue per 10 users
MAX_QUEUES: 8,
AREA_CODE_MIN: 200,
AREA_CODE_MAX: 900,
PHONE_LAST_FOUR_MIN: 1000,
PHONE_LAST_FOUR_MAX: 9990,
// Performance tuning
MAX_CONCURRENT_DOMAINS: 5, // Process multiple domains in parallel
USER_BATCH_SIZE: 25, // Increased from 15
DEVICE_BATCH_SIZE: 25, // Larger batches for devices
REDUCE_DELAYS: false, // Feature flag to reduce/eliminate delays
AGENT_DELAY_MS: 500, // Reduced from 3000ms
BATCH_DELAY_MS: 100, // Reduced from 200ms
DEVICE_DELAY_MS: 25 // Reduced from 100ms
};
// Use server-specific configuration values
const SEED = selectedServer.seed;
const APIKEY = selectedServer.apikey;
fakerator.seed(SEED);
const TARGET_SERVER = selectedServer.hostname;
const MAX_DOMAIN = selectedServer.maxDomains;
const NDP_SERVERNAME = process.env.NDP_SERVERNAME || "core1";
const RESELLER = process.env.RESELLER || "NetSapiens";
const RECORDING_DIVISER = process.env.RECORDING_DIVISER || 4;
// Input validation
function validateEnvironment() {
const errors = [];
// Configuration is now validated in config.js, but do additional checks here
if (!APIKEY) {
errors.push("APIKEY is required in server configuration");
} else if (!APIKEY.startsWith('nss_')) {
console.warn("Warning: APIKEY should typically start with 'nss_'");
}
if (!TARGET_SERVER) {
errors.push("TARGET_SERVER is required in server configuration");
} else if (!TARGET_SERVER.includes('.')) {
console.warn("Warning: TARGET_SERVER should be a valid hostname");
}
if (MAX_DOMAIN < 1 || MAX_DOMAIN > 1000) {
errors.push("MAX_DOMAIN must be between 1 and 1000");
}
if (RECORDING_DIVISER < 1) {
errors.push("RECORDING_DIVISER must be greater than 0");
}
if (errors.length > 0) {
console.error("Configuration errors:");
errors.forEach(error => console.error(` - ${error}`));
process.exit(1);
}
console.log("Server configuration validation passed");
}
validateEnvironment();
//function to generate random data for the caller ids.
randomdata.buildRandomCallerData();
async function buildDomains() {
console.log(`Starting to build ${MAX_DOMAIN} domains with ${CONFIG.MAX_CONCURRENT_DOMAINS} concurrent processes...`);
const startTime = Date.now();
let domains_list = [];
for (var i = 0; i < MAX_DOMAIN; i++) { //Preload the domains list to get consistent results.
domains_list.push(fakerator.company.name());
}
// Process domains in parallel batches
const domainBatches = [];
for (let i = 0; i < domains_list.length; i += CONFIG.MAX_CONCURRENT_DOMAINS) {
domainBatches.push(domains_list.slice(i, i + CONFIG.MAX_CONCURRENT_DOMAINS));
}
for (let batchIndex = 0; batchIndex < domainBatches.length; batchIndex++) {
const batch = domainBatches[batchIndex];
const batchStartTime = Date.now();
console.log(`Processing batch ${batchIndex + 1}/${domainBatches.length} with ${batch.length} domains...`);
// Process all domains in this batch concurrently
const domainPromises = batch.map((description, batchItemIndex) => {
const globalIndex = batchIndex * CONFIG.MAX_CONCURRENT_DOMAINS + batchItemIndex;
return processSingleDomain(description, globalIndex);
});
await Promise.allSettled(domainPromises);
const batchTime = ((Date.now() - batchStartTime) / 1000).toFixed(2);
console.log(`Batch ${batchIndex + 1} completed in ${batchTime}s`);
// Small delay between batches to prevent API overload
if (batchIndex < domainBatches.length - 1) {
await new Promise(resolve => setTimeout(resolve, CONFIG.BATCH_DELAY_MS));
}
}
const totalTime = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`All ${MAX_DOMAIN} domains completed in ${totalTime}s`);
}
async function processSingleDomain(description, i) {
try {
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 * (CONFIG.AREA_CODE_MAX - CONFIG.AREA_CODE_MIN) + CONFIG.AREA_CODE_MIN);
const last_four = Math.floor(last_four_random * (CONFIG.PHONE_LAST_FOUR_MAX - CONFIG.PHONE_LAST_FOUR_MIN) + CONFIG.PHONE_LAST_FOUR_MIN);
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 / CONFIG.USERS_PER_SITE); 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.
createNdpUiConfig({domain});
// Prepare all user data upfront for better async batching
const userDataBatch = [];
const deviceDataBatch = [];
const macDataBatch = [];
for (let u = 0; u < domainSize; u++) {
let userArgs = {
domain: domain,
user: CONFIG.USER_EXTENSION_START + u,
"name-first-name": fakerator.names.firstName(),
"name-last-name": fakerator.names.lastName(),
"email-address": (CONFIG.USER_EXTENSION_START + u) + "@" + domain + ".com",
"user-scope": u == 0 ? "Office Manager" : u == 1 ? "Call Center Supervisor": "Basic User",
site: sites[u % sites.length],
//use 6 departements if domain size is < 100, otherwise use 12 departments. Randomize start in the list by domain and user index.
department: randomdata.departmentNames[((u%(domainSize>CONFIG.LARGE_DOMAIN_THRESHOLD?12:6))+i) % randomdata.departmentNames.length],
}
let deviceArgs = {
domain: domain,
user: CONFIG.USER_EXTENSION_START + u,
device: CONFIG.USER_EXTENSION_START + u,
displayName: userArgs["name-first-name"] + " " + userArgs["name-last-name"],
'device-sip-registration-password': md5((CONFIG.USER_EXTENSION_START + 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:" + (CONFIG.USER_EXTENSION_START + u) + "@" + domain,
'device-provisioning-mac-address': md5("mac" + (CONFIG.USER_EXTENSION_START + u) + "@" + domain).replace(/[^0-9a-fA-F]/g, '').substring(0, 12),
'model': randomdata.phoneModels[u % randomdata.phoneModels.length],
'server': NDP_SERVERNAME,
}
userDataBatch.push(userArgs);
deviceDataBatch.push(deviceArgs);
if (u % 10 < (CONFIG.MAC_ADDRESS_PERCENTAGE * 10)) { // 50% of users will have a phone
macDataBatch.push(macArgs);
}
}
// Process users in optimized batches
const BATCH_SIZE = CONFIG.USER_BATCH_SIZE;
for (let batchStart = 0; batchStart < userDataBatch.length; batchStart += BATCH_SIZE) {
const userBatch = userDataBatch.slice(batchStart, batchStart + BATCH_SIZE);
const deviceBatch = deviceDataBatch.slice(batchStart, batchStart + BATCH_SIZE);
// Process user batch with proper error handling
const userPromises = userBatch.map(async (userArgs) => {
try {
await createUser(userArgs);
return { success: true, user: userArgs.user };
} catch (error) {
console.error(`Failed to create user ${userArgs.user} in domain ${domain}:`, error.message);
return { success: false, user: userArgs.user, error };
}
});
const userResults = await Promise.allSettled(userPromises);
// Only create devices for successfully created users
if (!CONFIG.REDUCE_DELAYS) {
await new Promise(resolve => setTimeout(resolve, 250)); // Reduced wait time
}
const successfulUsers = userResults
.map((result, index) => ({ result: result.value, device: deviceBatch[index] }))
.filter(item => item.result && item.result.success)
.map(item => item.device);
// Process devices in larger batches for better performance
const deviceBatchSize = Math.min(CONFIG.DEVICE_BATCH_SIZE, successfulUsers.length);
for (let i = 0; i < successfulUsers.length; i += deviceBatchSize) {
const deviceSubBatch = successfulUsers.slice(i, i + deviceBatchSize);
// Create all devices in this sub-batch concurrently
deviceSubBatch.forEach(deviceArgs => createDevice(deviceArgs));
// Smaller delay between device batches
if (i + deviceBatchSize < successfulUsers.length && !CONFIG.REDUCE_DELAYS) {
await new Promise(resolve => setTimeout(resolve, CONFIG.DEVICE_DELAY_MS));
}
}
// Reduced delay between batches
if (batchStart + BATCH_SIZE < userDataBatch.length && !CONFIG.REDUCE_DELAYS) {
await new Promise(resolve => setTimeout(resolve, CONFIG.BATCH_DELAY_MS));
}
}
// Process MAC addresses asynchronously
macDataBatch.forEach(macArgs => createMac(macArgs));
for (let h = 0; h * CONFIG.QUEUES_PER_USERS_RATIO < domainSize; h++) {
if (h > CONFIG.MAX_QUEUES) continue;
const queueName = randomdata.queueNames[(domainSize + h) % randomdata.queueNames.length];
const queue_index = h;
let queueArgs = {
domain: domain,
callqueue: CONFIG.QUEUE_EXTENSION_START + queue_index,
description: queueName,
"callqueue-agent-dispatch-timeout-seconds": 30,
"callqueue-dispatch-type": "round-robin",
"callqueue-calculate-statistics": "yes",
}
let queueUser = {
domain: domain,
user: CONFIG.QUEUE_EXTENSION_START + queue_index,
"name-first-name": queueName,
"name-last-name": "Queue",
"email-address": (CONFIG.QUEUE_EXTENSION_START + 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": CONFIG.QUEUE_EXTENSION_START + queue_index,
"dial-rule-translation-destination-host": domain,
"phone-number-description": queueName,
"time_zone": time_zone //just for scheudling calls.
}
try {
await createQueue(queue_index, queueArgs, () => { }, updateQueue);
await createUser(queueUser); // user for the queue
createPhonenumber(phonenumberArgs);
} catch (error) {
console.error(`Failed to create queue ${queueArgs.callqueue} in domain ${domain}:`, error.message);
continue; // Skip this queue and move to the next one
}
// Reduced wait time for large domains
if (domainSize > CONFIG.LARGE_DOMAIN_THRESHOLD && !CONFIG.REDUCE_DELAYS) {
await new Promise(resolve => setTimeout(resolve, 250)); // Reduced from 1000ms
}
for (var a = 0; a < Math.floor(domainSize * CONFIG.AGENTS_PER_QUEUE_PERCENTAGE) + CONFIG.MIN_AGENTS_PER_QUEUE; 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);
// Reduced agent processing delay
if (a % 20 == 19 && !CONFIG.REDUCE_DELAYS) { // Less frequent delays
await new Promise(resolve => setTimeout(resolve, 100)); // Reduced from 200ms
}
let agentArgs = {
domain: domain,
"callqueue-agent-id": (CONFIG.USER_EXTENSION_START + random_agent_index) + "@" + domain,
callqueue: CONFIG.QUEUE_EXTENSION_START + queue_index,
"callqueue-agent-priority": random_agent_index > domainSize / 2 ? "1" : "2" // ~50% of agents will have priority 1
}
createAgent(JSON.parse(JSON.stringify(agentArgs))); // Not waiting for this to complete
}
}
console.log(`[${i}] Domain ${domain} completed successfully`);
} catch (error) {
console.error(`[${i}] Failed to process domain ${description}:`, error.message);
// Continue processing - don't let one domain failure stop others
}
}
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`;
await apiClient.apiCreateSync(path, data);
await new Promise(resolve => setTimeout(resolve, 200));
}
async function createNdpUiConfig(args) {
const path = `configurations` ;
const data = {
"reseller": "*",
"user": "*",
"user-scope": "*",
"core-server": "*",
"config-name": "PORTAL_DEVICE_NDP_SERVER",
"config-value": NDP_SERVERNAME,
"domain": args.domain
}
apiClient.apiCreate(path, data, () => { }, updateNdpUiConfig);
}
async function updateNdpUiConfig(data) {
const path = `configurations` ;
apiClient.apiUpdate(path, data);
}
async function createUser(data) {
data.synchronous = 'yes';
const path = `domains/` + data.domain + '/users';
await apiClient.apiCreateSync(path, data, () => { }, updateUser);
}
async function createDevice(data) {
const path = `domains/` + data.domain + '/users/' + data.user + '/devices';
// Pass server ID to utils.addToCsv for server-specific CSV paths
const successCallback = (deviceData) => utils.addToCsv(deviceData, selectedServer.id);
await apiClient.apiCreate(path, data, successCallback, updateDevice);
}
async function createMac(data) {
const path = `domains/` + data.domain + '/phones';
await apiClient.apiCreate(path, data);
}
async function updateUser(data) {
const path = `domains/` + data.domain + '/users/' + data.user;
apiClient.apiUpdate(path, data);
}
async function updateDevice(data) {
const path = `domains/` + data.domain + '/users/' + data.user + '/devices/' + data.device;
apiClient.apiUpdate(path, data);
}
async function createPhonenumber(data) {
const path = `domains/` + data.domain + '/phonenumbers';
// Pass server ID to utils.addToCsvNumber for server-specific CSV paths
const successCallback = (phoneData) => utils.addToCsvNumber(phoneData, selectedServer.id);
apiClient.apiCreate(path, data, successCallback, updatePhonenumber);
}
async function updatePhonenumber(data) {
const path = `domains/` + data.domain + '/phonenumbers/' + data.phonenumber;
apiClient.apiUpdate(path, data);
}
async function createQueue(i, data) {
data.synchronous = 'yes';
const path = `domains/` + data.domain + '/callqueues';
await apiClient.apiCreateSync(path, data, () => { }, updateQueue);
}
function updateQueue(data) {
const path = `domains/` + data.domain + '/callqueues/'+ data.callqueue;
apiClient.apiUpdate(path, data);
}
async function createAgent(data) {
// Significantly reduced delay - rely on retry logic for timing issues
if (!CONFIG.REDUCE_DELAYS) {
await new Promise(resolve => setTimeout(resolve, CONFIG.AGENT_DELAY_MS)); // Reduced from 3000ms
}
const path = `domains/` + data.domain + '/callqueues/' + data.callqueue + '/agents';
try {
await apiClient.apiCreateSync(path, data);
} catch (error) {
console.error(`Failed to create agent ${data['callqueue-agent-id']} for queue ${data.callqueue}:`, error.message);
}
}