Skip to content

Commit

Permalink
Remove median & mode stats from contribution summary in order to impr…
Browse files Browse the repository at this point in the history
…ove performance
  • Loading branch information
eileenmcnaughton committed Feb 18, 2019
1 parent 2d97ba3 commit b64b7a9
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 69 deletions.
61 changes: 2 additions & 59 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5100,15 +5100,11 @@ public function summaryContribution($context = NULL) {

$summary = ['total' => []];
$this->addBasicStatsToSummary($summary, $where, $from);
$this->addModeToStats($summary, $from, $where);
$this->addMedianToStats($summary, $where, $from);

if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) {
$this->addBasicSoftCreditStatsToStats($summary, $where, $from);
}

$summary['total']['currencyCount'] = count($summary['total']['median']);

$this->addBasicCancelStatsToSummary($summary, $where, $from);

return $summary;
Expand Down Expand Up @@ -6558,70 +6554,17 @@ protected function addBasicStatsToSummary(&$summary, $where, $from) {
$summary['total']['amount'][] = CRM_Utils_Money::format($dao->total_amount, $dao->currency);
$summary['total']['avg'][] = CRM_Utils_Money::format($dao->total_avg, $dao->currency);
}
$summary['total']['currencyCount'] = count($summary['total']['amount']);
if (!empty($summary['total']['amount'])) {
$summary['total']['amount'] = implode(', ', $summary['total']['amount']);
$summary['total']['avg'] = implode(', ', $summary['total']['avg']);
}
else {
$summary['total']['amount'] = $summary['total']['avg'] = $summary['total']['median'] = 0;
$summary['total']['amount'] = $summary['total']['avg'] = 0;
}
return $summary;
}

/**
* Add the mode to stats.
*
* Note that his is a slow query when performed on more than a handful or results - often taking many minutes
*
* See https://lab.civicrm.org/dev/core/issues/720
*
* @param array $summary
* @param string $from
* @param string $where
*/
protected function addModeToStats(&$summary, $from, $where) {
$modeSQL = "
SELECT COUNT( conts.total_amount ) as total_count,
SUM( conts.total_amount ) as total_amount,
AVG( conts.total_amount ) as total_avg,
conts.currency as currency,
SUBSTRING_INDEX(GROUP_CONCAT(conts.total_amount
ORDER BY conts.civicrm_contribution_total_amount_count DESC SEPARATOR ';'), ';', 1) as amount,
MAX(conts.civicrm_contribution_total_amount_count) as civicrm_contribution_total_amount_count
FROM (
SELECT civicrm_contribution.total_amount,
COUNT(civicrm_contribution.total_amount) as civicrm_contribution_total_amount_count,
civicrm_contribution.currency
$from
$where AND civicrm_contribution.contribution_status_id = 1
GROUP BY currency, civicrm_contribution.total_amount
ORDER BY civicrm_contribution_total_amount_count DESC
) as conts
GROUP BY currency";

$mode = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL);
$summary['total']['mode'] = implode(', ', (array) $mode);
}

/**
* Add the median to the stats.
*
* Note that is can be a very slow query - taking many many minutes and even on a small
* data set it's likely to take longer than all the other queries combined by a significant
* multiple
*
* see https://lab.civicrm.org/dev/core/issues/720
*
* @param array $summary
* @param string $where
* @param string $from
*/
protected function addMedianToStats(&$summary, $where, $from) {
$medianSQL = "{$from} {$where} AND civicrm_contribution.contribution_status_id = 1 ";
$median = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, 'civicrm_contribution');
$summary['total']['median'] = implode(', ', (array) $median);
}

/**
* Add basic soft credit statistics to summary array.
*
Expand Down
6 changes: 1 addition & 5 deletions templates/CRM/Contribute/Page/ContributionTotals.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,10 @@
<th class="left contriTotalRight"> &nbsp; {ts}# Completed{/ts} &ndash; {$contributionSummary.total.count}</th>
</tr><tr>
<th class="contriTotalLeft">{ts}Avg{/ts} &ndash; {$contributionSummary.total.avg}</th>
<th class="right"> &nbsp; {ts}Median{/ts} &ndash; {$contributionSummary.total.median}</th>
<th class="right contriTotalRight"> &nbsp; {ts}Mode{/ts} &ndash; {$contributionSummary.total.mode}</th>
{else}
<th class="contriTotalLeft right">{ts}Total{/ts} &ndash; {$contributionSummary.total.amount}</th>
<th class="right"> &nbsp; {ts}# Completed{/ts} &ndash; {$contributionSummary.total.count}</th>
<th class="right"> &nbsp; {ts}Avg{/ts} &ndash; {$contributionSummary.total.avg}</th>
<th class="right"> &nbsp; {ts}Median{/ts} &ndash; {$contributionSummary.total.median}</th>
<th class="right contriTotalRight"> &nbsp; {ts}Mode{/ts} &ndash; {$contributionSummary.total.mode}</th>
<th class="right contriTotalRight"> &nbsp; {ts}Avg{/ts} &ndash; {$contributionSummary.total.avg}</th>
{/if}
{/if}
{if $contributionSummary.cancel.amount}
Expand Down
5 changes: 0 additions & 5 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,6 @@ public function testGetSummaryQueryWithFinancialACLDisabled() {
'avg' => '$ 233.33',
'amount' => '$ 1,400.00',
'count' => 6,
'mode' => '$ 300.00',
'median' => '$ 300.00',
'currencyCount' => 1,
],
'cancel' => [
Expand Down Expand Up @@ -773,9 +771,6 @@ public function testGetSummaryQueryWithFinancialACLEnabled() {
'avg' => '$ 200.00',
'amount' => '$ 400.00',
'count' => 2,
'mode' => 'N/A',
'median' => '$ 200.00',
'currencyCount' => 1,
],
'cancel' => [
'count' => 1,
Expand Down

0 comments on commit b64b7a9

Please sign in to comment.