mirror of https://github.com/MISP/MISP
chg: [internal] Faster loading od Distribution graph
parent
c89df1d39d
commit
6770ba8222
|
@ -4357,16 +4357,17 @@ class EventsController extends AppController
|
||||||
return new CakeResponse(array('body' => json_encode($json), 'status' => 200, 'type' => 'json'));
|
return new CakeResponse(array('body' => json_encode($json), 'status' => 200, 'type' => 'json'));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function genDistributionGraph($id, $type = 'event', $extended = 0) {
|
private function genDistributionGraph($id, $type = 'event', $extended = 0)
|
||||||
|
{
|
||||||
$validTools = array('event');
|
$validTools = array('event');
|
||||||
if (!in_array($type, $validTools)) {
|
if (!in_array($type, $validTools)) {
|
||||||
throw new MethodNotAllowedException(__('Invalid type.'));
|
throw new MethodNotAllowedException(__('Invalid type.'));
|
||||||
}
|
}
|
||||||
$this->loadModel('Server');
|
|
||||||
$this->loadModel('Organisation');
|
|
||||||
App::uses('DistributionGraphTool', 'Tools');
|
App::uses('DistributionGraphTool', 'Tools');
|
||||||
$grapher = new DistributionGraphTool();
|
$grapher = new DistributionGraphTool();
|
||||||
|
|
||||||
|
$this->loadModel('Server');
|
||||||
$servers = $this->Server->find('list', array(
|
$servers = $this->Server->find('list', array(
|
||||||
'fields' => array('name'),
|
'fields' => array('name'),
|
||||||
));
|
));
|
||||||
|
|
|
@ -1,223 +1,221 @@
|
||||||
<?php
|
<?php
|
||||||
class DistributionGraphTool
|
class DistributionGraphTool
|
||||||
|
{
|
||||||
|
private $__user = false;
|
||||||
|
private $__json = array();
|
||||||
|
/** @var Event */
|
||||||
|
private $__eventModel;
|
||||||
|
/** @var Organisation */
|
||||||
|
private $__organisationModel;
|
||||||
|
|
||||||
|
public function construct(Event $eventModel, array $servers, array $user, $extended_view=0)
|
||||||
{
|
{
|
||||||
private $__user = false;
|
$this->__eventModel = $eventModel;
|
||||||
private $__json = array();
|
$this->__serverList = $servers;
|
||||||
private $__eventModel = false;
|
$this->__organisationModel = $eventModel->Orgc;
|
||||||
|
$this->__user = $user;
|
||||||
|
$this->__json = array();
|
||||||
|
$this->__extended_view = $extended_view;
|
||||||
|
|
||||||
public function construct($eventModel, $servers, $user, $extended_view=0)
|
// construct distribution info
|
||||||
{
|
$this->__json['distributionInfo'] = array();
|
||||||
$this->__eventModel = $eventModel;
|
$sgs = $this->__eventModel->SharingGroup->fetchAllAuthorised($this->__user, 'simplified', 1);
|
||||||
$this->__serverList = $servers;
|
$this->__json['allSharingGroup'] = h($sgs);
|
||||||
$this->__organisationModel = $eventModel->Orgc;
|
$distributionLevels = $this->__eventModel->distributionLevels;
|
||||||
$this->__user = $user;
|
foreach ($distributionLevels as $key => $value) {
|
||||||
$this->__json = array();
|
$this->__json['distributionInfo'][$key] = array('key' => h($value), 'desc' => h($this->__eventModel->distributionDescriptions[$key]['formdesc']), 'value' => h($key));
|
||||||
$this->__extended_view = $extended_view;
|
}
|
||||||
|
$this->__json['distributionInfo'][5] = ""; // inherit event. Will be deleted afterward
|
||||||
|
|
||||||
// construct distribution info
|
return true;
|
||||||
$this->__json['distributionInfo'] = array();
|
}
|
||||||
$sgs = $this->__eventModel->SharingGroup->fetchAllAuthorised($this->__user, 'simplified', 1);
|
|
||||||
$this->__json['allSharingGroup'] = h($sgs);
|
private function __fetchAndAddDistributionInfo($elem)
|
||||||
$distributionLevels = $this->__eventModel->distributionLevels;
|
{
|
||||||
foreach ($distributionLevels as $key => $value) {
|
$distributionLevel = $elem['distribution'];
|
||||||
$this->__json['distributionInfo'][$key] = array('key' => h($value), 'desc' => h($this->__eventModel->distributionDescriptions[$key]['formdesc']), 'value' => h($key));
|
|
||||||
|
if ($distributionLevel == 5) { // inherit -> convert it to the event distribution level
|
||||||
|
$elem['distribution'] = $this->__eventDistribution;
|
||||||
|
$this->__fetchAndAddDistributionInfo($elem);
|
||||||
|
} elseif ($distributionLevel == 4) { // sharing group
|
||||||
|
if (isset($elem['SharingGroup'])) {
|
||||||
|
$sg_name = $elem['SharingGroup']['name'];
|
||||||
|
$this->__addAdditionalDistributionInfo($distributionLevel, $sg_name);
|
||||||
|
} elseif ($this->__eventDistribution == 4) { // event is distributed for sg
|
||||||
|
$sg_name = $this->__eventSharingGroupName;
|
||||||
|
$this->__addAdditionalDistributionInfo($distributionLevel, $sg_name);
|
||||||
}
|
}
|
||||||
$this->__json['distributionInfo'][5] = ""; // inherit event. Will be deleted afterward
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
private function __addAdditionalDistributionInfo($distributionLevel, $data)
|
||||||
|
{
|
||||||
|
if (empty($this->__json['additionalDistributionInfo'][$distributionLevel])) {
|
||||||
|
$this->__json['additionalDistributionInfo'][$distributionLevel] = array();
|
||||||
|
}
|
||||||
|
$this->__json['additionalDistributionInfo'][$distributionLevel][h($data)] = 0; // set-alike
|
||||||
|
if ($distributionLevel == 4) {
|
||||||
|
if (!isset($this->__json['sharingGroupRepartition'][h($data)])) {
|
||||||
|
$this->__json['sharingGroupRepartition'][h($data)] = 0;
|
||||||
|
}
|
||||||
|
$this->__json['sharingGroupRepartition'][h($data)]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function __addOtherDistributionInfo()
|
||||||
|
{
|
||||||
|
// all comm
|
||||||
|
$this->__addAdditionalDistributionInfo(3, "This community"); // add current community
|
||||||
|
$this->__addAdditionalDistributionInfo(3, "All other communities"); // add current community
|
||||||
|
|
||||||
|
// connected
|
||||||
|
$servers = $this->__serverList;
|
||||||
|
$this->__addAdditionalDistributionInfo(2, "This community"); // add current community
|
||||||
|
foreach ($servers as $server) {
|
||||||
|
$this->__addAdditionalDistributionInfo(2, $server);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function __extract_sharing_groups_names($sharingArray)
|
// community
|
||||||
{
|
$orgs = $this->__organisationModel->find('list', array(
|
||||||
return $sharingArray['name'];
|
'fields' => array('name'),
|
||||||
}
|
'conditions' => array('local' => true)
|
||||||
|
));
|
||||||
private function __fetchAndAddDistributionInfo($elem)
|
$thisOrg = $this->__user['Organisation']['name'];
|
||||||
{
|
$this->__addAdditionalDistributionInfo(1, $thisOrg); // add current community
|
||||||
$distributionLevel = $elem['distribution'];
|
foreach ($orgs as $org) {
|
||||||
|
if ($thisOrg != $org) {
|
||||||
if ($distributionLevel == 5) { // inherit -> convert it to the event distribution level
|
$this->__addAdditionalDistributionInfo(1, $org);
|
||||||
$elem['distribution'] = $this->__eventDistribution;
|
|
||||||
$this->__fetchAndAddDistributionInfo($elem);
|
|
||||||
} elseif ($distributionLevel == 4) { // sharing group
|
|
||||||
if (isset($elem['SharingGroup'])) {
|
|
||||||
$sg_name = $this->__extract_sharing_groups_names($elem['SharingGroup']);
|
|
||||||
$this->__addAdditionalDistributionInfo($distributionLevel, $sg_name);
|
|
||||||
} elseif ($this->__eventDistribution == 4) { // event is distributed for sg
|
|
||||||
$sg_name = $this->__eventSharingGroupName;
|
|
||||||
$this->__addAdditionalDistributionInfo($distributionLevel, $sg_name);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function __addAdditionalDistributionInfo($distributionLevel, $data)
|
|
||||||
{
|
|
||||||
if (empty($this->__json['additionalDistributionInfo'][$distributionLevel])) {
|
|
||||||
$this->__json['additionalDistributionInfo'][$distributionLevel] = array();
|
|
||||||
}
|
|
||||||
$this->__json['additionalDistributionInfo'][$distributionLevel][h($data)] = 0; // set-alike
|
|
||||||
if ($distributionLevel == 4) {
|
|
||||||
if (!isset($this->__json['sharingGroupRepartition'][h($data)])) {
|
|
||||||
$this->__json['sharingGroupRepartition'][h($data)] = 0;
|
|
||||||
}
|
|
||||||
$this->__json['sharingGroupRepartition'][h($data)]++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function __addOtherDistributionInfo()
|
// org only
|
||||||
{
|
$thisOrg = $this->__user['Organisation']['name'];
|
||||||
// all comm
|
$this->__addAdditionalDistributionInfo(0, $thisOrg); // add current community
|
||||||
$this->__addAdditionalDistributionInfo(3, "This community"); // add current community
|
}
|
||||||
$this->__addAdditionalDistributionInfo(3, "All other communities"); // add current community
|
|
||||||
|
|
||||||
// connected
|
|
||||||
$servers = $this->__serverList;
|
|
||||||
$this->__addAdditionalDistributionInfo(2, "This community"); // add current community
|
|
||||||
foreach ($servers as $server) {
|
|
||||||
$this->__addAdditionalDistributionInfo(2, $server);
|
|
||||||
}
|
|
||||||
|
|
||||||
// community
|
|
||||||
$orgs = $this->__organisationModel->find('list', array(
|
|
||||||
'fields' => array('name'),
|
|
||||||
'conditions' => array('local' => true)
|
|
||||||
));
|
|
||||||
$thisOrg = $this->__user['Organisation']['name'];
|
|
||||||
$this->__addAdditionalDistributionInfo(1, $thisOrg); // add current community
|
|
||||||
foreach ($orgs as $org) {
|
|
||||||
if ($thisOrg != $org) {
|
|
||||||
$this->__addAdditionalDistributionInfo(1, $org);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// org only
|
|
||||||
$thisOrg = $this->__user['Organisation']['name'];
|
|
||||||
$this->__addAdditionalDistributionInfo(0, $thisOrg); // add current community
|
|
||||||
}
|
|
||||||
|
|
||||||
private function __get_event($id)
|
|
||||||
{
|
|
||||||
$fullevent = $this->__eventModel->fetchEvent($this->__user, array('eventid' => $id, 'flatten' => 0, 'includeTagRelations' => 1, 'extended' => $this->__extended_view));
|
|
||||||
$event = array();
|
|
||||||
if (empty($fullevent)) {
|
|
||||||
return $event;
|
|
||||||
}
|
|
||||||
|
|
||||||
$fullevent = $fullevent[0];
|
|
||||||
if (!empty($fullevent['Object'])) {
|
|
||||||
$event['Object'] = $fullevent['Object'];
|
|
||||||
} else {
|
|
||||||
$event['Object'] = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($fullevent['Attribute'])) {
|
|
||||||
$event['Attribute'] = $fullevent['Attribute'];
|
|
||||||
} else {
|
|
||||||
$event['Attribute'] = array();
|
|
||||||
}
|
|
||||||
$event['distribution'] = $fullevent['Event']['distribution'];
|
|
||||||
|
|
||||||
if (isset($fullevent['SharingGroup'])) {
|
|
||||||
$event['SharingGroupName'] = $fullevent['SharingGroup']['name'];
|
|
||||||
} else {
|
|
||||||
$event['SharingGroupName'] = "?";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch event containing just 'Attribute', 'Object', 'SharingGroup' and 'distribution'
|
||||||
|
* @param int $id
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function __get_event($id)
|
||||||
|
{
|
||||||
|
$fullevent = $this->__eventModel->fetchEvent($this->__user, array(
|
||||||
|
'eventid' => $id,
|
||||||
|
'flatten' => 0,
|
||||||
|
'noShadowAttributes' => true,
|
||||||
|
'noEventReports' => true,
|
||||||
|
'noSightings' => true,
|
||||||
|
'extended' => $this->__extended_view,
|
||||||
|
));
|
||||||
|
$event = array();
|
||||||
|
if (empty($fullevent)) {
|
||||||
return $event;
|
return $event;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_distributions_graph($id)
|
$fullevent = $fullevent[0];
|
||||||
{
|
if (isset($fullevent['Object'])) {
|
||||||
$this->__json['event'] = $this->init_array_distri();
|
$event['Object'] = $fullevent['Object'];
|
||||||
$this->__json['attribute'] = $this->init_array_distri();
|
} else {
|
||||||
$this->__json['object'] = $this->init_array_distri();
|
$event['Object'] = array();
|
||||||
$this->__json['obj_attr'] = $this->init_array_distri();
|
}
|
||||||
$this->__json['additionalDistributionInfo'] = $this->init_array_distri(array());
|
|
||||||
$this->__json['sharingGroupRepartition'] = array();
|
|
||||||
|
|
||||||
$this->__addOtherDistributionInfo();
|
if (isset($fullevent['Attribute'])) {
|
||||||
|
$event['Attribute'] = $fullevent['Attribute'];
|
||||||
|
} else {
|
||||||
|
$event['Attribute'] = array();
|
||||||
|
}
|
||||||
|
$event['distribution'] = $fullevent['Event']['distribution'];
|
||||||
|
|
||||||
// transform set into array
|
if (isset($fullevent['SharingGroup'])) {
|
||||||
foreach (array_keys($this->__json['additionalDistributionInfo']) as $d) {
|
$event['SharingGroupName'] = $fullevent['SharingGroup']['name'];
|
||||||
$this->__json['additionalDistributionInfo'][$d] = array_keys($this->__json['additionalDistributionInfo'][$d]);
|
} else {
|
||||||
}
|
$event['SharingGroupName'] = "?";
|
||||||
|
}
|
||||||
|
|
||||||
if ($id === -1) {
|
return $event;
|
||||||
return $this->__json;
|
}
|
||||||
}
|
|
||||||
$event = $this->__get_event($id);
|
|
||||||
$eventDist = $event['distribution'];
|
|
||||||
$eventSGName = $event['SharingGroupName'];
|
|
||||||
$this->__eventDistribution = $eventDist;
|
|
||||||
$this->__eventSharingGroupName = $eventSGName;
|
|
||||||
|
|
||||||
if (empty($event)) {
|
public function get_distributions_graph($id)
|
||||||
return $this->__json;
|
{
|
||||||
}
|
$this->__json['event'] = $this->init_array_distri();
|
||||||
|
$this->__json['attribute'] = $this->init_array_distri();
|
||||||
|
$this->__json['object'] = $this->init_array_distri();
|
||||||
|
$this->__json['obj_attr'] = $this->init_array_distri();
|
||||||
|
$this->__json['additionalDistributionInfo'] = $this->init_array_distri(array());
|
||||||
|
$this->__json['sharingGroupRepartition'] = array();
|
||||||
|
|
||||||
if (!empty($event['Object'])) {
|
$this->__addOtherDistributionInfo();
|
||||||
$object = $event['Object'];
|
|
||||||
} else {
|
|
||||||
$object = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($event['Attribute'])) {
|
// transform set into array
|
||||||
$attribute = $event['Attribute'];
|
foreach (array_keys($this->__json['additionalDistributionInfo']) as $d) {
|
||||||
} else {
|
$this->__json['additionalDistributionInfo'][$d] = array_keys($this->__json['additionalDistributionInfo'][$d]);
|
||||||
$attribute = array();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// extract distribution
|
|
||||||
foreach ($attribute as $attr) {
|
|
||||||
$distri = $attr['distribution'];
|
|
||||||
$this->__json['event'][$distri] += 1;
|
|
||||||
$this->__json['attribute'][$distri] += 1;
|
|
||||||
$this->__fetchAndAddDistributionInfo($attr);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($object as $obj) {
|
|
||||||
$distri = $obj['distribution'];
|
|
||||||
$this->__json['event'][$distri] += 1;
|
|
||||||
$this->__json['object'][$distri] += 1;
|
|
||||||
$this->__fetchAndAddDistributionInfo($obj);
|
|
||||||
|
|
||||||
$added_value = array();
|
|
||||||
if (!empty($obj['Attribute'])) {
|
|
||||||
foreach ($obj['Attribute'] as $objAttr) {
|
|
||||||
$distri = $objAttr['distribution'];
|
|
||||||
$this->__json['event'][$distri] += 1;
|
|
||||||
$this->__json['obj_attr'][$distri] += 1;
|
|
||||||
$this->__fetchAndAddDistributionInfo($objAttr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// distribution 5 is inherit event, apply this fact on values
|
|
||||||
$this->__json['event'][$eventDist] += $this->__json['event'][5];
|
|
||||||
unset($this->__json['event'][5]);
|
|
||||||
$this->__json['attribute'][$eventDist] += $this->__json['attribute'][5];
|
|
||||||
unset($this->__json['attribute'][5]);
|
|
||||||
$this->__json['object'][$eventDist] += $this->__json['object'][5];
|
|
||||||
unset($this->__json['object'][5]);
|
|
||||||
$this->__json['obj_attr'][$eventDist] += $this->__json['obj_attr'][5];
|
|
||||||
unset($this->__json['obj_attr'][5]);
|
|
||||||
|
|
||||||
unset($this->__json['distributionInfo'][5]); // inherit event.
|
|
||||||
|
|
||||||
// transform set into array for SG (others are already done)
|
|
||||||
$this->__json['additionalDistributionInfo'][4] = array_keys($this->__json['additionalDistributionInfo'][4]);
|
|
||||||
|
|
||||||
|
if ($id === -1) {
|
||||||
|
return $this->__json;
|
||||||
|
}
|
||||||
|
$event = $this->__get_event($id);
|
||||||
|
if (empty($event)) {
|
||||||
return $this->__json;
|
return $this->__json;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init_array_distri($default=0)
|
$eventDist = $event['distribution'];
|
||||||
{
|
$eventSGName = $event['SharingGroupName'];
|
||||||
$ret = array();
|
$this->__eventDistribution = $eventDist;
|
||||||
foreach ($this->__json['distributionInfo'] as $d => $v) {
|
$this->__eventSharingGroupName = $eventSGName;
|
||||||
$ret[h($d)] = $default;
|
|
||||||
}
|
// extract distribution
|
||||||
return $ret;
|
foreach ($event['Attribute'] as $attr) {
|
||||||
|
$distri = $attr['distribution'];
|
||||||
|
$this->__json['event'][$distri] += 1;
|
||||||
|
$this->__json['attribute'][$distri] += 1;
|
||||||
|
$this->__fetchAndAddDistributionInfo($attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($event['Object'] as $obj) {
|
||||||
|
$distri = $obj['distribution'];
|
||||||
|
$this->__json['event'][$distri] += 1;
|
||||||
|
$this->__json['object'][$distri] += 1;
|
||||||
|
$this->__fetchAndAddDistributionInfo($obj);
|
||||||
|
|
||||||
|
if (!empty($obj['Attribute'])) {
|
||||||
|
foreach ($obj['Attribute'] as $objAttr) {
|
||||||
|
$distri = $objAttr['distribution'];
|
||||||
|
$this->__json['event'][$distri] += 1;
|
||||||
|
$this->__json['obj_attr'][$distri] += 1;
|
||||||
|
$this->__fetchAndAddDistributionInfo($objAttr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// distribution 5 is inherit event, apply this fact on values
|
||||||
|
$this->__json['event'][$eventDist] += $this->__json['event'][5];
|
||||||
|
unset($this->__json['event'][5]);
|
||||||
|
$this->__json['attribute'][$eventDist] += $this->__json['attribute'][5];
|
||||||
|
unset($this->__json['attribute'][5]);
|
||||||
|
$this->__json['object'][$eventDist] += $this->__json['object'][5];
|
||||||
|
unset($this->__json['object'][5]);
|
||||||
|
$this->__json['obj_attr'][$eventDist] += $this->__json['obj_attr'][5];
|
||||||
|
unset($this->__json['obj_attr'][5]);
|
||||||
|
|
||||||
|
unset($this->__json['distributionInfo'][5]); // inherit event.
|
||||||
|
|
||||||
|
// transform set into array for SG (others are already done)
|
||||||
|
$this->__json['additionalDistributionInfo'][4] = array_keys($this->__json['additionalDistributionInfo'][4]);
|
||||||
|
|
||||||
|
return $this->__json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function init_array_distri($default=0)
|
||||||
|
{
|
||||||
|
$ret = array();
|
||||||
|
foreach ($this->__json['distributionInfo'] as $d => $v) {
|
||||||
|
$ret[h($d)] = $default;
|
||||||
|
}
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue