modems.js
3.37 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
/* eslint-disable no-console */
const DEBUG = !!process.env.DEBUG_KOMODO_CENTER_CP_EVO_MODEMS;
const config = require('komodo-sdk/config');
exports.modemsDictionary = (arrayOfModem) => {
const modems = arrayOfModem || config.modems || [];
const retval = {};
modems.forEach((modem) => {
retval[modem.name] = modem;
});
return retval;
};
/**
* deprecated
*/
exports.enabledModems = (arrayOfModem) => {
const modems = arrayOfModem || config.modems || [];
return modems.filter((modem) => !modem.disabled);
};
exports.outgoingModems = (arrayOfModem) => {
const modems = arrayOfModem || config.modems || [];
return modems.filter((modem) => modem.outgoing);
};
exports.OUTGOING_MODEMS = this.outgoingModems();
exports.prefixes = (arrayOfModem) => {
const prefixModemsObject = {};
const prefixModemsArray = [];
const modems = this.outgoingModems(arrayOfModem);
modems.forEach((modem) => {
const modemPrefixes = modem.prefix || [];
modemPrefixes.forEach((prefix) => {
const normalizedPrefix = prefix.trim().replace(/^0/, '62');
if (!prefixModemsObject[normalizedPrefix]) {
prefixModemsObject[normalizedPrefix] = [modem.name];
} else {
prefixModemsObject[normalizedPrefix].push(modem.name);
}
});
});
// eslint-disable-next-line no-restricted-syntax
for (const [prefix, value] of Object.entries(prefixModemsObject)) {
prefixModemsArray.push({ prefix, modems: value });
}
const retval = prefixModemsArray.sort((a, b) => {
if (a.prefix.length < b.prefix.length) return 1;
if (a.prefix.length > b.prefix.length) return -1;
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
retval.forEach((item) => {
// eslint-disable-next-line no-param-reassign
item.modems = item.modems.sort((a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
});
return retval;
};
exports.randomModem = (arrayOfModem) => {
const modems = this.outgoingModems(arrayOfModem);
if (DEBUG) {
console.log('DEBUG_MODEMS: Get random modems', {
haystack: modems,
});
}
const modemCount = modems.length;
if (!modemCount) return null;
const idx = Math.floor(Math.random() * modemCount);
return modems[idx];
};
exports.findPrefixForNumber = (number, prefixes) => prefixes.find(
(item) => number.replace(/^0/, '62').indexOf(item.prefix) === 0,
);
exports.isFreePrefixModem = (modem) => !modem.prefix || !modem.prefix.length;
exports.randomModemByPrefix = (number, arrayOfModem) => {
const modems = this.outgoingModems(arrayOfModem);
const prefixes = this.prefixes(arrayOfModem);
const prefix = this.findPrefixForNumber(number, prefixes);
const modemNamesForPrefix = (prefix || {}).modems;
if (modemNamesForPrefix && modemNamesForPrefix.length) {
const count = modemNamesForPrefix.length;
const idx = Math.floor(Math.random() * count);
const modemName = modemNamesForPrefix[idx];
return modems.find((modem) => modem.name === modemName);
}
const freeModems = modems.filter((modem) => this.isFreePrefixModem(modem));
const count = freeModems.length;
const idx = Math.floor(Math.random() * count);
return freeModems[idx];
};