Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

awa-odoo
Copy link
Contributor

@awa-odoo awa-odoo commented May 7, 2024

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

@robodoo
Copy link
Contributor

robodoo commented May 7, 2024

Pull request status dashboard.

@C3POdoo C3POdoo requested a review from a team May 7, 2024 09:52
@C3POdoo C3POdoo added the RD research & development, internal work label May 7, 2024
Copy link
Contributor

@flch-odoo flch-odoo left a 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 :)

Comment on lines 273 to 275
self.assertTrue(all(
bool(session_code) for session_code in surveys.mapped('session_code')
))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertTrue(all(
bool(session_code) for session_code in surveys.mapped('session_code')
))
self.assertNotIn(False, surveys.mapped('session_code'))

Comment on lines +270 to +272
surveys = self.env['survey.survey'].create([{
'title': f'Survey {i}'
} for i in range(10)])
Copy link
Contributor

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).

addons/survey/models/survey_survey.py Outdated Show resolved Hide resolved
@@ -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'])
Copy link
Contributor

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?

Comment on lines 1285 to 1290
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
Copy link
Contributor

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

Suggested change
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']
Copy link
Contributor

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

Suggested change
['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')
Copy link
Contributor

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 🤷 ?

Suggested change
@api.depends('access_token')
@api.depends('title')

@awa-odoo awa-odoo force-pushed the master-survey-session-code-rework-awa branch from a9059f8 to 0421915 Compare June 11, 2024 13:25
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
@awa-odoo awa-odoo force-pushed the master-survey-session-code-rework-awa branch from 0421915 to 7ef8205 Compare June 11, 2024 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RD research & development, internal work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants