Skip to content

Commit

Permalink
Merge pull request #818 from TIP-Global-Health/issue-816
Browse files Browse the repository at this point in the history
Speed up dashboards stats execution time on Pantheon
  • Loading branch information
anvmn authored Aug 8, 2023
2 parents d973a25 + 5e2eb42 commit c7807e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions client/src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ const getSyncSpeed = function() {
return storageArr;
}

// Idle time between sync is 5 min.
// Idle time between sync is 10 min.
// Sync cicle last 50 milliseconds.
// When offline, we check network state every 30 secons.
return {idle: (5 * 60 * 1000), cycle: 50, offline: (30 * 1000)};
return {idle: (10 * 60 * 1000), cycle: 50, offline: (30 * 1000)};
}

// Start up our Elm app.
Expand Down
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 @@ -1390,7 +1390,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 @@ -1670,8 +1670,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 @@ -1850,7 +1848,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 @@ -1876,6 +1874,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 @@ -1901,6 +1901,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 @@ -1928,14 +1939,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 @@ -1946,11 +1949,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 c7807e6

Please sign in to comment.