chg: [internal] Faster attaching clusters to events/attributes

pull/8297/head
Jakub Onderka 2022-04-24 14:53:43 +02:00
parent 2150fa4ef7
commit 9b8b1514ef
4 changed files with 61 additions and 70 deletions

View File

@ -529,7 +529,6 @@ class GalaxiesController extends AppController
public function attachMultipleClusters($target_id, $target_type = 'event')
{
$local = !empty($this->params['named']['local']);
$this->set('local', $local);
if ($this->request->is('post')) {
if ($target_id === 'selected') {
$target_id_list = json_decode($this->request->data['Galaxy']['attribute_ids']);
@ -561,6 +560,7 @@ class GalaxiesController extends AppController
$this->redirect($this->referer());
}
} else {
$this->set('local', $local);
$this->set('target_id', $target_id);
$this->set('target_type', $target_type);
$this->layout = false;

View File

@ -353,14 +353,19 @@ class Galaxy extends AppModel
return $results;
}
public function attachCluster($user, $target_type, $target_id, $cluster_id, $local = false)
/**
* @param array $user
* @param string $target_type
* @param int $target_id
* @param int $cluster_id
* @param bool $local
* @return string
* @throws Exception
*/
public function attachCluster(array $user, $target_type, $target_id, $cluster_id, $local = false)
{
$connectorModel = Inflector::camelize($target_type) . 'Tag';
if ($local == 1 || $local === true) {
$local = 1;
} else {
$local = 0;
}
$local = $local == 1 || $local === true ? 1 : 0;
$cluster_alias = $this->GalaxyCluster->alias;
$galaxy_alias = $this->alias;
$cluster = $this->GalaxyCluster->fetchGalaxyClusters($user, array(
@ -368,30 +373,32 @@ class Galaxy extends AppModel
'conditions' => array("${cluster_alias}.id" => $cluster_id),
'contain' => array('Galaxy'),
'fields' => array('tag_name', 'id', 'value', "${galaxy_alias}.local_only"),
), $full=false);
));
if (empty($cluster)) {
throw new NotFoundException(__('Invalid Galaxy cluster'));
}
$local_only = $cluster['GalaxyCluster']['Galaxy']['local_only'];
if ($local_only && !$local) {
throw new MethodNotAllowedException(__("This Cluster can only be attached in a local scope"));
}
$this->Tag = ClassRegistry::init('Tag');
if ($target_type === 'event') {
$target = $this->Tag->EventTag->Event->fetchEvent($user, array('eventid' => $target_id, 'metadata' => 1));
$target = $this->Tag->EventTag->Event->fetchSimpleEvent($user, $target_id);
} elseif ($target_type === 'attribute') {
$target = $this->Tag->AttributeTag->Attribute->fetchAttributes($user, array('conditions' => array('Attribute.id' => $target_id), 'flatten' => 1));
$target = $this->Tag->AttributeTag->Attribute->fetchAttributeSimple($user, array('conditions' => array('Attribute.id' => $target_id)));
} elseif ($target_type === 'tag_collection') {
$target = $this->Tag->TagCollectionTag->TagCollection->fetchTagCollection($user, array('conditions' => array('TagCollection.id' => $target_id)));
if (!empty($target)) {
$target = $target[0];
}
}
if (empty($target)) {
throw new NotFoundException(__('Invalid %s.', $target_type));
}
$target = $target[0];
$local_only = $cluster['GalaxyCluster']['Galaxy']['local_only'];
if ($local_only && !$local) {
throw new MethodNotAllowedException(__("This Cluster can only be attached in a local scope"));
}
$tag_id = $this->Tag->captureTag(array('name' => $cluster['GalaxyCluster']['tag_name'], 'colour' => '#0088cc', 'exportable' => 1, 'local_only' => $local_only), $user, true);
$existingTag = $this->Tag->$connectorModel->find('first', array('conditions' => array($target_type . '_id' => $target_id, 'tag_id' => $tag_id)));
if (!empty($existingTag)) {
$existingTag = $this->Tag->$connectorModel->hasAny(array($target_type . '_id' => $target_id, 'tag_id' => $tag_id));
if ($existingTag) {
return 'Cluster already attached.';
}
$this->Tag->$connectorModel->create();
@ -428,17 +435,8 @@ class Galaxy extends AppModel
$event['Event']['timestamp'] = $date->getTimestamp();
$this->Tag->EventTag->Event->save($event);
}
$this->Log = ClassRegistry::init('Log');
$this->Log->create();
$this->Log->save(array(
'org' => $user['Organisation']['name'],
'model' => ucfirst($target_type),
'model_id' => $target_id,
'email' => $user['email'],
'action' => 'galaxy',
'title' => 'Attached ' . $cluster['GalaxyCluster']['value'] . ' (' . $cluster['GalaxyCluster']['id'] . ') to ' . $target_type . ' (' . $target_id . ')',
'change' => ''
));
$logTitle = 'Attached ' . $cluster['GalaxyCluster']['value'] . ' (' . $cluster['GalaxyCluster']['id'] . ') to ' . $target_type . ' (' . $target_id . ')';
$this->loadLog()->createLogEntry($user, 'galaxy', ucfirst($target_type), $target_id, $logTitle);
return 'Cluster attached.';
}
return 'Could not attach the cluster';

