new: [sync] Added sync priority system to prioritise the order of instances to push to

pull/5158/head
iglocska 2019-09-13 11:49:12 +02:00
parent b004cf0290
commit ffc9147018
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
7 changed files with 118 additions and 6 deletions

View File

@ -46,7 +46,7 @@ class AppController extends Controller
public $helpers = array('Utility', 'OrgImg', 'FontAwesome', 'UserName');
private $__queryVersion = '84';
private $__queryVersion = '86';
public $pyMispVersion = '2.4.114';
public $phpmin = '7.0';
public $phprec = '7.2';

View File

@ -22,7 +22,7 @@ class ServersController extends AppController
),
'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events
'order' => array(
'Server.url' => 'ASC'
'Server.priority' => 'ASC'
),
);
@ -2112,4 +2112,22 @@ misp.direct_call(relative_path, body)
}
}
}
public function changePriority($id, $direction) {
$this->Server->id = $id;
if (!$this->Server->exists()) {
throw new InvalidArgumentException(__('ID has to be a valid server connection'));
}
if ($direction !== 'up' && $direction !== 'down') {
throw new InvalidArgumentException(__('Invalid direction. Valid options: ', 'up', 'down'));
}
$success = $this->Server->reprioritise($id, $direction);
if ($success) {
$message = __('Priority changed.');
return $this->RestResponse->saveSuccessResponse('Servers', 'changePriority', $message, $this->response->type());
} else {
$message = __('Priority could not be changed.');
return $this->RestResponse->saveFailResponse('Servers', 'resetRemoteAuthKey', $id, $message, $this->response->type());
}
}
}

View File

@ -75,7 +75,7 @@ class AppModel extends Model
13 => false, 14 => false, 15 => false, 18 => false, 19 => false, 20 => false,
21 => false, 22 => false, 23 => false, 24 => false, 25 => false, 26 => false,
27 => false, 28 => false, 29 => false, 30 => false, 31 => false, 32 => false,
33 => false, 34 => false, 35 => false, 36 => false
33 => false, 34 => false, 35 => false, 36 => false, 37 => false
);
public $advanced_updates_description = array(
@ -189,12 +189,23 @@ class AppModel extends Model
case 34:
$this->__fixServerPullPushRules();
break;
case 37:
$this->updateDatabase($command);
$this->__addServerPriority();
break;
default:
$this->updateDatabase($command);
break;
}
}
private function __addServerPriority()
{
$this->Server = ClassRegistry::init('Server');
$this->Server->reprioritise();
return true;
}
private function __addNewFeeds($feeds)
{
$this->Feed = ClassRegistry::init('Feed');
@ -1198,6 +1209,10 @@ class AppModel extends Model
$sqlArray[] = "ALTER TABLE `event_tags` ADD `local` tinyint(1) NOT NULL DEFAULT 0;";
$sqlArray[] = "ALTER TABLE `attribute_tags` ADD `local` tinyint(1) NOT NULL DEFAULT 0;";
break;
case 37:
$sqlArray[] = "ALTER TABLE servers ADD priority int(11) NOT NULL DEFAULT 0;";
$indexArray[] = array('servers', 'priority');
break;
case 'fixNonEmptySharingGroupID':
$sqlArray[] = 'UPDATE `events` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';
$sqlArray[] = 'UPDATE `attributes` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';

View File

@ -4050,7 +4050,10 @@ class Event extends AppModel
if ($passAlong) {
$conditions[] = array('Server.id !=' => $passAlong);
}
$servers = $this->Server->find('all', array('conditions' => $conditions));
$servers = $this->Server->find('all', array(
'conditions' => $conditions,
'order' => array('Server.priority ASC', 'Server.id ASC')
));
// iterate over the servers and upload the event
if (empty($servers)) {
return true;

View File

@ -5287,4 +5287,36 @@ class Server extends AppModel
return __('Could not reset the remote authentication key.');
}
}
public function reprioritise($id = false, $direction = 'up')
{
$servers = $this->find('all', array(
'recursive' => -1,
'order' => array('Server.priority ASC', 'Server.id ASC')
));
$success = true;
if ($id) {
foreach ($servers as $k => $server) {
if ($server['Server']['id'] && $server['Server']['id'] == $id) {
if (
!($k === 0 && $direction === 'up') &&
!(empty($servers[$k+1]) && $direction === 'down')
) {
$temp = $servers[$k];
$destination = $direction === 'up' ? $k-1 : $k+1;
$servers[$k] = $servers[$destination];
$servers[$destination] = $temp;
} else {
return false;
}
}
}
}
foreach ($servers as $k => $server) {
$server['Server']['priority'] = $k + 1;
$success = $success && $this->save($server);
}
return $success;
}
}

