flows/planner: optimise db queries during plan building

This commit is contained in:
Jens Langhammer 2020-09-07 11:27:02 +02:00
parent 28893b9695
commit 87b14e8761
2 changed files with 10 additions and 13 deletions

View file

@ -11,7 +11,7 @@ from rest_framework.serializers import BaseSerializer
from structlog import get_logger from structlog import get_logger
from passbook.core.types import UIUserSettings from passbook.core.types import UIUserSettings
from passbook.lib.models import SerializerModel from passbook.lib.models import InheritanceForeignKey, SerializerModel
from passbook.policies.models import PolicyBindingModel from passbook.policies.models import PolicyBindingModel
if TYPE_CHECKING: if TYPE_CHECKING:
@ -150,7 +150,7 @@ class FlowStageBinding(SerializerModel, PolicyBindingModel):
fsb_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4) fsb_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
target = models.ForeignKey("Flow", on_delete=models.CASCADE) target = models.ForeignKey("Flow", on_delete=models.CASCADE)
stage = models.ForeignKey(Stage, on_delete=models.CASCADE) stage = InheritanceForeignKey(Stage, on_delete=models.CASCADE)
re_evaluate_policies = models.BooleanField( re_evaluate_policies = models.BooleanField(
default=False, default=False,

View file

@ -156,25 +156,22 @@ class FlowPlanner:
if default_context: if default_context:
plan.context = default_context plan.context = default_context
# Check Flow policies # Check Flow policies
for stage in ( for binding in FlowStageBinding.objects.filter(
self.flow.stages.order_by("flowstagebinding__order") target__pk=self.flow.pk
.select_subclasses() ).order_by("order"):
.select_related()
):
binding: FlowStageBinding = stage.flowstagebinding_set.get(
target__pk=self.flow.pk
)
engine = PolicyEngine(binding, user, request) engine = PolicyEngine(binding, user, request)
engine.request.context = plan.context engine.request.context = plan.context
engine.build() engine.build()
if engine.passing: if engine.passing:
LOGGER.debug("f(plan): Stage passing", stage=stage, flow=self.flow) LOGGER.debug(
plan.stages.append(stage) "f(plan): Stage passing", stage=binding.stage, flow=self.flow
)
plan.stages.append(binding.stage)
marker = StageMarker() marker = StageMarker()
if binding.re_evaluate_policies: if binding.re_evaluate_policies:
LOGGER.debug( LOGGER.debug(
"f(plan): Stage has re-evaluate marker", "f(plan): Stage has re-evaluate marker",
stage=stage, stage=binding.stage,
flow=self.flow, flow=self.flow,
) )
marker = ReevaluateMarker(binding=binding, user=user) marker = ReevaluateMarker(binding=binding, user=user)