View File

@ -1,12 +1,12 @@
<?php
$url = sprintf(
'/galaxies/attachMultipleClusters/%s/%s/local:%s',
$target_id,
$target_type,
$local ? '1' : '0'
);
echo $this->Form->create('Galaxy', array('url' => $url, 'style' => 'margin:0px;'));
echo $this->Form->input('target_ids', array('type' => 'text'));
echo $this->Form->input('attribute_ids', array('style' => 'display:none;', 'label' => false));
echo $this->Form->end();
?>
$url = sprintf(
'/galaxies/attachMultipleClusters/%s/%s/local:%s',
$target_id,
$target_type,
$local ? '1' : '0'
);
echo $this->Form->create('Galaxy', array('url' => $url, 'style' => 'margin:0px;'));
echo $this->Form->input('target_ids', array('type' => 'text'));
echo $this->Form->input('attribute_ids', array('style' => 'display:none;', 'label' => false));
echo $this->Form->end();

View File

@ -647,25 +647,23 @@ function quickSubmitTagForm(selected_tag_ids, addData) {
}
var url = baseurl + "/events/addTag/" + event_id + localFlag;
fetchFormDataAjax(url, function(formData) {
$('body').append($('<div id="temp"/>').html(formData));
$('#temp #EventTag').val(JSON.stringify(selected_tag_ids));
var $formData = $(formData);
$formData.find('#EventTag').val(JSON.stringify(selected_tag_ids));
xhr({
data: $('#EventAddTagForm').serialize(),
data: $formData.serialize(),
success: function (data) {
loadEventTags(event_id);
loadGalaxies(event_id, 'event');
handleGenericAjaxResponse(data);
},
error: function() {
showMessage('fail', 'Could not add tag.');
loadEventTags(event_id);
loadGalaxies(event_id, 'event');
},
complete: function() {
loadEventTags(event_id);
loadGalaxies(event_id, 'event');
$("#popover_form").fadeOut();
$("#gray_out").fadeOut();
$(".loading").hide();
$('#temp').remove();
},
type: "post",
url: url
@ -681,13 +679,13 @@ function quickSubmitAttributeTagForm(selected_tag_ids, addData) {
}
var url = baseurl + "/attributes/addTag/" + attribute_id + localFlag;
fetchFormDataAjax(url, function(formData) {
$('body').append($('<div id="temp"/>').html(formData));
$('#temp #AttributeTag').val(JSON.stringify(selected_tag_ids));
if (attribute_id == 'selected') {
$('#AttributeAttributeIds').val(getSelected());
var $formData = $(formData);
$formData.find('#AttributeTag').val(JSON.stringify(selected_tag_ids));
if (attribute_id === 'selected') {
$formData.find('#AttributeAttributeIds').val(getSelected());
}
xhr({
data: $('#AttributeAddTagForm').serialize(),
data: $formData.serialize(),
success:function (data) {
if (attribute_id == 'selected') {
updateIndex(0, 'event');
@ -706,7 +704,6 @@ function quickSubmitAttributeTagForm(selected_tag_ids, addData) {
$("#popover_form").fadeOut();
$("#gray_out").fadeOut();
$(".loading").hide();
$('#temp').remove();
},
type:"post",
url: url
@ -4177,37 +4174,34 @@ function quickSubmitGalaxyForm(cluster_ids, additionalData) {
var local = additionalData['local'];
var url = baseurl + "/galaxies/attachMultipleClusters/" + target_id + "/" + scope + "/local:" + local;
fetchFormDataAjax(url, function(formData) {
$('body').append($('<div id="temp"/>').html(formData));
$('#temp #GalaxyTargetIds').val(JSON.stringify(cluster_ids));
if (target_id == 'selected') {
$('#AttributeAttributeIds, #GalaxyAttributeIds').val(getSelected());
var $formData = $(formData);
$formData.find("#GalaxyTargetIds").val(JSON.stringify(cluster_ids));
if (target_id === 'selected') {
$formData.find('#GalaxyAttributeIds').val(getSelected());
}
$.ajax({
data: $('#GalaxyAttachMultipleClustersForm').serialize(),
beforeSend: function (XMLHttpRequest) {
data: $formData.serialize(),
beforeSend: function () {
$(".loading").show();
},
success:function (data, textStatus) {
if (target_id === 'selected') {
success:function (data) {
if (target_id === 'selected' || scope === 'tag_collection') {
location.reload();
} else {
if (scope == 'tag_collection') {
location.reload();
} else {
loadGalaxies(target_id, scope);
handleGenericAjaxResponse(data);
}
loadGalaxies(target_id, scope);
handleGenericAjaxResponse(data);
}
},
error:function() {
showMessage('fail', 'Could not add cluster.');
loadGalaxies(target_id, scope);
if (target_id !== 'selected') {
loadGalaxies(target_id, scope);
}
},
complete:function() {
$("#popover_form").fadeOut();
$("#gray_out").fadeOut();
$(".loading").hide();
$('#temp').remove();
},
type:"post",
url: url
@ -4949,7 +4943,6 @@ function submit_feed_overlap_tool(feedId) {
function fetchFormDataAjax(url, callback, errorCallback) {
$.ajax({
data: '[]',
success: function (data) {
callback(data);
},