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

[16.0][PERF] stock_release_channel: kanban display performance #965

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions stock_release_channel/models/stock_release_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import logging
from collections import defaultdict
from copy import deepcopy
from operator import itemgetter

from pytz import timezone

from odoo import _, api, exceptions, fields, models
from odoo.osv.expression import NEGATIVE_TERM_OPERATORS
from odoo.tools import groupby
from odoo.tools.safe_eval import (
datetime as safe_datetime,
dateutil as safe_dateutil,
Expand Down Expand Up @@ -460,15 +462,18 @@ def _query_get_chain(self, pickings):
WITH RECURSIVE
pickings AS (
SELECT move.picking_id,
stock_picking.release_channel_id,
true as outgoing,
''::varchar as state, -- no need it, we exclude outgoing
move.id as move_orig_id
FROM stock_move move
INNER JOIN stock_picking ON move.picking_id = stock_picking.id
WHERE move.picking_id in %s

UNION

SELECT move.picking_id,
pickings.release_channel_id,
false as outgoing,
picking.state,
rel.move_orig_id
Expand All @@ -480,7 +485,7 @@ def _query_get_chain(self, pickings):
INNER JOIN stock_picking picking
ON picking.id = move.picking_id
)
SELECT DISTINCT picking_id, state FROM pickings
SELECT DISTINCT release_channel_id, picking_id, state FROM pickings
WHERE outgoing is false;
"""
return (query, (tuple(pickings.ids),))
Expand All @@ -490,20 +495,24 @@ def _compute_picking_chain(self):
["move_dest_ids", "move_orig_ids", "picking_id"]
)
self.env["stock.picking"].flush_model(["state"])
for channel in self:
domain = self._field_picking_domains()["released"]
domain += [("release_channel_id", "=", channel.id)]
released = self.env["stock.picking"].search(domain)

if not released:
domain = self._field_picking_domains()["released"]
domain += [("release_channel_id", "in", self.ids)]
released = self.env["stock.picking"].search(domain)

if not released:
for channel in self:
channel.picking_chain_ids = False
channel.count_picking_chain = 0
channel.count_picking_chain_in_progress = 0
channel.count_picking_chain_done = 0
continue
return

self.env.cr.execute(*self._query_get_chain(released))
rows = self.env.cr.dictfetchall()
self.env.cr.execute(*self._query_get_chain(released))
rows = self.env.cr.dictfetchall()
rows_by_channel = dict(groupby(rows, key=itemgetter("release_channel_id")))
for channel in self:
rows = rows_by_channel.get(channel.id, [])
channel.picking_chain_ids = [row["picking_id"] for row in rows]
channel.count_picking_chain_in_progress = sum(
[1 for row in rows if row["state"] not in ("cancel", "done")]
Expand Down
Loading