Optimize pandora_db iterations when possible

This commit is contained in:
Calvo 2023-07-17 16:31:40 +02:00
parent d3fded0fa8
commit 24b555da87

View File

@ -761,56 +761,51 @@ sub pandora_checkdb_integrity {
# Delete orphan data_inc reference records # 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)'); 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. # Delete orphan data form deleted agents.
my @agents_ids = get_db_rows ($dbh, 'SELECT id_agente, alias FROM tagente'); # Clearl orphan data from dashboards
my $agents_id = '0'; log_message ('INTEGRITY', "Deleting orphan dahsboard items.");
foreach my $id (@agents_ids) { my @agents_ids = get_db_rows($dbh, 'SELECT id_agente FROM tagente');
$agents_id .= ','.$id->{'id_agente'}; 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 db_do ($dbh, 'UPDATE twidget_dashboard set options = NULL WHERE '.$where_condition);
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); $where_condition = '';
my @modules = get_db_rows($dbh, 'SELECT id_agente_modulo FROM tagente_modulo');
$where_condition = ''; foreach my $id_agente_modulo (@modules) {
my @modules = get_db_rows($dbh, 'SELECT id_agente_modulo FROM tagente_modulo'); $where_condition .= 'options NOT LIKE ("%\\"moduleId\\":\\"'.$id_agente_modulo->{'id_agente_modulo'}.'\\"%")';
foreach my $id_agente_modulo (@modules) { if($id_agente_modulo == $modules[-1]) {
$where_condition .= 'options NOT LIKE ("%\\"moduleId\\":\\"'.$id_agente_modulo->{'id_agente_modulo'}.'\\"%")'; last;
if($id_agente_modulo == $modules[-1]) { }
last; $where_condition .= ' AND ';
}
$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);
} }
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 # Check enterprise tables
enterprise_hook ('pandora_checkdb_integrity_enterprise', [$conf, $dbh]); enterprise_hook ('pandora_checkdb_integrity_enterprise', [$conf, $dbh]);