Skip to content

Commit

Permalink
#797 Оптимизация запроса SIP пиров сотрудников.
Browse files Browse the repository at this point in the history
  • Loading branch information
boffart committed Nov 11, 2024
1 parent 78337fc commit abda94f
Showing 1 changed file with 91 additions and 88 deletions.
179 changes: 91 additions & 88 deletions src/Core/Asterisk/Configs/SIPConf.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use MikoPBX\Core\Asterisk\Configs\Generators\Extensions\IncomingContexts;
use MikoPBX\Core\System\{ Network, Processes, SystemMessages, Util};
use MikoPBX\Core\Utilities\SubnetCalculator;
use Phalcon\Di\Di;
use Throwable;

/**
Expand All @@ -63,11 +62,18 @@ class SIPConf extends AsteriskConfigClass
private const string TOPOLOGY_HASH_FILE = '/topology_hash';

/**
* Peers data.
* Peers data offset.
*
* @var mixed
* @var int
*/
protected int $offsetPeers = 0;

/**
* Peers data offset.
*
* @var int
*/
protected $data_peers;
protected int $limitSelectPeers = 3;

/**
* Providers data.
Expand Down Expand Up @@ -127,12 +133,7 @@ public function getDependenceModels(): array
*/
public function needAsteriskRestart(): bool
{
$di = Di::getDefault();
if ($di === null) {
return false;
}
[$topology, $extIpAddress, $externalHostName, $subnets] = $this->getTopologyData();

$externalSipPort = $this->generalSettings[PbxSettings::EXTERNAL_SIP_PORT];
$externalTlsPort = $this->generalSettings[PbxSettings::EXTERNAL_TLS_PORT];
$sipPort = $this->generalSettings[PbxSettings::SIP_PORT];
Expand All @@ -142,7 +143,7 @@ public function needAsteriskRestart(): bool
$now_hash = md5($timeZone.$topology . $externalHostName . $extIpAddress . $sipPort . $externalSipPort . $tlsPort . $externalTlsPort . implode('', $subnets));

$old_hash = '';
$varEtcDir = $di->getShared('config')->path('core.varEtcDir');
$varEtcDir = $this->di->getShared('config')->path('core.varEtcDir');
if (file_exists($varEtcDir . self::TOPOLOGY_HASH_FILE)) {
$old_hash = file_get_contents($varEtcDir . self::TOPOLOGY_HASH_FILE);
}
Expand Down Expand Up @@ -212,7 +213,7 @@ private function getTopologyData(): array
*/
public function extensionGenContexts(): string
{
if ($this->data_peers === null) {
if ($this->data_providers === null) {
$this->getSettings();
}
// Generate internal number plan.
Expand Down Expand Up @@ -241,21 +242,24 @@ public function extensionGenContexts(): string
$conf .= PHP_EOL . '[monitor-internal]' . PHP_EOL;
$confExceptions = '';

// Process peers and their numbers.
foreach ($this->data_peers as $peer) {
$numbers = $usersNumbers[$peer['user_id']] ?? [];
foreach ($numbers as $num) {
$num = substr($num, -9);
if (!str_contains($conf, " $num,")) {
$conf .= "exten => $num,1,NoOp(-)" . PHP_EOL;
}
if ($peer['enableRecording'] !== true && !str_contains($confExceptions, " $num,")) {
$confExceptions .= "exten => $num,1,NoOp(-)" . PHP_EOL;
do {
$data_peers = $this->getPeers();
foreach ($data_peers as $peer) {
$numbers = $usersNumbers[$peer['user_id']] ?? [];
foreach ($numbers as $num) {
$num = substr($num, -9);
if (!str_contains($conf, " $num,")) {
$conf .= "exten => $num,1,NoOp(-)" . PHP_EOL;
}
if ($peer['enableRecording'] !== true && !str_contains($confExceptions, " $num,")) {
$confExceptions .= "exten => $num,1,NoOp(-)" . PHP_EOL;
}
}
}
}
} while (!empty($data_peers));

$conf .= PHP_EOL . '[monitor-exceptions]' . PHP_EOL .
$confExceptions . PHP_EOL . PHP_EOL;
$confExceptions . PHP_EOL . PHP_EOL;
return $conf;
}

Expand All @@ -270,7 +274,6 @@ public function getSettings(): void
{
$this->contexts_data = [];
// Retrieve peers, providers, out routes, technology, and SIP hosts data.
$this->data_peers = $this->getPeers();
$this->data_providers = $this->getProviders();
$this->data_rout = $this->getOutRoutes();
$this->technology = self::getTechnology();
Expand All @@ -292,33 +295,40 @@ private function getPeers(): array
/** @var Users $user */
/** @var ExtensionForwardingRights $extensionForwarding */
$data = [];
$db_data = Sip::find("type = 'peer' AND ( disabled <> '1')");

$filter = [
"type = 'peer' AND ( disabled <> '1')",
"offset" => $this->offsetPeers,
'limit' => $this->limitSelectPeers
];
$db_data = Sip::find($filter)->toArray();
$this->offsetPeers += $this->limitSelectPeers;
if(count($db_data)===0){
$this->offsetPeers = 0;
return $data;
}
// Process each SIP peer.
foreach ($db_data as $sip_peer) {
$arr_data = $sip_peer->toArray();
foreach ($db_data as $arr_data) {
$network_filter = null;

// Retrieve associated network filter if available.
if (! empty($sip_peer->networkfilterid)) {
$network_filter = NetworkFilters::findFirst($sip_peer->networkfilterid);
if (!empty($arr_data['networkfilterid'])) {
$network_filter = NetworkFilters::findFirst($arr_data['networkfilterid']);
}

// Assign permit and deny values based on network filter.
$arr_data['permit'] = ($network_filter === null) ? '' : $network_filter->permit;
$arr_data['deny'] = ($network_filter === null) ? '' : $network_filter->deny;
$arr_data['permit'] = ($network_filter === null)?'': $network_filter->permit;
$arr_data['deny'] = ($network_filter === null)?'': $network_filter->deny;

$arr_data['transport'] = trim($arr_data['transport'] ?? '');
// Retrieve used codecs.
$arr_data['codecs'] = $this->getCodecs();
$arr_data['enableRecording'] = $sip_peer->enableRecording !== '0';
$arr_data['enableRecording'] = $arr_data['enableRecording'] !== '0';

// Retrieve employee name.
$extension = Extensions::findFirst("number = '$sip_peer->extension'");
$extension = Extensions::findFirst("number = '$arr_data[extension]'");
if (null === $extension) {
$arr_data['publicaccess'] = false;
$arr_data['language'] = '';
$arr_data['calleridname'] = $sip_peer->extension;
$arr_data['calleridname'] = $arr_data['extension'];
} else {
$arr_data['publicaccess'] = $extension->public_access;
$arr_data['calleridname'] = $extension->callerid;
Expand All @@ -328,9 +338,8 @@ private function getPeers(): array
$arr_data['user_id'] = $user->id;
}
}

// Retrieve extension forwarding rights.
$extensionForwarding = ExtensionForwardingRights::findFirst("extension = '$sip_peer->extension'");
$extensionForwarding = ExtensionForwardingRights::findFirst("extension = '$arr_data[extension]'");
if (null === $extensionForwarding) {
$arr_data['ringlength'] = '';
$arr_data['forwarding'] = '';
Expand Down Expand Up @@ -423,9 +432,6 @@ private function getProviders(): array
*/
private function getOutRoutes(): array
{
if ($this->data_peers === null) {
$this->getSettings();
}
/** @var OutgoingRoutingTable $rout */
/** @var OutgoingRoutingTable $routs */
/** @var Sip $db_data */
Expand Down Expand Up @@ -495,17 +501,18 @@ public static function getSipHosts(): array
*/
public function extensionGenHints(): string
{
if ($this->data_peers === null) {
$this->getSettings();
}
$conf = '';
foreach ($this->data_peers as $peer) {
$hint = "$this->technology/{$peer['extension']}";
if ($this->generalSettings[PbxSettings::USE_WEB_RTC] === '1') {
$hint .= "&$this->technology/{$peer['extension']}-WS";
do {
$data_peers = $this->getPeers();
foreach ($data_peers as $peer) {
$hint = "$this->technology/$peer[extension]";
if ($this->generalSettings[PbxSettings::USE_WEB_RTC] === '1') {
$hint .= "&$this->technology/$peer[extension]-WS";
}
$conf .= "exten => $peer[extension],hint,$hint&Custom:$peer[extension] \n";
}
$conf .= "exten => {$peer['extension']},hint,$hint&Custom:{$peer['extension']} \n";
}
} while (!empty($data_peers));

return $conf;
}

Expand All @@ -518,15 +525,14 @@ public function extensionGenHints(): string
*/
public function extensionGenInternal(): string
{
if ($this->data_peers === null) {
$this->getSettings();
}
$conf = '';
foreach ($this->data_peers as $peer) {
$conf .= "exten => {$peer['extension']},1,Goto(internal-users,{$peer['extension']},1) \n";
}
do {
$data_peers = $this->getPeers();
foreach ($data_peers as $peer) {
$conf .= "exten => $peer[extension],1,Goto(internal-users,$peer[extension],1) \n";
}
} while (!empty($data_peers));
$conf .= "\n";

return $conf;
}

Expand All @@ -539,16 +545,15 @@ public function extensionGenInternal(): string
*/
public function extensionGenInternalTransfer(): string
{
if ($this->data_peers === null) {
$this->getSettings();
}
$conf = '';
foreach ($this->data_peers as $peer) {
$conf .= "exten => {$peer['extension']},1,Set(__ISTRANSFER=transfer_) \n";
$conf .= " same => n,Goto(internal-users,{$peer['extension']},1) \n";
}
do {
$data_peers = $this->getPeers();
foreach ($data_peers as $peer) {
$conf .= "exten => $peer[extension],1,Set(__ISTRANSFER=transfer_) \n";
$conf .= " same => n,Goto(internal-users,$peer[extension],1) \n";
}
} while (!empty($data_peers));
$conf .= "\n";

return $conf;
}

Expand Down Expand Up @@ -592,19 +597,20 @@ protected function generateConfigProtected(): void
*/
public function updateAsteriskDatabase(): bool
{
if ($this->data_peers === null) {
$this->getSettings();
}
$warError = false;
$db = new AstDB();
foreach ($this->data_peers as $peer) {
// Update Asterisk database with routing information.
$ringLength = ((string)$peer['ringlength'] === '0') ? '' : trim($peer['ringlength']??'');
$warError |= !$db->databasePut('FW_TIME', $peer['extension'], $ringLength);
$warError |= !$db->databasePut('FW', $peer['extension'], trim($peer['forwarding']??''));
$warError |= !$db->databasePut('FW_BUSY', $peer['extension'], trim($peer['forwardingonbusy']??''));
$warError |= !$db->databasePut('FW_UNAV', $peer['extension'], trim($peer['forwardingonunavailable']??''));
}

do {
$data_peers = $this->getPeers();
foreach ($data_peers as $peer) {
// Update Asterisk database with routing information.
$ringLength = ((string)$peer['ringlength'] === '0') ? '' : trim($peer['ringlength']??'');
$warError |= !$db->databasePut('FW_TIME', $peer['extension'], $ringLength);
$warError |= !$db->databasePut('FW', $peer['extension'], trim($peer['forwarding']??''));
$warError |= !$db->databasePut('FW_BUSY', $peer['extension'], trim($peer['forwardingonbusy']??''));
$warError |= !$db->databasePut('FW_UNAV', $peer['extension'], trim($peer['forwardingonunavailable']??''));
}
} while (!empty($data_peers));

return !$warError;
}
Expand Down Expand Up @@ -1151,21 +1157,18 @@ public static function getToneZone(array &$options, string $lang): void
*/
public function generatePeersPj(): string
{
if ($this->data_peers === null) {
$this->getSettings();
}
$lang = $this->generalSettings[PbxSettings::PBX_LANGUAGE];
$conf = '';

foreach ($this->data_peers as $peer) {
$manual_attributes = Util::parseIniSettings($peer['manualattributes'] ?? '');
$conf .= $this->generatePeerAuth($peer, $manual_attributes);
$conf .= $this->generatePeerAor($peer, $manual_attributes);
$conf .= $this->generatePeerEndpoint($lang, $peer, $manual_attributes);
}

do {
$data_peers = $this->getPeers();
foreach ($data_peers as $peer) {
$manual_attributes = Util::parseIniSettings($peer['manualattributes'] ?? '');
$conf .= $this->generatePeerAuth($peer, $manual_attributes);
$conf .= $this->generatePeerAor($peer, $manual_attributes);
$conf .= $this->generatePeerEndpoint($lang, $peer, $manual_attributes);
}
} while (!empty($data_peers));
$conf .= $this->hookModulesMethod(AsteriskConfigInterface::GENERATE_PEERS_PJ);

return $conf;
}

Expand Down

0 comments on commit abda94f

Please sign in to comment.