Skip to content

Commit

Permalink
Some tweaks to try speeding up execution [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
anvmn committed Aug 3, 2023
1 parent 9948c39 commit d76871b
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions server/hedley/modules/custom/hedley_stats/hedley_stats.module
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ function hedley_stats_get_total_encounters_by_patients_at_clinic($health_center_
hedley_general_join_field_to_query($query, 'node', 'field_person');
// Only participants who actually attended the sessions.
$query->condition('field_attended.field_attended_value', TRUE);
$result = $query->execute()->fetchAll();
$result = hedley_stats_run_node_query_in_batches($query);

$total_by_patient_at_clinic = [];
$clinic_types = hedley_stats_get_all_clinic_types();
Expand Down Expand Up @@ -1657,8 +1657,6 @@ function hedley_stats_get_case_management_for_period($health_center_id, $period
}
}
$people[] = $item;
// Free up memory.
drupal_static_reset();
}

$result[$program_type] = $people;
Expand Down Expand Up @@ -1837,7 +1835,7 @@ function hedley_stats_get_nutrition_measurements_for_individual_encounters_group
* @return array
* Query accumulated result.
*/
function hedley_stats_run_node_query_in_batches(SelectQuery $query, $batch = 5000) {
function hedley_stats_run_node_query_in_batches(SelectQuery $query, $batch = 10000) {
$query->orderBy('node.nid');

$result = [];
Expand All @@ -1863,6 +1861,8 @@ function hedley_stats_run_node_query_in_batches(SelectQuery $query, $batch = 500
$result = array_merge($result, $batch_result);
// Locate the latest node processed, for the next batch.
$nid = end($batch_result)->nid;
// Free up memory.
drupal_static_reset();
}

return $result;
Expand All @@ -1888,6 +1888,17 @@ function hedley_stats_run_node_query_in_batches(SelectQuery $query, $batch = 500
function hedley_stats_get_base_query($health_center_id, $node_type, $period = FALSE, $extend = FALSE) {
$query = db_select('node', 'node');
$query->fields('node', ['nid', 'type', 'created']);
$node_type_operator = is_array($node_type) ? 'IN' : '=';
$query
->condition('type', $node_type, $node_type_operator)
->condition('status', NODE_PUBLISHED);

// All nodes have the Health center info under `field_shards`.
$health_center_field = 'field_shards';
// Filter by health center.
hedley_general_join_field_to_query($query, 'node', $health_center_field);
$health_center_field_name = $health_center_field . '.' . $health_center_field . '_target_id';
$query->condition($health_center_field_name, $health_center_id);

$encounter_types = hedley_general_get_individual_encounter_types();
$participant_types = [
Expand Down Expand Up @@ -1915,14 +1926,6 @@ function hedley_stats_get_base_query($health_center_id, $node_type, $period = FA
hedley_general_join_field_to_query($query, 'node', $date_field);
}

// All nodes have the Health center info under `field_shards`.
$health_center_field = 'field_shards';
// Filter by health center.
hedley_general_join_field_to_query($query, 'node', $health_center_field);
$health_center_field_name = $health_center_field . '.' . $health_center_field . '_target_id';

$query->condition($health_center_field_name, $health_center_id);

if ($period) {
$date_field_name = $date_field . '.' . $date_field . '_value';

Expand All @@ -1933,11 +1936,6 @@ function hedley_stats_get_base_query($health_center_id, $node_type, $period = FA
], 'BETWEEN');
}

$node_type_operator = is_array($node_type) ? 'IN' : '=';
$query
->condition('type', $node_type, $node_type_operator)
->condition('status', NODE_PUBLISHED);

return $query;
}

Expand Down

0 comments on commit d76871b

Please sign in to comment.