chg: [sync] Refactor server overlap events fetching

pull/7635/head
Jakub Onderka 2021-08-10 21:04:34 +02:00
parent 748f74a072
commit 6b474f78fc
2 changed files with 71 additions and 23 deletions

View File

@ -5,6 +5,7 @@ class ServerSyncTool
{
const FEATURE_BR = 'br',
FEATURE_GZIP = 'gzip',
FEATURE_ORG_RULE = 'org_rule',
FEATURE_FILTER_SIGHTINGS = 'filter_sightings';
/** @var array */
@ -46,14 +47,25 @@ class ServerSyncTool
*/
public function eventExists(array $event)
{
$exists = $this->socket->head($this->server['Server']['url'] . '/events/view/' . $event['Event']['uuid'], [], $this->request);
$url = $this->server['Server']['url'] . '/events/view/' . $event['Event']['uuid'];
$exists = $this->socket->head($url, [], $this->request);
if ($exists->code == '404') {
return false;
}
if ($exists->code == '200') {
return true;
}
throw new HttpSocketHttpException($exists);
throw new HttpSocketHttpException($exists, $url);
}
/**
* @param array $params
* @throws HttpSocketHttpException
* @throws HttpSocketJsonException
*/
public function eventIndex($params = [])
{
return $this->post('/events/index', $params);
}
/**
@ -129,6 +141,39 @@ class ServerSyncTool
return $this->post('/servers/postTest', ['testString' => $testString]);
}
/**
* @return array
*/
public function server()
{
return $this->server;
}
/**
* @param string $flag
* @return bool
* @throws HttpSocketJsonException
* @throws HttpSocketHttpException
* @throws InvalidArgumentException
*/
public function isSupported($flag)
{
$info = $this->info();
switch ($flag) {
case self::FEATURE_BR:
return isset($info['request_encoding']) && in_array('br', $info['request_encoding'], true);
case self::FEATURE_GZIP:
return isset($info['request_encoding']) && in_array('gzip', $info['request_encoding'], true);
case self::FEATURE_FILTER_SIGHTINGS:
return isset($info['filter_sightings']) && $info['filter_sightings'];
case self::FEATURE_ORG_RULE:
$version = explode('.', $info['version']);
return $version[0] == 2 && $version[1] == 4 && $version[2] > 123;
default:
throw new InvalidArgumentException("Invalid flag `$flag` provided");
}
}
/**
* @params string $url Relative URL
* @return HttpSocketResponseExtended
@ -183,24 +228,4 @@ class ServerSyncTool
}
return $response;
}
/**
* @param string $flag
* @return bool
* @throws HttpSocketJsonException
*/
private function isSupported($flag)
{
$info = $this->info();
switch ($flag) {
case self::FEATURE_BR:
return isset($info['request_encoding']) && in_array('br', $info['request_encoding'], true);
case self::FEATURE_GZIP:
return isset($info['request_encoding']) && in_array('gzip', $info['request_encoding'], true);
case self::FEATURE_FILTER_SIGHTINGS:
return isset($info['filter_sightings']) && $info['filter_sightings'];
default:
throw new InvalidArgumentException("Invalid flag `$flag` provided");
}
}
}

View File

@ -761,6 +761,28 @@ class Server extends AppModel
return $localClusters;
}
/**
* @param ServerSyncTool $serverSync
* @param bool $ignoreFilterRules Ignore defined server pull rules
* @return array
* @throws HttpSocketHttpException
* @throws HttpSocketJsonException
*/
public function getEventIndexFromServer(ServerSyncTool $serverSync, $ignoreFilterRules = false)
{
if (!$ignoreFilterRules) {
$filterRules = $this->filterRuleToParameter($serverSync->server()['Server']['pull_rules']);
if (!empty($filterRules['org']) && !$serverSync->isSupported(ServerSyncTool::FEATURE_ORG_RULE)) {
$filterRules['org'] = implode('|', $filterRules['org']);
}
} else {
$filterRules = [];
}
$filterRules['minimal'] = 1;
$filterRules['published'] = 1;
return $serverSync->eventIndex($filterRules)->json();
}
/**
* Get an array of event UUIDs that are present on the remote server.
*
@ -873,7 +895,8 @@ class Server extends AppModel
$serverUuids = [];
foreach ($servers as &$server) {
try {
$uuids = $this->getEventIdsFromServer($server, true, null, true);
$serverSync = new ServerSyncTool($server, $this->setupSyncRequest($server));
$uuids = array_column($this->getEventIndexFromServer($serverSync, true), 'uuid');
$serverUuids[$server['Server']['id']] = array_flip($uuids);
$server['Server']['events_count'] = count($uuids);
} catch (Exception $e) {