chg: [decayingTool] Added model and controller

pull/5032/head
mokaddem 2019-03-08 09:22:37 +01:00
parent 64114199bc
commit 785d83607a
11 changed files with 406 additions and 35 deletions

View File

@ -0,0 +1,119 @@
<?php
App::uses('AppController', 'Controller');
class DecayingModelController extends AppController
{
public $components = array('Security' ,'RequestHandler');
public $paginate = array(
'limit' => 50,
'order' => array(
'DecayingModel.name' => 'asc'
)
);
public function view($id) {
if (!$this->request->is('get')) {
throw new Exception("This method is not allowed");
}
$decayingModel = $this->DecayingModel->checkAuthorisation($this->Auth->user(), $id);
if (!$this->_isSiteAdmin() && !$decModel) {
throw new MethodNotAllowedException('No Decaying Model with the provided ID exists, or you are not authorised to edit it.');
}
$this->set('mayModify', true);
$this->set('id', $id);
$this->set('decayingModel', $decayingModel);
}
public function index() {
$conditions = array();
if (!$this->_isSiteAdmin()) {
$conditions['OR'] = array('org_id' => $this->Auth->user('Organisation')['id']);
}
if (!$this->_isSiteAdmin()) {
$this->paginate = Set::merge($this->paginate, array(
'conditions' => $conditions
));
}
$this->set('decayingModel', $this->paginate());
}
public function add() {
if ($this->request->is('post')) {
if (!isset($this->request->data['DecayingModel']['org_id'])) {
$this->request->data['DecayingModel']['org_id'] = $this->Auth->user()['org_id'];
}
if (empty($this->request->data['DecayingModel']['name'])) {
throw new MethodNotAllowedException("The model must have a name");
}
if ($this->DecayingModel->save($this->request->data)) {
$this->Flash->success('The model has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
public function edit($id) {
$decayingModel = $this->DecayingModel->checkAuthorisation($this->Auth->user(), $id);
if (!$this->_isSiteAdmin() && !$decModel) {
throw new MethodNotAllowedException('No Decaying Model with the provided ID exists, or you are not authorised to edit it.');
}
$this->set('mayModify', true);
if ($this->request->is('post')) {
if (!isset($this->request->data['DecayingModel']['org_id'])) {
$this->request->data['DecayingModel']['org_id'] = $this->Auth->user()['org_id'];
}
$this->request->data['DecayingModel']['id'] = $id;
if ($this->DecayingModel->save($this->request->data)) {
$this->Flash->success('The model has been saved.');
$this->redirect(array('action' => 'index'));
}
}
$this->request->data = $decayingModel;
$this->set('id', $id);
$this->set('decayingModel', $decayingModel);
}
public function delete($id) {
if ($this->request->is('post')) {
$decayingModel = $this->DecayingModel->checkAuthorisation($this->Auth->user(), $id);
if (!$this->_isSiteAdmin() && !$decModel) {
throw new MethodNotAllowedException('No Decaying Model with the provided ID exists, or you are not authorised to edit it.');
}
if ($this->DecayingModel->delete($id, true)) {
$this->Flash->success('Decaying Model deleted.');
$this->redirect(array('action' => 'index'));
} else {
$this->Flash->error('The Decaying Model could not be deleted.');
$this->redirect(array('action' => 'index'));
}
}
}
public function decayingTool() {
$parameters = array(
'Tau' => array('value' => 30, 'step' => 1, 'max' => 365, 'greek' => 'τ', 'unit' => 'days', 'info' => 'Lifetime withouth threshold'),
'Delta' => array('value' => 0.3, 'step' => 0.1, 'max' => 10, 'greek' => 'δ', 'info' => 'Decay speed'),
'Threshold' => array('value' => 30, 'step' => 1, 'info' => 'Cut-off value to expire')
);
$types = $this->User->Event->Attribute->typeDefinitions;
$types = array_filter($types, function($v, $k) {
return $v['to_ids'] == 1;
}, ARRAY_FILTER_USE_BOTH);
ksort($types);
$savedDecayingModels = $this->DecayingModel->fetchAllowedModels($this->Auth->user());
$this->set('parameters', $parameters);
$this->set('types', $types);
$this->set('savedModels', $savedDecayingModels);
}
}

View File

@ -1970,28 +1970,6 @@ class UsersController extends AppController
}
}
public function decayingTool() {
$parameters = array(
'Tau' => array('value' => 30, 'step' => 1, 'max' => 365, 'greek' => 'τ', 'unit' => 'days', 'info' => 'Lifetime withouth threshold'),
'Delta' => array('value' => 0.3, 'step' => 0.1, 'max' => 10, 'greek' => 'δ', 'info' => 'Decay speed'),
'Threshold' => array('value' => 30, 'step' => 1, 'info' => 'Cut-off value to expire')
);
$this->set('parameters', $parameters);
$types = $this->User->Event->Attribute->typeDefinitions;
$types = array_filter($types, function($v, $k) {
return $v['to_ids'] == 1;
}, ARRAY_FILTER_USE_BOTH);
ksort($types);
$this->set('types', $types);
$savedDecayingModels = array(
array(
'name' => 'Model 1',
'parameters' => array('delta' => 0.10, 'tau' => 7, 'threshold' => 20),
)
);
$this->set('savedModels', $savedDecayingModels);
}
public function verifyGPG($full = false)
{
if (!self::_isSiteAdmin()) {

View File

@ -72,7 +72,7 @@ class AppModel extends Model
7 => false, 8 => false, 9 => false, 10 => false, 11 => false, 12 => false,
13 => false, 14 => false, 15 => false, 18 => false, 19 => false, 20 => false,
21 => false, 22 => false, 23 => false, 24 => false, 25 => false, 26 => false,
27 => false, 28 => false, 29 => false, 30 => false
27 => false, 28 => false, 29 => false, 30 => false, 31 => false
);
public function afterSave($created, $options = array())
@ -1096,6 +1096,17 @@ class AppModel extends Model
$sqlArray[] = "ALTER TABLE `galaxies` MODIFY COLUMN `kill_chain_order` text";
$sqlArray[] = "ALTER TABLE `feeds` ADD `force_to_ids` tinyint(1) NOT NULL DEFAULT 0;";
break;
case 31:
$sqlArray[] = "CREATE TABLE IF NOT EXISTS decaying_models (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text,
`parameters` text,
`description` text,
`org_id` int(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
break;
case 'fixNonEmptySharingGroupID':
$sqlArray[] = 'UPDATE `events` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';
$sqlArray[] = 'UPDATE `attributes` SET `sharing_group_id` = 0 WHERE `distribution` != 4;';

View File

@ -0,0 +1,22 @@
<div class="form">
<?php echo $this->Form->create('DecayingModel');?>
<fieldset>
<legend><?php echo __('Add DecayingModel');?></legend>
<?php
echo $this->Form->input('name', array(
));
echo $this->Form->input('description', array(
));
echo $this->Form->input('parameters', array(
));
?>
<div class="clear"></div>
</fieldset>
<?php
echo $this->Form->button(__('Add'), array('class' => 'btn btn-primary'));
echo $this->Form->end();
?>
</div>
<?php
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'add'));
?>

View File

@ -68,9 +68,10 @@
<div class="row">
<div class="span10">
<form class="form-inline">
<input type="text" class="input" placeholder="Model name">
<span class="btn btn-success">Save</span>
<form id="saveForm" class="form-inline">
<input type="text" name="name" class="input" placeholder="Model name">
<textarea rows="1" name="description" class="input" placeholder="Description"></textarea>
<span class="btn btn-success" data-save-type="add" onclick="saveModel(this)"><i class="fa fa-save"> Save</i></span>
</form>
</div>
</div>
@ -81,6 +82,7 @@
<thead>
<tr>
<th rowspan="2">Model Name</th>
<th rowspan="2">Description</th>
<th colspan="3">Parameters</th>
<th rowspan="2">Action</th>
</tr>
@ -93,11 +95,15 @@
<tbody>
<?php foreach ($savedModels as $k => $model): ?>
<tr>
<td><?php echo h($model['name']); ?></td>
<td><?php echo h($model['parameters']['tau']); ?></td>
<td><?php echo h($model['parameters']['delta']); ?></td>
<td><?php echo h($model['parameters']['threshold']); ?></td>
<td><button class="btn btn-primary btn-small" onclick="loadModel(this);" data-parameters="<?php echo json_encode($model['parameters'])?>" ><span class="fa fa-arrow-up"><?php echo __(' Load model') ?></span></button></td>
<td><?php echo h($model['DecayingModel']['name']); ?></td>
<td><?php echo h($model['DecayingModel']['description']); ?></td>
<td><?php echo h($model['DecayingModel']['parameters']['tau']); ?></td>
<td><?php echo h($model['DecayingModel']['parameters']['delta']); ?></td>
<td><?php echo h($model['DecayingModel']['parameters']['threshold']); ?></td>
<td>
<button class="btn btn-primary btn-small" onclick="loadModel(this);"><span class="fa fa-arrow-up"><?php echo __(' Load model') ?></span></button>
<button class="btn btn-danger btn-small" data-save-type="edit" data-model-id="<?php echo h($model['DecayingModel']['id']); ?>" onclick="loadModel(this);"><span class="fa fa-paste"><?php echo __(' Overwrite model') ?></span></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
@ -220,9 +226,9 @@ function loadModel(clicked) {
var $clicked = $(clicked);
var parameters = $clicked.closest('tr').find('td');
parameters = {
tau: parseFloat(parameters[1].innerHTML),
delta: parseFloat(parameters[2].innerHTML),
threshold: parseInt(parameters[3].innerHTML)
tau: parseFloat(parameters[2].innerHTML),
delta: parseFloat(parameters[3].innerHTML),
threshold: parseInt(parameters[4].innerHTML)
};
$('#input_Tau').val(parameters.tau);
@ -235,6 +241,73 @@ function loadModel(clicked) {
chart.update();
}
function saveModel(clicked) {
var $clicked = $(clicked);
var type = $clicked.data('save-type');
var model_id = false;
var data = {};
if (type == 'add') {
var $form = $('#saveForm')
data.name = $form.find('[name="name"]').val();
data.description = $form.find('[name="description"]').val();
var params = {};
params.tau = $('#input_Tau').val();
params.delta = $('#input_Delta').val();
params.threshold = $('#input_Threshold').val();
data.parameters = JSON.stringify(params);
} else {
model_id = $clicked.data('model-id');
}
fetchFormAndSubmit($clicked, type, model_id, data);
}
function injectData($form, data) {
Object.keys(data).forEach(function(k) {
var v = data[k];
var field = k.charAt(0).toUpperCase() + k.slice(1);
$('#DecayingModel'+field).val(v);
});
}
function fetchFormAndSubmit($clicked, type, model_id, formData) {
var url = "/decayingModel/";
if (type == "add") {
url += type;
} else {
url += type + "/" + model_id;
}
var loadingSpan = '<span id="loadingSpan" class="fa fa-spin fa-spinner" style="margin-left: 5px;"></span>';
$.get(url, function(data) {
var $confbox = $("#confirmation_box");
$confbox.html(data);
var $form = $confbox.find('form');
console.log($form);
throw 'dwqd';
injectData($form, formData);
$.ajax({
data: $form.serialize(),
cache: false,
beforeSend: function(XMLHttpRequest) {
$clicked.append(loadingSpan);
},
success: function(data, textStatus) {
showMessage('success', 'Network has been saved');
},
error: function( jqXhr, textStatus, errorThrown ){
showMessage('fail', 'Could not save network');
console.log( errorThrown );
},
complete: function() {
$clicked.find('#loadingSpan').remove();
$form.remove();
},
type: 'post',
url: url
});
});
}
var chart;
$(document).ready(function() {
$('[data-toggle="tooltip"]').tooltip();

View File

@ -0,0 +1,23 @@
<div class="form">
<?php echo $this->Form->create('DecayingModel');?>
<fieldset>
<legend><?php echo __('Add DecayingModel');?></legend>
<?php
echo $this->Form->input('name', array(
));
echo $this->Form->input('description', array(
));
echo $this->Form->input('parameters', array(
"value" => isset($this->request->data['DecayingModel']['parameters']) ? json_encode($this->request->data['DecayingModel']['parameters'], JSON_PRETTY_PRINT) : ''
));
?>
<div class="clear"></div>
</fieldset>
<?php
echo $this->Form->button(__('Edit'), array('class' => 'btn btn-primary'));
echo $this->Form->end();
?>
</div>
<?php
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'edit'));
?>

View File

@ -0,0 +1,81 @@
<div class="templates index">
<h2><?php echo __('Decaying Models');?></h2>
<div class="pagination">
<ul>
<?php
$this->Paginator->options(array(
'update' => '.span12',
'evalScripts' => true,
'before' => '$(".progress").show()',
'complete' => '$(".progress").hide()',
));
echo $this->Paginator->prev('&laquo; ' . __('previous'), array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'prev disabled', 'escape' => false, 'disabledTag' => 'span'));
echo $this->Paginator->numbers(array('modulus' => 20, 'separator' => '', 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'span'));
echo $this->Paginator->next(__('next') . ' &raquo;', array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'next disabled', 'escape' => false, 'disabledTag' => 'span'));
?>
</ul>
</div>
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('org');?></th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo $this->Paginator->sort('description');?></th>
<th><?php echo $this->Paginator->sort('parameters');?></th>
<?php if ($isAclTemplate): ?>
<th class="actions"><?php echo __('Actions');?></th>
<?php endif; ?>
</tr><?php
foreach ($decayingModel as $item): ?>
<tr>
<td class="short"><a href="<?php echo $baseurl."/decayingModel/view/".$item['DecayingModel']['id']; ?>"><?php echo h($item['DecayingModel']['id']); ?>&nbsp;</a></td>
<td class="short">
<?php
echo $this->OrgImg->getOrgImg(array('name' => $item['DecayingModel']['org_id'], 'size' => 24));
?>
&nbsp;
</td>
<td><a href="<?php echo $baseurl."/decayingModel/view/".$item['DecayingModel']['id']; ?>"><?php echo h($item['DecayingModel']['name']); ?>&nbsp;</a></td>
<td><?php echo h($item['DecayingModel']['description']); ?>&nbsp;</td>
<td data-toggle="json" onclick="document.location.href ='<?php echo $baseurl."/decayingModels/view/".$item['DecayingModel']['id']; ?>'"><?php echo json_encode($item['DecayingModel']['parameters']); ?>&nbsp;</td>
<?php if ($isAclTemplate): ?>
<td class="short action-links">
<?php echo $this->Html->link('', array('action' => 'view', $item['DecayingModel']['id']), array('class' => 'icon-list-alt', 'title' => 'View'));?>
<?php echo $this->Html->link('', array('action' => 'edit', $item['DecayingModel']['id']), array('class' => 'icon-edit', 'title' => 'Edit'));?>
<?php echo $this->Form->postLink('', array('action' => 'delete', $item['DecayingModel']['id']), array('class' => 'icon-trash', 'title' => 'Delete'), __('Are you sure you want to delete DecayingModel #' . $item['DecayingModel']['id'] . '?'));?>
</td>
<?php endif; ?>
</tr><?php
endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?>
</p>
<div class="pagination">
<ul>
<?php
echo $this->Paginator->prev('&laquo; ' . __('previous'), array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'prev disabled', 'escape' => false, 'disabledTag' => 'span'));
echo $this->Paginator->numbers(array('modulus' => 20, 'separator' => '', 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'span'));
echo $this->Paginator->next(__('next') . ' &raquo;', array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'next disabled', 'escape' => false, 'disabledTag' => 'span'));
?>
</ul>
</div>
</div>
<script>
$(document).ready(function() {
$('[data-toggle="json"]').each(function(i) {
var parsedJson = syntaxHighlightJson($(this).text().trim());
$(this).html(parsedJson);
});
});
</script>
<?php
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'index'));
?>

