chg: [internal] Use faster fetcher for viewing sightings

pull/6268/head
Jakub Onderka 2020-09-01 17:03:31 +02:00
parent e11f6d0087
commit e7f80fd51d
3 changed files with 16 additions and 10 deletions

View File

@ -3,6 +3,7 @@ App::uses('AppController', 'Controller');
/**
* @property Sighting $Sighting
* @property Event $Event
*/
class SightingsController extends AppController
{
@ -326,28 +327,28 @@ class SightingsController extends AppController
throw new MethodNotAllowedException('Invalid object.');
}
$eventIds = array();
foreach ($object as $k => $v) {
foreach ($object as $v) {
$eventIds[] = $v['Attribute']['event_id'];
}
$events = $this->Event->fetchEvent($this->Auth->user(), array('eventid' => $eventIds, 'metadata' => true));
$events = $this->Event->fetchSimpleEvents($this->Auth->user(), ['conditions' => ['id' => $eventIds]]);
} else {
$attribute_id = false;
// let's set the context to event here, since we reuse the variable later on for some additional lookups.
// Passing $context = 'org' could have interesting results otherwise...
$context = 'event';
$events = $this->Event->fetchEvent($this->Auth->user(), array('eventid' => $id, 'metadata' => true));
$events = $this->Event->fetchSimpleEvents($this->Auth->user(), ['conditions' => ['id' => $id]]);
}
if (empty($events)) {
throw new MethodNotAllowedException('Invalid object.');
}
$results = array();
$raw = array();
foreach ($events as $event) {
$raw = array_merge($raw, $this->Sighting->attachToEvent($event, $this->Auth->user(), $attribute_id));
}
$results = array();
foreach ($raw as $sighting) {
$results[$sighting['type']][date('Ymd', $sighting['date_sighting'])][] = $sighting;
}
unset($raw);
$dataPoints = array();
$startDate = date('Ymd');
$range = date('Ymd', $this->Sighting->getMaximumRange());

View File

@ -1746,7 +1746,13 @@ class Event extends AppModel
return $this->find('first', $params);
}
public function fetchSimpleEvents($user, $params, $includeOrgc = false)
/**
* @param array $user
* @param array $params
* @param bool $includeOrgc
* @return array
*/
public function fetchSimpleEvents(array $user, array $params, $includeOrgc = false)
{
$conditions = $this->createEventConditions($user);
$conditions['AND'][] = $params['conditions'];
@ -1757,8 +1763,7 @@ class Event extends AppModel
if ($includeOrgc) {
$params['contain'] = array('Orgc.name');
}
$results = array_values($this->find('all', $params));
return $results;
return $this->find('all', $params);
}
public function fetchEventIds($user, $from = false, $to = false, $last = false, $list = false, $timestamp = false, $publish_timestamp = false, $eventIdList = false)

View File

@ -207,7 +207,7 @@ class Sighting extends AppModel
/**
* @param array $event
* @param array $user
* @param array|int|null $attribute Attribute array or attribute ID
* @param array|int|null $attribute Attribute model or attribute ID
* @param bool $extraConditions
* @return array|int
*/
@ -220,7 +220,7 @@ class Sighting extends AppModel
$contain = [];
$conditions = array('Sighting.event_id' => $event['Event']['id']);
if (is_array($attribute)) {
if (isset($attribute['Attribute']['id'])) {
$conditions['Sighting.attribute_id'] = $attribute['Attribute']['id'];
} elseif (is_numeric($attribute)) {
$conditions['Sighting.attribute_id'] = $attribute;