Skip to content

Commit

Permalink
Cache allowed transitions for analyses on the request (#1417)
Browse files Browse the repository at this point in the history
* Cache allowed transitions for analyses in request

This improves the rendering performance of worksheets containing a huge
amount of analyses with dependencies.

* Changelog updated
  • Loading branch information
ramonski authored and xispa committed Aug 9, 2019
1 parent 6afbaf5 commit 115cb3c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -11,6 +11,7 @@ Changelog

**Changed**

- #1417 Cache allowed transitions for analyses on the request
- #1413 Improved Email Publication


Expand Down
42 changes: 39 additions & 3 deletions bika/lims/workflow/analysis/guards.py
Expand Up @@ -19,9 +19,13 @@
# Some rights reserved, see README and LICENSE.

from bika.lims import api
from bika.lims.interfaces import IWorksheet, IVerified, ISubmitted
from bika.lims.interfaces.analysis import IRequestAnalysis
from bika.lims import logger
from bika.lims import workflow as wf
from bika.lims.interfaces import ISubmitted
from bika.lims.interfaces import IVerified
from bika.lims.interfaces import IWorksheet
from bika.lims.interfaces.analysis import IRequestAnalysis
from plone.memoize.request import cache


def is_worksheet_context():
Expand Down Expand Up @@ -311,11 +315,43 @@ def is_transition_allowed(analyses, transition_id):
if not isinstance(analyses, list):
return is_transition_allowed([analyses], transition_id)
for analysis in analyses:
if not wf.isTransitionAllowed(analysis, transition_id):
if not cached_is_transition_allowed(analysis, transition_id):
return False
return True


def _transition_cache_key(fun, obj, action):
"""Cache key generator for the request cache
This function generates cache keys like this:
>>> from bika.lims import api
>>> from zope.annotation.interfaces import IAnnotations
>>> request = api.get_request()
>>> IAnnotations(request)
# noqa: E501
{'bika.lims.workflow.analysis.guards.check_analysis_allows_transition:3ff02762c70f4a56b1b30c1b74d32bf6-retract': True,
'bika.lims.workflow.analysis.guards.check_analysis_allows_transition:0390c16ddec14a04b87ff8408e2aa229-retract': True,
...
}
"""
return "%s-%s" % (api.get_uid(obj), action)


@cache(get_key=_transition_cache_key, get_request="analysis.REQUEST")
def cached_is_transition_allowed(analysis, transition_id):
"""Check if the transition is allowed for the given analysis and cache the
value on the request.
Note: The request is obtained by the given expression from the `locals()`,
which includes the given arguments.
"""
logger.debug("cached_is_transition_allowed: analyis=%r transition=%s"
% (analysis, transition_id))
if wf.isTransitionAllowed(analysis, transition_id):
return True
return False


def is_submitted_or_submittable(analysis):
"""Returns whether the analysis is submittable or has already been submitted
"""
Expand Down

0 comments on commit 115cb3c

Please sign in to comment.