View File

@ -0,0 +1,2 @@
<?php
echo (json_encode($decayingModel));

View File

@ -0,0 +1,35 @@
<?php
$table_data = array();
$table_data[] = array('key' => __('Model ID'), 'value' => $decayingModel['DecayingModel']['id']);
$table_data[] = array(
'key' => __('Creator org'),
'html' => sprintf(
'<a href="%s/organisations/view/%s">%s</a>',
$baseurl,
h($decayingModel['DecayingModel']['org_id']),
h($decayingModel['DecayingModel']['org_id'])
)
);
$table_data[] = array('key' => __('Name'), 'value' => $decayingModel['DecayingModel']['name']);
$table_data[] = array('key' => __('Description'), 'value' => $decayingModel['DecayingModel']['description']);
$table_data[] = array('key' => __('Parameters'), 'value' => json_encode($decayingModel['DecayingModel']['parameters']), 'class' => 'json-transform');
?>
<div class='view'>
<div class="row-fluid">
<div class="span8">
<?php echo $this->element('genericElements/viewMetaTable', array('table_data' => $table_data)); ?>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('td.meta_table_value.json-transform').each(function(i) {
var parsedJson = syntaxHighlightJson($(this).text().trim());
$(this).html(parsedJson);
});
});
</script>
<?php
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'view'));
?>

View File

@ -856,6 +856,33 @@
}
break;
case 'decayingModel':
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'url' => '/decayingModel/index',
'text' => __('List Decaying Models')
));
if ($isSiteAdmin || $isAclTemplate) {
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'url' => '/decayingModel/add',
'text' => __('Add Decaying Model')
));
}
if (($menuItem === 'view' || $menuItem === 'edit')) {
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'element_id' => 'view',
'url' => '/decayingModel/view/' . h($id),
'text' => __('View Decaying Model')
));
if ($mayModify) {
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'element_id' => 'edit',
'url' => '/decayingModel/edit/' . h($id),
'text' => __('Edit Decaying Model')
));
}
}
break;
case 'feeds':
echo $this->element('/genericElements/SideMenu/side_menu_link', array(
'url' => '/feeds/index',

View File

@ -270,7 +270,7 @@
),
array(
'text' => __('Decaying Rules'),
'url' => '/users/decayingTool',
'url' => '/decayingModel/decayingTool',
),
array(
'type' => 'separator',