Memory optimization in blockedlog module

This commit is contained in:
Laurent Destailleur 2021-02-04 23:03:09 +01:00
parent 98e5a655cc
commit 42557a0148
4 changed files with 49 additions and 39 deletions

View File

@ -77,6 +77,7 @@ if (preg_match('/del_(.*)/', $action, $reg))
$form = new Form($db); $form = new Form($db);
$block_static = new BlockedLog($db); $block_static = new BlockedLog($db);
$block_static->loadTrackedEvents();
llxHeader('', $langs->trans("BlockedLogSetup")); llxHeader('', $langs->trans("BlockedLogSetup"));

View File

@ -68,7 +68,7 @@ if (empty($sortfield)) $sortfield = 'rowid';
if (empty($sortorder)) $sortorder = 'DESC'; if (empty($sortorder)) $sortorder = 'DESC';
$block_static = new BlockedLog($db); $block_static = new BlockedLog($db);
$block_static->loadTrackedEvents();
$result = restrictedArea($user, 'blockedlog', 0, ''); $result = restrictedArea($user, 'blockedlog', 0, '');
@ -270,7 +270,7 @@ if (GETPOST('withtab', 'alpha'))
llxHeader('', $langs->trans("BrowseBlockedLog")); llxHeader('', $langs->trans("BrowseBlockedLog"));
$MAXLINES = 100000; $MAXLINES = 50000;
$blocks = $block_static->getLog('all', 0, $MAXLINES, $sortfield, $sortorder, $search_fk_user, $search_start, $search_end, $search_ref, $search_amount, $search_code); $blocks = $block_static->getLog('all', 0, $MAXLINES, $sortfield, $sortorder, $search_fk_user, $search_start, $search_end, $search_ref, $search_amount, $search_code);
if (!is_array($blocks)) if (!is_array($blocks))

View File

