-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract query to generate annual totals and add test #12810
Conversation
Performance discussion - the below should not prevent this being merged - it is about the next steps @monishdeb @lcdservices @seamuslee001 @pradpnayak @davejenx I'm interested to see someone else's performance testing results - I realise our Db is a bit of an anomaly & it's likely no-one else has a single contact with 18k donations or however many million contributions we have. However, even confirmation that changing the query to something that looks like the last one in the comment below 'does no harm' would be good. Note that the last one above is what would happen for sites WITH financial acls enabled. There are 2 significant changes to the query
As an aside I am not fully understanding the line item subquery - ie. the use of NOT IN - see the very first query above Current query
6 seconds 20 Without unnecessary ACL clauses
6 seconds 20 Restructured for performance
0.06 seconds Restructured for performance (with extra Having)
0.05 seconds Restructured for performance (with extra Having) & financial types back
.07 seconds Proposed final query Restructured for performance (with extra Having) & financial types back for ACL enabled sites
0.09 seconds |
c597801
to
1cbcdb0
Compare
@monishdeb we have an extension that provides financial acls for reports. It was created after we factored out this functionality from core due to performance reasons. We should a) ensure that there is a message displayed when enabling financial acl in core that the extension is required (I'm not sure that we did this) and b) see if some of the performance refactoring here might be useful in the extension. |
test this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor feedback around standards and clarity. Otherwise looks good.
CRM/Contribute/BAO/Contribution.php
Outdated
'b.is_test = 0', | ||
'b.receive_date >= ' . $startDate, | ||
'b.receive_date < ' . $endDate, | ||
'b.contribution_status_id = 1', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need b.contribution_status_id = 1 in there twice (I appreciate it is important).
Far more important, it should not be 1. 1 isn't clear. I don't know what 1 maps to - a Constant or PseudoConstant would be better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
$monthDay = $config->fiscalYearStart['M'] . $config->fiscalYearStart['d']; | ||
} | ||
else { | ||
$monthDay = '0101'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment like "First of January" at the end of the 0101 line would help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
* | ||
* @return string | ||
*/ | ||
public static function getAnnualQuery($contactIDs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some detail in the comment would help. What does this function do? I know it returns a query, but for what? etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
CRM/Contribute/BAO/Contribution.php
Outdated
$config = CRM_Core_Config::singleton(); | ||
$currentMonth = date('m'); | ||
$currentDay = date('d'); | ||
if ((int ) $config->fiscalYearStart['M'] > $currentMonth || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be (int).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
CRM/Contribute/BAO/Contribution.php
Outdated
$currentDay = date('d'); | ||
if ((int ) $config->fiscalYearStart['M'] > $currentMonth || | ||
((int ) $config->fiscalYearStart['M'] == $currentMonth && | ||
(int ) $config->fiscalYearStart['d'] > $currentDay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please bracket this - I know the order of execution but it can be confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the line breaks which I think makes it clear
$endDate = "$nextYear$monthDay"; | ||
$financialTypes = []; | ||
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes); | ||
$liWhere = " AND i.financial_type_id IN (0)"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the significance of 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment / todo added
CRM/Contribute/BAO/Contribution.php
Outdated
CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClauses, NULL, 'b'); | ||
$query = " | ||
SELECT count(*) as count, | ||
sum(total_amount) as amount, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
COUNT, SUM, AVG AS should probably be all in caps (I think is the convention)
$newFiscalYearStart['M'] = '0' . $newFiscalYearStart['M']; | ||
} | ||
if ($newFiscalYearStart['d'] < 10) { | ||
$newFiscalYearStart['d'] = '0' . $newFiscalYearStart['d']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably this is to convert 1 into 01. I'm 50/50 on whether or a comment should say that's what this is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment / todo added
public function testAnnualQueryWithFinancialACLsDisabled() { | ||
$sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]); | ||
$this->assertContains('sum(total_amount) as amount,', $sql); | ||
$this->assertContains('WHERE b.contact_id IN ( 1,2,3 )', $sql); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be IN (1, 2, 3)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -1316,68 +1316,11 @@ public static function sortName($id) { | |||
* @return array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing as we're in the headspace - please could you add a helpful comment into annual function's header? What is annual? What does it do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
1cbcdb0
to
d92597e
Compare
d92597e
to
55187b2
Compare
55187b2
to
44267a4
Compare
@eileenmcnaughton test fail looks related (noting i think it might just be a correction to the test - whitespace issue @JoeMurray Joe there is this
|
Yeah, that would be a message along the lines I described. Thx. |
44267a4
to
f09517b
Compare
f09517b
to
6b60d32
Compare
Overview
This PR extracts the function that generates the annual totals & adds a test.
Before
No test
After
Test exists both for with & without financial type ACLS. One unnecessary clause is removed from non-ACL sites
Technical Details
If the PR introduces noteworthy technical changes, please describe them here. Provide code snippets if necessary
Comments
This is primarily a preliminary cleanup. We are seeing contribution tabs on our contact with the most contributions (18k) take 6 seconds on this query. From my tests the reason is a combination of