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 passbook.core.types import UIUserSettings
from passbook.lib.models import SerializerModel
from passbook.lib.models import InheritanceForeignKey, SerializerModel
from passbook.policies.models import PolicyBindingModel
if TYPE_CHECKING:
@ -150,7 +150,7 @@ class FlowStageBinding(SerializerModel, PolicyBindingModel):
fsb_uuid = models.UUIDField(primary_key=True, editable=False, default=uuid4)
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(
default=False,

View File

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