-
Notifications
You must be signed in to change notification settings - Fork 23.2k
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
[IMP] survey: improve session code generation #164687
base: master
Are you sure you want to change the base?
[IMP] survey: improve session code generation #164687
Conversation
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.
Hello @awa-odoo ,
Thank you for making and taking on this task!
Here are a few thoughts to contribute :)
addons/survey/tests/test_survey.py
Outdated
self.assertTrue(all( | ||
bool(session_code) for session_code in surveys.mapped('session_code') | ||
)) |
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.
self.assertTrue(all( | |
bool(session_code) for session_code in surveys.mapped('session_code') | |
)) | |
self.assertNotIn(False, surveys.mapped('session_code')) |
surveys = self.env['survey.survey'].create([{ | ||
'title': f'Survey {i}' | ||
} for i in range(10)]) |
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.
This is a nice result! I think we can now simplify a few tests that had split create
because of the codes (or I had to manually set to fix tours).
@@ -317,6 +297,17 @@ def _compute_session_question_answer_count(self): | |||
)[0] | |||
survey.session_question_answer_count = answer_count | |||
|
|||
@api.depends('access_token') | |||
def _compute_session_code(self): | |||
self.flush_model(['session_code']) |
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.
Isn't this automatically done in _fetch_query
since session_code
is in the requested fields?
And if not, shouldn't this rather be in _generate_session_codes
before the search_read
?
code_candidates -= {colliding_code['session_code'] for colliding_code in colliding_codes} | ||
code_candidates -= excluded_codes | ||
if code_candidates: | ||
session_codes += list(code_candidates)[:code_count] | ||
if len(session_codes) == code_count: | ||
return session_codes |
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.
Keeping session_codes
as a set until returning and with an additional step outside the loop
unavailable_codes = excluded_codes + {colliding_code['session_code'] for colliding_code in colliding_codes}
we could have the slightly shorter/simpler
code_candidates -= {colliding_code['session_code'] for colliding_code in colliding_codes} | |
code_candidates -= excluded_codes | |
if code_candidates: | |
session_codes += list(code_candidates)[:code_count] | |
if len(session_codes) == code_count: | |
return session_codes | |
session_codes += code_candidates - unavailable_codes | |
if len(session_codes) >= code_count: | |
return list(session_codes)[:code_count] |
excluded_codes = excluded_codes or set() | ||
colliding_codes = self.sudo().search_read( | ||
[('session_code', '!=', False)], | ||
['session_code'] |
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.
It doesn't cost much to have this as well
['session_code'] | |
['session_code'], | |
load='', |
@@ -317,6 +297,17 @@ def _compute_session_question_answer_count(self): | |||
)[0] | |||
survey.session_question_answer_count = answer_count | |||
|
|||
@api.depends('access_token') |
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.
This seems to work as fake-dependency trigger. I'm guessing because it's required and therefore present when precomputing 🤷 ?
@api.depends('access_token') | |
@api.depends('title') |
a9059f8
to
0421915
Compare
The current session code generation for surveys is a bit messy: - It runs multiple times for a create-multi syntax - It has poor performance with a search within a loop - It can fail non-deterministically This commit reworks the code generation in favor of a precomputed stored field with the compute method generating all the necessary codes at once. Task-3916147 Runbot-error-57541
0421915
to
7ef8205
Compare
The current session code generation for surveys is a bit messy:
This commit reworks the code generation in favor of a precomputed stored field with the compute method generating all the necessary codes at once.
Task-3916147
Runbot-error-57541