@ -124,9 +124,17 @@ class BlockedLog
*/ */
public function __construct(DoliDB $db) public function __construct(DoliDB $db)
{ {
global $conf;
$this->db = $db; $this->db = $db;
}
/**
* Load list of tracked events into $this->trackedevents.
*
* @return int Always 1
*/
public function loadTrackedEvents() {
global $conf;
$this->trackedevents = array(); $this->trackedevents = array();
@ -176,17 +184,21 @@ class BlockedLog
$moduleposenabled = (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->BANK_ENABLE_POS_CASHCONTROL)); $moduleposenabled = (!empty($conf->cashdesk->enabled) || !empty($conf->takepos->enabled) || !empty($conf->global->BANK_ENABLE_POS_CASHCONTROL));
if ($moduleposenabled) $this->trackedevents['CASHCONTROL_VALIDATE'] = 'logCASHCONTROL_VALIDATE'; if ($moduleposenabled) $this->trackedevents['CASHCONTROL_VALIDATE'] = 'logCASHCONTROL_VALIDATE';
// Add more action to track from a conf variable
if (!empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED)) { if (!empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED)) {
$tmparrayofmoresupportedevents = explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED); $tmparrayofmoresupportedevents = explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED);
foreach ($tmparrayofmoresupportedevents as $val) { foreach ($tmparrayofmoresupportedevents as $val) {
$this->trackedevents[$val] = 'log'.$val; $this->trackedevents[$val] = 'log'.$val;
} }
} }
return 1;
} }
/** /**
* Try to retrieve source object (it it still exists) * Try to retrieve source object (it it still exists).
* @return string *
* @return string URL string of source object
*/ */
public function getObjectLink() public function getObjectLink()
{ {
@ -626,31 +638,24 @@ class BlockedLog
*/ */
public function fetch($id) public function fetch($id)
{ {
global $langs; global $langs;
dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG);
if (empty($id)) if (empty($id))
{ {
$this->error = 'BadParameter'; $this->error = 'BadParameter';
return -1; return -1;
} }
$langs->load("blockedlog");
$sql = "SELECT b.rowid, b.date_creation, b.signature, b.signature_line, b.amounts, b.action, b.element, b.fk_object, b.entity,"; $sql = "SELECT b.rowid, b.date_creation, b.signature, b.signature_line, b.amounts, b.action, b.element, b.fk_object, b.entity,";
$sql .= " b.certified, b.tms, b.fk_user, b.user_fullname, b.date_object, b.ref_object, b.object_data, b.object_version"; $sql .= " b.certified, b.tms, b.fk_user, b.user_fullname, b.date_object, b.ref_object, b.object_data, b.object_version";
$sql .= " FROM ".MAIN_DB_PREFIX."blockedlog as b"; $sql .= " FROM ".MAIN_DB_PREFIX."blockedlog as b";
if ($id) $sql .= " WHERE b.rowid = ".$id; if ($id) $sql .= " WHERE b.rowid = ".((int) $id);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{
if ($this->db->num_rows($resql))
{ {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
if ($obj) {
$this->id = $obj->rowid; $this->id = $obj->rowid;
$this->entity = $obj->entity; $this->entity = $obj->entity;
$this->ref = $obj->rowid; $this->ref = $obj->rowid;
@ -678,6 +683,7 @@ class BlockedLog
return 1; return 1;
} else { } else {
$langs->load("blockedlog");
$this->error = $langs->trans("RecordNotFound"); $this->error = $langs->trans("RecordNotFound");
return 0; return 0;
} }
@ -946,10 +952,11 @@ class BlockedLog
*/ */
public function getLog($element, $fk_object, $limit = 0, $sortfield = '', $sortorder = '', $search_fk_user = -1, $search_start = -1, $search_end = -1, $search_ref = '', $search_amount = '', $search_code = '') public function getLog($element, $fk_object, $limit = 0, $sortfield = '', $sortorder = '', $search_fk_user = -1, $search_start = -1, $search_end = -1, $search_ref = '', $search_amount = '', $search_code = '')
{ {
global $conf, $cachedlogs; global $conf;
//global $cachedlogs;
/* $cachedlogs allow fastest search */ /* $cachedlogs allow fastest search */
if (empty($cachedlogs)) $cachedlogs = array(); //if (empty($cachedlogs)) $cachedlogs = array();
if ($element == 'all') { if ($element == 'all') {
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
@ -962,7 +969,7 @@ class BlockedLog
WHERE entity=".$conf->entity." AND certified = 1"; WHERE entity=".$conf->entity." AND certified = 1";
} else { } else {
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."blockedlog
WHERE entity=".$conf->entity." AND element='".$element."' AND fk_object=".(int) $fk_object; WHERE entity=".$conf->entity." AND element='".$this->db->escape($element)."' AND fk_object=".(int) $fk_object;
} }
if ($search_fk_user > 0) $sql .= natural_search("fk_user", $search_fk_user, 2); if ($search_fk_user > 0) $sql .= natural_search("fk_user", $search_fk_user, 2);
@ -989,15 +996,16 @@ class BlockedLog
return -2; return -2;
} }
if (!isset($cachedlogs[$obj->rowid])) //if (!isset($cachedlogs[$obj->rowid]))
{ //{
$b = new BlockedLog($this->db); $b = new BlockedLog($this->db);
$b->fetch($obj->rowid); $b->fetch($obj->rowid);
//$b->loadTrackedEvents();
//$cachedlogs[$obj->rowid] = $b;
//}
$cachedlogs[$obj->rowid] = $b; //$results[] = $cachedlogs[$obj->rowid];
} $results[] = $b;
$results[] = $cachedlogs[$obj->rowid];
} }
return $results; return $results;

View File

@ -69,6 +69,7 @@ class InterfaceActionsBlockedLog extends DolibarrTriggers
require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php'; require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
$b = new BlockedLog($this->db); $b = new BlockedLog($this->db);
$b->loadTrackedEvents();
// Tracked events // Tracked events
if (!in_array($action, array_keys($b->trackedevents))) { if (!in_array($action, array_keys($b->trackedevents))) {