View File

@ -20,6 +20,7 @@
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo __('Prio');?></th>
<th><?php echo __('Connection test');?></th>
<th><?php echo __('Reset API key');?></th>
<th><?php echo $this->Paginator->sort('internal');?></th>
@ -38,7 +39,7 @@
<th class="actions"><?php echo __('Actions');?></th>
</tr>
<?php
foreach ($servers as $server):
foreach ($servers as $row_pos => $server):
$rules = array();
$rules['push'] = json_decode($server['Server']['push_rules'], true);
$rules['pull'] = json_decode($server['Server']['pull_rules'], true);
@ -61,8 +62,19 @@ foreach ($servers as $server):
}
}
}
$arrows = '';
foreach (['up', 'down'] as $direction) {
$arrows .= sprintf(
'<i class="fas fa-arrow-circle-%s rearrange-%s" aria-label="%s" title="%s" data-server-id="%s"></i>',
$direction,
$direction,
$direction === 'up' ? __('Move server priority up') : __('Move server priority down'),
$direction === 'up' ? __('Move server priority up') : __('Move server priority down'),
$server['Server']['id']
);
}
?>
<tr>
<tr id="row_<?php echo h($server['Server']['id']); ?>">
<td class="short"><?php echo h($server['Server']['id']); ?></td>
<td>
<?php
@ -70,6 +82,9 @@ foreach ($servers as $server):
else echo h($server['Server']['url']);
?>
</td>
<td id="priority_<?php echo $server['Server']['id'];?>">
<?php echo $arrows;?>
</td>
<td id="connection_test_<?php echo $server['Server']['id'];?>"><span role="button" tabindex="0" aria-label="<?php echo __('Test the connection to the remote instance');?>" title="<?php echo __('Test the connection to the remote instance');?>" class="btn btn-primary" style="line-height:10px; padding: 4px 4px;" onClick="testConnection('<?php echo $server['Server']['id'];?>');"><?php echo __('Run');?></span></td>
<td id="reset_api_key_<?php echo $server['Server']['id'];?>">
<?php
@ -179,6 +194,12 @@ endforeach; ?>
<script type="text/javascript">
$(document).ready(function(){
popoverStartup();
$('.rearrange-up').click(function() {
moveIndexRow($(this).data('server-id'), 'up', '/servers/changePriority');
});
$('.rearrange-down').click(function() {
moveIndexRow($(this).data('server-id'), 'down', '/servers/changePriority');
});
});
</script>
<?php

View File

@ -4395,6 +4395,29 @@ function fetchFormDataAjax(url, callback) {
});
}
function moveIndexRow(id, direction, endpoint) {
var row = $('#row_' + id);
$.ajax({
url: baseurl + endpoint + '/' + id + '/' + direction,
type: 'GET',
success: function(data) {
if (direction === 'up') {
if (row.prev().length) {
row.insertBefore(row.prev());
}
} else {
if (row.next().length) {
row.insertAfter(row.next());
}
}
handleGenericAjaxResponse({'saved':true, 'success':['Server priority changed.']});
},
error: function(data) {
handleGenericAjaxResponse({'saved':false, 'errors':['Something went wrong, could not change the priority as requested.']});
}
});
}
(function(){
"use strict";
$(".datepicker").datepicker({