Optimize pandora_db iterations when possible

This commit is contained in:
Calvo 2023-07-17 16:31:40 +02:00
parent d3fded0fa8
commit 24b555da87
1 changed files with 40 additions and 45 deletions

View File

@ -761,56 +761,51 @@ sub pandora_checkdb_integrity {
# Delete orphan data_inc reference records
db_do ($dbh, 'DELETE FROM tagente_datos_inc WHERE id_agente_modulo NOT IN (SELECT id_agente_modulo FROM tagente_modulo)');
# Delete orphan data from visual console.
log_message ('INTEGRITY', "Deleting orphan visual console items.");
db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agent NOT IN (SELECT id_agente FROM tagente)');
db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agente_modulo NOT IN (SELECT id_agente_modulo FROM tagente_modulo)');
# Delete orphan data form deleted agents.
my @agents_ids = get_db_rows ($dbh, 'SELECT id_agente, alias FROM tagente');
my $agents_id = '0';
foreach my $id (@agents_ids) {
$agents_id .= ','.$id->{'id_agente'};
# Clearl orphan data from dashboards
log_message ('INTEGRITY', "Deleting orphan dahsboard items.");
my @agents_ids = get_db_rows($dbh, 'SELECT id_agente FROM tagente');
my $where_condition;
foreach my $agent_id (@agents_ids) {
$where_condition .= 'options NOT LIKE ("%\\"agentid\\":\\"'.$agent_id->{'id_agente'}.'\\"%")';
if($agent_id == $agents_ids[-1]) {
last;
}
$where_condition .= ' AND ';
}
if(defined($agents_id) && $agents_id ne '0') {
# Delete orphan data from visual console.
log_message ('INTEGRITY', "Deleting orphan visual console items.");
db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agent NOT IN (?)', $agents_id);
db_do ($dbh, 'DELETE FROM tlayout_data WHERE id_agente_modulo NOT IN (SELECT id_agente_modulo FROM tagente_modulo)');
# Clearl orphan data from dashboards
log_message ('INTEGRITY', "Deleting orphan dahsboard items.");
my $where_condition;
foreach my $agent_id (@agents_ids) {
$where_condition .= 'options NOT LIKE ("%\\"agentid\\":\\"'.$agent_id->{'id_agente'}.'\\"%")';
if($agent_id == $agents_ids[-1]) {
last;
}
$where_condition .= ' AND ';
}
db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
$where_condition = '';
my @modules = get_db_rows($dbh, 'SELECT id_agente_modulo FROM tagente_modulo');
foreach my $id_agente_modulo (@modules) {
$where_condition .= 'options NOT LIKE ("%\\"moduleId\\":\\"'.$id_agente_modulo->{'id_agente_modulo'}.'\\"%")';
if($id_agente_modulo == $modules[-1]) {
last;
}
$where_condition .= ' AND ';
}
db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
# Delete orphan data from favorite agents
log_message ('INTEGRITY', "Deleting orphan favories items.");
db_do ($dbh, 'DELETE FROM tfavmenu_user WHERE section = "Agents" AND id_element NOT IN (?)', $agents_id);
# Delete orphan data from gis maps
log_message ('INTEGRITY', "Deleting orphan GIS data.");
db_do ($dbh, 'DELETE FROM tgis_data_history WHERE tagente_id_agente NOT IN (?)', $agents_id);
# Delete orphan tnetwork maps data
log_message ('INTEGRITY', "Deleting orphan networkmaps data.");
db_do ($dbh, 'DELETE FROM titem WHERE source_data NOT IN (?)', $agents_id);
db_do ($dbh, 'DELETE FROM trel_item WHERE id_parent_source_data NOT IN (?) OR id_child_source_data NOT IN (?)', $agents_id, $agents_id);
$where_condition = '';
my @modules = get_db_rows($dbh, 'SELECT id_agente_modulo FROM tagente_modulo');
foreach my $id_agente_modulo (@modules) {
$where_condition .= 'options NOT LIKE ("%\\"moduleId\\":\\"'.$id_agente_modulo->{'id_agente_modulo'}.'\\"%")';
if($id_agente_modulo == $modules[-1]) {
last;
}
$where_condition .= ' AND ';
}
db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
# Delete orphan data from favorite agents
log_message ('INTEGRITY', "Deleting orphan favories items.");
db_do ($dbh, 'DELETE FROM tfavmenu_user WHERE section = "Agents" AND id_element NOT IN (SELECT id_agente FROM tagente)');
# Delete orphan data from gis maps
log_message ('INTEGRITY', "Deleting orphan GIS data.");
db_do ($dbh, 'DELETE FROM tgis_data_history WHERE tagente_id_agente NOT IN (SELECT id_agente FROM tagente)');
# Delete orphan tnetwork maps data
log_message ('INTEGRITY', "Deleting orphan networkmaps data.");
db_do ($dbh, 'DELETE FROM titem WHERE source_data NOT IN (SELECT id_agente FROM tagente)');
db_do ($dbh, 'DELETE FROM trel_item WHERE id_parent_source_data NOT IN (SELECT id_agente FROM tagente) OR id_child_source_data NOT IN (SELECT id_agente FROM tagente)');
# Check enterprise tables
enterprise_hook ('pandora_checkdb_integrity_enterprise', [$conf, $dbh]);