new: [sync] Previewing a remote instance now passes pagination rules in the request instead of fetching the full data-set and paginating in memory

- fixes issues with empty preview pages
- massive performance boost
- requires the remote side to be the same version or newer
pull/4955/head
iglocska 2019-08-02 14:42:23 +02:00
parent c79ae263eb
commit d6692c44a0
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 20 additions and 5 deletions

View File

@ -110,16 +110,28 @@ class ServersController extends AppController
$combinedArgs['sort'] = 'timestamp';
$combinedArgs['direction'] = 'desc';
}
$events = $this->Server->previewIndex($id, $this->Auth->user(), $combinedArgs);
if (empty($combinedArgs['page'])) {
$combinedArgs['page'] = 1;
}
if (empty($combinedArgs['limit'])) {
$combinedArgs['limit'] = 60;
}
$total_count = 0;
$events = $this->Server->previewIndex($id, $this->Auth->user(), $combinedArgs, $total_count);
$this->loadModel('Event');
$threat_levels = $this->Event->ThreatLevel->find('all');
$this->set('threatLevels', Set::combine($threat_levels, '{n}.ThreatLevel.id', '{n}.ThreatLevel.name'));
App::uses('CustomPaginationTool', 'Tools');
$customPagination = new CustomPaginationTool();
$params = $customPagination->createPaginationRules($events, $this->passedArgs, $this->alias);
if (!empty($total_count)) {
$params['pageCount'] = ceil($total_count / $params['limit']);
}
$this->params->params['paging'] = array($this->modelClass => $params);
if (is_array($events)) {
$customPagination->truncateByPagination($events, $params);
if (count($events) > 60) {
$customPagination->truncateByPagination($events, $params);
}
} else ($events = array());
$this->set('events', $events);
$this->set('eventDescriptions', $this->Event->fieldDescriptions);

View File

@ -4568,7 +4568,7 @@ class Server extends AppModel
* 2: no route to host
* 3: empty result set
*/
public function previewIndex($id, $user, $passedArgs)
public function previewIndex($id, $user, $passedArgs, &$total_count = 0)
{
$server = $this->find('first', array(
'conditions' => array('Server.id' => $id),
@ -4578,7 +4578,7 @@ class Server extends AppModel
}
$HttpSocket = $this->setupHttpSocket($server);
$request = $this->setupSyncRequest($server);
$validArgs = array_merge(array('sort', 'direction'), $this->validEventIndexFilters);
$validArgs = array_merge(array('sort', 'direction', 'page', 'limit'), $this->validEventIndexFilters);
$urlParams = '';
foreach ($validArgs as $v) {
if (isset($passedArgs[$v])) {
@ -4587,6 +4587,10 @@ class Server extends AppModel
}
$uri = $server['Server']['url'] . '/events/index' . $urlParams;
$response = $HttpSocket->get($uri, $data = '', $request);
if (!empty($response->headers['X-Result-Count'])) {
$temp = $response->headers['X-Result-Count'];
$total_count = $temp;
}
if ($response->code == 200) {
try {
$events = json_decode($response->body, true);
@ -4692,7 +4696,6 @@ class Server extends AppModel
}
$validServers[] = $server;
}
return $validServers;
}