Skip to content

Commit

Permalink
connector_search_engine: allow recompute specific bindings
Browse files Browse the repository at this point in the history
_jobify_batch_recompute and batch_recompute now accept a binding_ids param

to force the records to be computed.
  • Loading branch information
simahawk committed Apr 4, 2024
1 parent e8f4667 commit 81f8e3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
17 changes: 12 additions & 5 deletions connector_search_engine/models/se_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,14 @@ def _jobify_batch_sync(self, force_export: bool = False) -> None:
description=description, identity_key=identity_exact
).batch_sync(force_export)

def _jobify_batch_recompute(self, force_export: bool = False) -> None:
def _jobify_batch_recompute(
self, force_export: bool = False, binding_ids: list | None = None
) -> None:
self.ensure_one()
description = _("Prepare a batch recompute of index '%s'") % self.name
self.with_delay(
description=description, identity_key=identity_exact
).batch_recompute(force_export)
).batch_recompute(force_export, binding_ids=binding_ids)

@api.model
def generate_batch_sync_per_index(self, domain: list | None = None) -> None:
Expand Down Expand Up @@ -256,11 +258,16 @@ def _get_domain_for_recomputing_binding(self, force_export: bool = False) -> lis
states.append("recomputing")
return [("index_id", "=", self.id), ("state", "in", states)]

def batch_recompute(self, force_export: bool = False) -> None:
def batch_recompute(
self, force_export: bool = False, binding_ids: list | None = None
) -> None:
"""Recompute all the bindings of the index marked as to_recompute."""
self.ensure_one()
domain = self._get_domain_for_recomputing_binding(force_export)
bindings = self.env["se.binding"].search(domain)
if binding_ids:
bindings = self.env["se.binding"].browse(binding_ids)

Check warning on line 267 in connector_search_engine/models/se_index.py

View check run for this annotation

Codecov / codecov/patch

connector_search_engine/models/se_index.py#L267

Added line #L267 was not covered by tests
else:
domain = self._get_domain_for_recomputing_binding(force_export)
bindings = self.env["se.binding"].search(domain)
bindings_count = len(bindings)
for batch in bindings._batch(self.batch_recomputing_size):
description = _(
Expand Down
2 changes: 1 addition & 1 deletion connector_search_engine/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_life_cycle(self):
trap.assert_enqueued_job(
self.se_index.batch_recompute,
args=(False,),
kwargs={},
kwargs={"binding_ids": None},
properties=dict(
identity_key=identity_exact,
),
Expand Down
9 changes: 8 additions & 1 deletion connector_search_engine/wizards/se_binding_state_updater.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright 2023 ACSONE SA/NV
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo.tools import groupby


class SeBindingStateUpdater(models.TransientModel):
Expand All @@ -14,7 +16,12 @@ class SeBindingStateUpdater(models.TransientModel):
selection=lambda self: self.env["se.binding"]._fields["state"].selection,
required=True,
)
do_it_now = fields.Boolean(help="Don't wait for the cron to process these records")

def doit(self):
res_ids = self.env.context.get("active_ids")
self.env["se.binding"].browse(res_ids).write({"state": self.state})
selected_bindings = self.env["se.binding"].browse(res_ids)
selected_bindings.write({"state": self.state})

Check warning on line 24 in connector_search_engine/wizards/se_binding_state_updater.py

View check run for this annotation

Codecov / codecov/patch

connector_search_engine/wizards/se_binding_state_updater.py#L23-L24

Added lines #L23 - L24 were not covered by tests
if self.do_it_now and self.state == "to_recompute":
for index, bindings in groupby(selected_bindings, key=lambda x: x.index_id):
index._jobify_batch_recompute(binding_ids=[x.id for x in bindings])
8 changes: 7 additions & 1 deletion connector_search_engine/wizards/se_binding_state_updater.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
<field name="arch" type="xml">
<form string="Se Binding State Updater">
<group>
<field name="state" />
<group colspan="2">
<field name="state" />
<field
name="do_it_now"
attrs="{'invisible': [('state', '!=', 'to_recompute')]}"
/>
</group>
</group>
<footer>
<button name="doit" string="OK" class="btn-primary" type="object" />
Expand Down

0 comments on commit 81f8e3c

Please sign in to comment.