chg: [internal] Move getting sightings range to one place

pull/6045/head
Jakub Onderka 2020-06-23 08:38:05 +02:00
parent ea5a819064
commit ce47d32df1
2 changed files with 17 additions and 14 deletions

View File

@ -5480,18 +5480,14 @@ class Event extends AppModel
);
}
public function getSightingData($event)
public function getSightingData(array $event)
{
$this->Sighting = ClassRegistry::init('Sighting');
if (!empty($event['Sighting'])) {
$attributeSightings = array();
$attributeOwnSightings = array();
$attributeSightingsPopover = array();
$sightingsData = array();
$sparklineData = array();
$startDates = array();
$range = (!empty(Configure::read('MISP.Sightings_range')) && is_numeric(Configure::read('MISP.Sightings_range'))) ? Configure::read('MISP.Sightings_range') : 365;
$range = strtotime("-" . $range . " days", time());
$range = $this->Sighting->getMaximumRange();
foreach ($event['Sighting'] as $sighting) {
$type = $this->Sighting->type[$sighting['type']];
if (!isset($sightingsData[$sighting['attribute_id']][$type])) {
@ -5533,22 +5529,21 @@ class Event extends AppModel
}
}
$csv = array();
$today = strtotime(date('Y-m-d', time()));
foreach ($startDates as $k => $v) {
$startDates[$k] = date('Y-m-d', $v);
}
$range = (!empty(Configure::read('MISP.Sightings_range')) && is_numeric(Configure::read('MISP.Sightings_range'))) ? Configure::read('MISP.Sightings_range') : 365;
foreach ($sparklineData as $aid => $data) {
if (!isset($startDates[$aid])) {
continue;
}
$startDate = $startDates[$aid];
if (strtotime($startDate) < strtotime('-' . $range . ' days', time())) {
if (strtotime($startDate) < $range) {
$startDate = date('Y-m-d');
}
$startDate = date('Y-m-d', strtotime("-3 days", strtotime($startDate)));
$to = date('Y-m-d', time());
$sighting = $data;
for ($date = $startDate; strtotime($date) <= strtotime($to); $date = date('Y-m-d', strtotime("+1 day", strtotime($date)))) {
for ($date = $startDate; strtotime($date) <= $today; $date = date('Y-m-d', strtotime("+1 day", strtotime($date)))) {
if (!isset($csv[$aid])) {
$csv[$aid] = 'Date,Close\n';
}

View File

@ -472,9 +472,8 @@ class Sighting extends AppModel
public function getSightingsForTag($user, $tag_id, $sgids = array(), $type = false)
{
$range = (!empty(Configure::read('MISP.Sightings_range')) && is_numeric(Configure::read('MISP.Sightings_range'))) ? Configure::read('MISP.Sightings_range') : 365;
$conditions = array(
'Sighting.date_sighting >' => strtotime("-" . $range . " days"),
'Sighting.date_sighting >' => $this->getMaximumRange(),
'EventTag.tag_id' => $tag_id
);
if ($type !== false) {
@ -511,9 +510,8 @@ class Sighting extends AppModel
public function getSightingsForObjectIds($user, $tagList, $context = 'event', $type = '0')
{
$range = (!empty(Configure::read('MISP.Sightings_range')) && is_numeric(Configure::read('MISP.Sightings_range'))) ? Configure::read('MISP.Sightings_range') : 365;
$conditions = array(
'Sighting.date_sighting >' => strtotime("-" . $range . " days"),
'Sighting.date_sighting >' => $this->getMaximumRange(),
ucfirst($context) . 'Tag.tag_id' => $tagList
);
@ -845,4 +843,14 @@ class Sighting extends AppModel
}
return $saved;
}
/**
* @return int Timestamp
*/
public function getMaximumRange()
{
$rangeInDays = Configure::read('MISP.Sightings_range');
$rangeInDays = (!empty($rangeInDays) && is_numeric($rangeInDays)) ? $rangeInDays : 365;
return strtotime("-$rangeInDays days");
}
}