-
-
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
CRM-20207 Added invocation for selectWhereClause hook in activity SQL #9916
Conversation
I don't think we should be adding anything to that function - I think we should be trying to remove it in favour of more predictable api calls - eg. something along the lines of this (note that doesn't 100% work, the api join syntax would need to be used to get a few extra fields to display, + unit tests needed, however, I just wanted to try quickly to see how integral that function is) |
@colemanw input welcome! |
I changed to WIP because I think the approach here it to add a hook to a toxic function & we can call the api instead, (with a little more work) and remove the toxic code |
@eileenmcnaughton I think we need to have a common understanding of what is toxic code and what is reasonable to request in terms of a refactoring. I'm very comfortable if you have confidence in your approach to go that way. But I don't have a good enough appreciation for implications of taking out all that code. Are you requesting other folks do this kind of refactoring? @colemanw comments welcome |
Yeah - I think we need to be very careful when adding hooks that we do research first to understand what way the code is going in, because hooks lock in bad code. A first step that I take when trying to assess if code is consistent with more recent efforts is a git blame, to see how recently it has been touched & who has touched it. Older code is more likely to be legacy. Code with lots of hand-written sql in it is likely to be legacy or undesirable, code using the api is likely to be recent, code written by Coleman is likely to be gold standard. It's worth asking the question when touching the form layer, in particular, if the work could be done using the api. ACL hooks are a particular place where we don't want to go down the wrong path. Ideally when first looking into the code areas devs go on chat & say what they are trying to do & try to get ideas. It doesn't always work, but sometimes it does. In this case there is a function with lots of hand-written sql code that was imported from svn, that is the subject of another open pr (where I said it should be re-written, although I didn't suggest switching to the api at that stage) & using api explorer it seems it we can do the same get with the api & in general, we know the api is where all the hook work will be directed. The alternative I suggested was more of a quick draft than final code - as it does not include all the fields yet : eileenmcnaughton@8dd8e64 |
Monish wrote me offline: Hi Joe Murray
Edsel responded: My sense: |
I'm going to get an estimate for replacing SQL with existing API call and using SQL to retrieve missing pieces in the absence of a CaseActivity.get API. |
To a couple of the above points, I've recently implemented |
@colemanw my understanding is that there effectively IS a caseActivity api based on the work you have done? The existing code appears to go to great lengths to be efficient, but then includes unindexed joins on the option_value table |
I guess the minimum here would be getting some unit tests in place to ensure the ACL works & also testing the options that can be passed into the function. |
oh - & the tests should test getContactActivities & count - not the mysql function - that needs to be littered with comments saying - if you want to change this further you need to fix it to use the api |
We should really index the optionValue field!
Actually, I confirmed https://github.com/civicrm/civicrm-core/blob/master/xml/schema/Core/OptionValue.xml#L204 both it and name are indexed, so I imagine the issue is that it is being joined to fields that are not indexed.
|
FWIW, the JOINs at eileenmcnaughton@8dd8e64#diff-a35044abd0f538e4af551c5ae039692dL1088 and L1129 can be greatly improved by changing |
@Edzelopez and @monishdeb, could you provide updated estimate given @colemanw's work means it's not necessary to create an API for CaseActivity, nor the associated tests? Seems like we can retrieve all needed fields using chained API calls. |
@JoeMurray the reason the join is not indexed is, as you identify, due to casting. Casting on the join resolution won't make it an indexed join :-( We resolved this around 4.4 by switching to using pseudoconstants to resolve our values from the option_value table & then using those. In most cases simply switching to using the api solves the issue, since it was solved at the api layer a while back. |
@JoeMurray yes now we can fetch all desired extra fields by using Activity.get with some api chaining and after discussing with @Edzelopez, my estimate will be 22-24 hr including CRM UT(unit test) on those two BAO functions and also one UT to ensure ACL works. |
None of this is paid work. Give it a medium priority. |
Actually, it's needed work just unfunded. So give it high priority. |
I just took a closer look and was surprised to discover that the function is only called in 2 places. The best course of action may be to just get rid of it and use the api from those 2 places instead. |
@colemanw are you referring to In my recent commit 2a11177 I have replaced it with activity.get API call here with some desired filters, ensure that there aren't regression due to refactoring. However I was unable to fix few issues listed here I have marked it with WIP since I am going to add associated CRM unit test for |
@monishdeb I mean that PHPStorm's "Find Usages" of |
@colemanw ah ok, got your point but as per my recent changes this function is used in 4 places
As now I have introduced a 2nd parameter to So overall the final patch replaces |
Just to provide some context here: this is all unfunded work that has taken many times the original funding. We have been generous in my view in completely refactoring a few files. We are not interested at this point in doing further refactorings on this issue which was originally to provide proper permissioning for Activity types, however nice that might be, to improve core in general. Please create a separate unfunded issue for refactoring out those additional functions. Perhaps we need a process for handling scope escalations on these sorts of issues. |
@eileenmcnaughton made the additional changes as per your comments |
Test build passed |
The code looks good to me ... @jitendrapurohit are you able to give this a bit of testing & then we can look to merge. I think you have a few open that @monishdeb might review in exchange... |
sure, will QA in a day or two. |
@eileenmcnaughton yes the join onto the option_value table is mismatched. We could fix that by changing the field to be varchar. Didn't you investigate that at one point? |
@colemanw I did recommend changes along those lines but it was a bit complex. The approach adopted was the pseudoconstants. I think in the api we should add special handling for joins on the option_value table to resolve without the join |
) { | ||
$limit = " LIMIT {$input['offset']}, {$input['rowCount']} "; | ||
$activityParams['options']['limit'] = $params['rowCount']; | ||
} |
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.
Also would like to discuss a change in behavior of filters(after this patch) : On a contact summary page (Activities tab). The Include and Exclude Filters don't respect the other if either of it is selected. For eg:
Should we consider this as an improvement (Recent filter is respected) or as an introduced bug? |
---------------------------------------- * CRM-20207: Introduce selectWhereClause hook for activity results on activity tab contact summary page https://issues.civicrm.org/jira/browse/CRM-20207
@jitendrapurohit I have updated the PR with additional fixes needed, please have a look 2d64829 |
test this please |
Just a note that I consider this to be in the last stages of QA so when @jitendrapurohit approves for merge you can go ahead & merge - preferably with commit squashing |
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.
Tested this on Activity Tab(add/edit/delete operation), Activity Dashlet, add/edit custom fields for activities, invoke selectWhereClause hook on activity tabs, works well in all cases.
Not sure why the test fails - it works for me on local. So I think its unrelated.
@jitendrapurohit it may not be related. Try doing a rebase & force push and see if that resolves it. Command (depending on your remote aliases):
|
Adding merge on pass per @jitendrapurohit review. I think the test issue is server - will try to kick it off again |
test this please |
test this please |
yay! |
As discussed on Mattermost, this PR appears to cause a fatal error on contact view for ACL'd user when the viewed contact has activities. |
https://issues.civicrm.org/jira/browse/CRM-20207