Skip to content

Commit

Permalink
dev/core#982 add dedupe.getstatistics api
Browse files Browse the repository at this point in the history
Per gitlab the intent is to add a bunch of api so get the logic out of the form into somewhere more accessible.

Following agreement on this I would change all calls to CRM_Dedupe_Merger::getMergeStats to call this api
and get rid of the call to CRM_Core_BAO_PrevNextCache::retrieve from getMergeStats since that retrieve
function really does differrent stuff & is being kinda pointlessly overloaded here.
  • Loading branch information
eileenmcnaughton committed May 22, 2019
1 parent ffe6b7c commit 7cd216a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 7 deletions.
14 changes: 8 additions & 6 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,13 +755,13 @@ public static function updateMergeStats($cacheKeyString, $result = []) {
}

// get previous stats
$previousStats = CRM_Core_BAO_PrevNextCache::retrieve("{$cacheKeyString}_stats");
$previousStats = CRM_Dedupe_Merger::getMergeStats($cacheKeyString);
if (!empty($previousStats)) {
if ($previousStats[0]['merged']) {
$merged = $merged + $previousStats[0]['merged'];
if ($previousStats['merged']) {
$merged = $merged + $previousStats['merged'];
}
if ($previousStats[0]['skipped']) {
$skipped = $skipped + $previousStats[0]['skipped'];
if ($previousStats['skipped']) {
$skipped = $skipped + $previousStats['skipped'];
}
}

Expand Down Expand Up @@ -796,9 +796,11 @@ public static function resetMergeStats($cacheKeyString) {
*
* @return array
* Array of how many were merged and how many were skipped.
*
* @throws \CiviCRM_API3_Exception
*/
public static function getMergeStats($cacheKeyString) {
$stats = CRM_Core_BAO_PrevNextCache::retrieve("{$cacheKeyString}_stats");
$stats = civicrm_api3('Dedupe', 'get', ['cachekey' => "{$cacheKeyString}_stats"]);
if (!empty($stats)) {
$stats = $stats[0];
}
Expand Down
120 changes: 120 additions & 0 deletions api/v3/Dedupe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* This api exposes CiviCRM dedupe functionality.
*
* @package CiviCRM_APIv3
*/

/**
* Get rows for any cached attempted merges on the passed criteria.
*
* @param array $params
*
* @return array
*/
function civicrm_api3_dedupe_get($params) {
$sql = CRM_Utils_SQL_Select::fragment();
$sql->where("cachekey LIKE 'merge_%'");

return _civicrm_api3_basic_get('CRM_Core_BAO_PrevNextCache', $params);
}

/**
* Delete rows for any cached attempted merges on the passed criteria.
*
* @param array $params
*
* @return array
*
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
function civicrm_api3_dedupe_delete($params) {
return _civicrm_api3_basic_delete('CRM_Core_BAO_PrevNextCache', $params);
}

/**
* Get the statistics for any cached attempted merges on the passed criteria.
*
* @param array $params
*
* @return array
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
function civicrm_api3_dedupe_create($params) {
$sql = CRM_Utils_SQL_Select::fragment();
$sql->where("cachekey LIKE 'merge_%'");

return _civicrm_api3_basic_create('CRM_Core_BAO_PrevNextCache', $params);
}

/**
* Get the statistics for any cached attempted merges on the passed criteria.
*
* @param array $params
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
function civicrm_api3_dedupe_getstatistics($params) {
$stats = CRM_Dedupe_Merger::getMergeStats(CRM_Dedupe_Merger::getMergeCacheKeyString(
$params['rule_group_id'],
CRM_Utils_Array::value('group_id', $params),
CRM_Utils_Array::value('criteria', $params, []),
CRM_Utils_Array::value('check_permissions', $params, [])
));
return civicrm_api3_create_success($stats);
}

/**
* Adjust Metadata for Create action.
*
* The metadata is used for setting defaults, documentation & validation.
*
* @param array $params
* Array of parameters determined by getfields.
*/
function _civicrm_api3_dedupe_getstatistics_spec(&$params) {
$params['rule_group_id'] = [
'title' => ts('Rule Group ID'),
'api.required' => TRUE,
'type' => CRM_Utils_Type::T_INT,
];
$params['group_id'] = [
'title' => ts('Group ID'),
'api.required' => FALSE,
'type' => CRM_Utils_Type::T_INT,
];
$params['criteria'] = [
'title' => ts('Criteria'),
'description' => ts('Dedupe search criteria, as parsable by v3 Contact.get api'),
];

}
12 changes: 11 additions & 1 deletion tests/phpunit/CRM/Dedupe/MergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public function testBatchMergeSelectedDuplicates() {
$select = ['pn.is_selected' => 'is_selected'];
$cacheKeyString = CRM_Dedupe_Merger::getMergeCacheKeyString($dao->id, $this->_groupId);
$pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);

$this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');

// mark first two pairs as selected
Expand All @@ -191,6 +190,13 @@ public function testBatchMergeSelectedDuplicates() {
$result = CRM_Dedupe_Merger::batchMerge($dao->id, $this->_groupId, 'safe', 5, 1);
$this->assertEquals(count($result['merged']), 2, 'Check number of merged pairs.');

$stats = $this->callAPISuccess('Dedupe', 'getstatistics', [
'group_id' => $this->_groupId,
'rule_group_id' => $dao->id,
'check_permissions' => TRUE,
])['values'];
$this->assertEquals(['merged' => 2, 'skipped' => 0], $stats);

// retrieve pairs from prev next cache table
$pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
$this->assertEquals(count($pnDupePairs), 1, 'Check number of remaining dupe pairs in prev next cache.');
Expand Down Expand Up @@ -248,6 +254,10 @@ public function testBatchMergeAllDuplicates() {
$result = CRM_Dedupe_Merger::batchMerge($dao->id, $this->_groupId, 'safe', 5, 2);
$this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');

$stats = $this->callAPISuccess('Dedupe', 'getstatistics', [
'rule_group_id' => $dao->id,
'group_id' => $this->_groupId,
]);
// retrieve pairs from prev next cache table
$pnDupePairs = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
$this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function tearDown() {
'civicrm_group_contact',
'civicrm_saved_search',
'civicrm_group_contact_cache',
'civicrm_prevnext_cache',
);

$this->quickCleanup($tablesToTruncate, TRUE);
Expand Down

0 comments on commit 7cd216a

Please sign in to comment.