Remove median & mode stats from contribution summary in order to improve performance
authoreileen <emcnaughton@wikimedia.org>
Mon, 18 Feb 2019 00:31:00 +0000 (13:31 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 18 Feb 2019 05:15:13 +0000 (18:15 +1300)
CRM/Contact/BAO/Query.php
templates/CRM/Contribute/Page/ContributionTotals.tpl
tests/phpunit/CRM/Contact/BAO/QueryTest.php

index 5247434ab824ea3793a0b7e5ac44c2168b814b4d..db917cd47029a6e3ae66f8b3a1a650820f9fb211 100644 (file)
@@ -5100,15 +5100,11 @@ civicrm_relationship.start_date > {$today}
 
     $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;
@@ -6558,70 +6554,17 @@ AND   displayRelType.is_active = 1
       $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(',&nbsp;', $summary['total']['amount']);
       $summary['total']['avg'] = implode(',&nbsp;', $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(',&nbsp;', (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(',&nbsp;', (array) $median);
-  }
-
   /**
    * Add basic soft credit statistics to summary array.
    *
index e995ee4e0f058c6074e69ddfc2ec2fdba2016033..3e5576d8dfba6d41af534ef5364089fa30ed066e 100644 (file)
               <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}
index c0f4af3740abb1119ff6bcdfbb341dcea6cc4e29..dc0cc0afff9200ca875228f0092322b46f030e8d 100644 (file)
@@ -734,8 +734,6 @@ civicrm_relationship.is_active = 1 AND
         'avg' => '$ 233.33',
         'amount' => '$ 1,400.00',
         'count' => 6,
-        'mode' => '$ 300.00',
-        'median' => '$ 300.00',
         'currencyCount' => 1,
         ],
       'cancel' => [
@@ -773,9 +771,6 @@ civicrm_relationship.is_active = 1 AND
         'avg' => '$ 200.00',
         'amount' => '$ 400.00',
         'count' => 2,
-        'mode' => 'N/A',
-        'median' => '$ 200.00',
-        'currencyCount' => 1,
       ],
       'cancel' => [
         'count' => 1,