422 lines
16 KiB
Python
422 lines
16 KiB
Python
from collections import deque
|
|
from django.utils import timezone
|
|
from django.core.exceptions import ValidationError, PermissionDenied
|
|
|
|
|
|
def validate_flow_dag(graph):
|
|
"""
|
|
Validates that the Vue Flow graph has no cycles and forms a valid DAG using Kahn's algorithm.
|
|
Also validates that all edges point to real node IDs.
|
|
"""
|
|
nodes = graph.get('nodes', [])
|
|
edges = graph.get('edges', [])
|
|
|
|
if not nodes:
|
|
return True
|
|
|
|
node_ids = {n['id'] for n in nodes if 'id' in n}
|
|
|
|
# Build adjacency and in-degree map
|
|
in_degree = {nid: 0 for nid in node_ids}
|
|
adj = {nid: [] for nid in node_ids}
|
|
|
|
for edge in edges:
|
|
src = edge.get('source')
|
|
tgt = edge.get('target')
|
|
|
|
# Dangling edge validation
|
|
if src not in node_ids or tgt not in node_ids:
|
|
raise ValidationError(f"Edge references non-existent node: {src} -> {tgt}")
|
|
|
|
adj[src].append(tgt)
|
|
in_degree[tgt] += 1
|
|
|
|
# Kahn's algorithm
|
|
queue = deque([nid for nid in node_ids if in_degree[nid] == 0])
|
|
visited_count = 0
|
|
|
|
while queue:
|
|
curr = queue.popleft()
|
|
visited_count += 1
|
|
|
|
for neighbor in adj[curr]:
|
|
in_degree[neighbor] -= 1
|
|
if in_degree[neighbor] == 0:
|
|
queue.append(neighbor)
|
|
|
|
if visited_count != len(node_ids):
|
|
raise ValidationError("Workflow graph contains cycles or self-referencing loops.")
|
|
|
|
return True
|
|
|
|
|
|
def topological_sort(graph):
|
|
"""
|
|
Returns ordered list of node IDs to execute in topological DAG sequence.
|
|
"""
|
|
validate_flow_dag(graph)
|
|
|
|
nodes = graph.get('nodes', [])
|
|
edges = graph.get('edges', [])
|
|
node_ids = [n['id'] for n in nodes if 'id' in n]
|
|
|
|
in_degree = {nid: 0 for nid in node_ids}
|
|
adj = {nid: [] for nid in node_ids}
|
|
|
|
for edge in edges:
|
|
src = edge.get('source')
|
|
tgt = edge.get('target')
|
|
adj[src].append(tgt)
|
|
in_degree[tgt] += 1
|
|
|
|
queue = deque([nid for nid in node_ids if in_degree[nid] == 0])
|
|
execution_order = []
|
|
|
|
while queue:
|
|
curr = queue.popleft()
|
|
execution_order.append(curr)
|
|
|
|
for neighbor in adj[curr]:
|
|
in_degree[neighbor] -= 1
|
|
if in_degree[neighbor] == 0:
|
|
queue.append(neighbor)
|
|
|
|
return execution_order
|
|
|
|
|
|
get_topological_execution_order = topological_sort
|
|
|
|
|
|
def evaluate_condition(cond, context):
|
|
field = cond.get('field')
|
|
op = cond.get('operator')
|
|
target_val = cond.get('value')
|
|
actual_val = context.get(field)
|
|
|
|
if op in ('gte', '>='):
|
|
return actual_val is not None and actual_val >= target_val
|
|
elif op in ('lte', '<='):
|
|
return actual_val is not None and actual_val <= target_val
|
|
elif op in ('gt', '>'):
|
|
return actual_val is not None and actual_val > target_val
|
|
elif op in ('lt', '<'):
|
|
return actual_val is not None and actual_val < target_val
|
|
elif op in ('eq', 'equals', '=='):
|
|
return actual_val == target_val
|
|
elif op in ('ne', 'not_equals', '!='):
|
|
return actual_val != target_val
|
|
elif op in ('contains',):
|
|
return target_val in str(actual_val or '')
|
|
return False
|
|
|
|
|
|
def evaluate_outcome(outcome, context):
|
|
conditions = outcome.get('conditions') or outcome.get('rules') or []
|
|
if not conditions:
|
|
return True # Default / Else branch
|
|
logic = outcome.get('logic', 'AND').upper()
|
|
results = [evaluate_condition(c, context) for c in conditions]
|
|
if logic == 'AND':
|
|
return all(results)
|
|
elif logic == 'OR':
|
|
return any(results)
|
|
return False
|
|
|
|
|
|
def _evaluate_decision_node(node_data, context):
|
|
"""
|
|
Evaluates a Decision node either using outcomes list or conditions list.
|
|
Returns (matched_handle, target_node_id).
|
|
"""
|
|
outcomes = node_data.get('outcomes', [])
|
|
if outcomes:
|
|
for outcome in outcomes:
|
|
conds = outcome.get('conditions', [])
|
|
logic = outcome.get('logic', 'ALL')
|
|
if _evaluate_condition(conds, logic, context):
|
|
return outcome.get('handle', 'default'), outcome.get('target_node')
|
|
return 'default', node_data.get('default_target_node')
|
|
|
|
conditions = node_data.get('conditions', [])
|
|
if conditions:
|
|
for cond in conditions:
|
|
field = cond.get('field')
|
|
op = cond.get('op') or cond.get('operator')
|
|
val = cond.get('value')
|
|
target = cond.get('target_node')
|
|
if _evaluate_single_condition(field, op, val, context):
|
|
return 'match', target
|
|
return 'default', node_data.get('default_target_node')
|
|
|
|
return 'default', node_data.get('default_target_node')
|
|
|
|
|
|
def execute_flow_dag(flow_or_graph, initial_context=None, idempotency_key=None, depth=0, user=None):
|
|
from .models import FlowDefinition, FlowExecutionLog
|
|
|
|
MAX_SUBFLOW_DEPTH = 10
|
|
if depth >= MAX_SUBFLOW_DEPTH:
|
|
raise ValidationError(f"Subflow recursion depth limit exceeded (max: {MAX_SUBFLOW_DEPTH}).")
|
|
|
|
is_dict = isinstance(flow_or_graph, dict)
|
|
graph = flow_or_graph if is_dict else flow_or_graph.graph
|
|
flow_def = None if is_dict else flow_or_graph
|
|
|
|
validate_flow_dag(graph)
|
|
|
|
if not is_dict and idempotency_key:
|
|
existing = FlowExecutionLog.objects.filter(flow=flow_def, idempotency_key=idempotency_key).first()
|
|
if existing:
|
|
return existing
|
|
|
|
log = None
|
|
if not is_dict and flow_def:
|
|
log = FlowExecutionLog.objects.create(
|
|
flow=flow_def,
|
|
status='running',
|
|
context_data=initial_context or {},
|
|
idempotency_key=idempotency_key
|
|
)
|
|
|
|
try:
|
|
nodes = graph.get('nodes', [])
|
|
edges = graph.get('edges', [])
|
|
node_map = {n['id']: n for n in nodes if 'id' in n}
|
|
|
|
# Build outgoing edge maps
|
|
outgoing_edges = {}
|
|
for edge in edges:
|
|
src = edge.get('source')
|
|
outgoing_edges.setdefault(src, []).append(edge)
|
|
|
|
# Topological sorting
|
|
in_degree = {nid: 0 for nid in node_map}
|
|
adj = {nid: [] for nid in node_map}
|
|
for edge in edges:
|
|
src = edge.get('source')
|
|
tgt = edge.get('target')
|
|
adj[src].append(tgt)
|
|
in_degree[tgt] += 1
|
|
|
|
queue = deque([nid for nid in node_map if in_degree[nid] == 0])
|
|
topo_order = []
|
|
while queue:
|
|
curr = queue.popleft()
|
|
topo_order.append(curr)
|
|
for neighbor in adj[curr]:
|
|
in_degree[neighbor] -= 1
|
|
if in_degree[neighbor] == 0:
|
|
queue.append(neighbor)
|
|
|
|
# Determine reachable nodes taking decision branches into account
|
|
reachable_nodes = set(topo_order)
|
|
disabled_nodes = set()
|
|
|
|
context = dict(initial_context or {})
|
|
steps = []
|
|
|
|
for node_id in topo_order:
|
|
if node_id in disabled_nodes:
|
|
continue
|
|
|
|
node = node_map.get(node_id, {})
|
|
node_data = node.get('data', {})
|
|
node_type = node.get('type') or node_data.get('type')
|
|
|
|
step_record = {
|
|
'node_id': node_id,
|
|
'type': node_type,
|
|
'status': 'success'
|
|
}
|
|
|
|
if node_type == 'action':
|
|
action_name = node_data.get('action')
|
|
if action_name == 'update_record':
|
|
field = node_data.get('field')
|
|
val = node_data.get('value')
|
|
context.setdefault('record_data', {})[field] = val
|
|
elif action_name == 'set_stage':
|
|
context['stage'] = node_data.get('stage')
|
|
elif action_name == 'increment_score':
|
|
pts = node_data.get('points', 0)
|
|
context['score'] = context.get('score', 0) + pts
|
|
|
|
elif node_type == 'update_record':
|
|
for k, v in node_data.items():
|
|
context[k] = v
|
|
# Check if this flow was triggered by a model record and update DB record if field/value specified
|
|
record_id = context.get('record_id')
|
|
record_type = context.get('record_type') or node_data.get('target_model')
|
|
|
|
# Check for direct field/value or field_updates dict
|
|
updates = {}
|
|
if node_data.get('field') and node_data.get('value') is not None:
|
|
updates[node_data.get('field')] = node_data.get('value')
|
|
if isinstance(node_data.get('field_updates'), dict):
|
|
updates.update(node_data.get('field_updates'))
|
|
|
|
if updates and record_id and record_type:
|
|
from django.apps import apps
|
|
model_cls = apps.get_model('crm_core', record_type)
|
|
if not model_cls:
|
|
model_cls = apps.get_model('custom_objects', record_type)
|
|
if model_cls:
|
|
if not user:
|
|
# Fail-closed: unauthenticated / missing identity cannot perform DB writes
|
|
raise PermissionDenied("Missing execution identity for update_record.")
|
|
|
|
qs = model_cls.objects.filter(pk=record_id)
|
|
if not (user.is_superuser or user.is_staff):
|
|
if hasattr(model_cls, 'owner'):
|
|
qs = qs.filter(owner=user)
|
|
elif hasattr(model_cls, 'assigned_to'):
|
|
qs = qs.filter(assigned_to=user)
|
|
elif hasattr(model_cls, 'created_by'):
|
|
qs = qs.filter(created_by=user)
|
|
else:
|
|
raise PermissionDenied(f"Permission denied: no ownership policy defined for model {record_type}")
|
|
|
|
inst = qs.first()
|
|
if not inst:
|
|
raise PermissionDenied(f"Permission denied: cannot update unauthorized record {record_type}:{record_id}")
|
|
else:
|
|
inst = qs.first()
|
|
if inst:
|
|
for f_name, f_val in updates.items():
|
|
if hasattr(inst, f_name):
|
|
setattr(inst, f_name, f_val)
|
|
inst.save()
|
|
|
|
elif node_type == 'assignment':
|
|
# Handle single assignment fields or assignments array
|
|
assignments = node_data.get('assignments')
|
|
if assignments and isinstance(assignments, list):
|
|
for item in assignments:
|
|
var_name = item.get('variable')
|
|
op = item.get('op') or item.get('operator', 'equals')
|
|
val = item.get('value')
|
|
if var_name:
|
|
if op in ('equals', '=', None):
|
|
context[var_name] = val
|
|
elif op in ('add', '+='):
|
|
curr = context.get(var_name, 0)
|
|
context[var_name] = curr + val
|
|
elif op in ('subtract', '-='):
|
|
curr = context.get(var_name, 0)
|
|
context[var_name] = curr - val
|
|
else:
|
|
var_name = node_data.get('variable')
|
|
op = node_data.get('operator', 'equals')
|
|
val = node_data.get('value')
|
|
if var_name:
|
|
if op in ('equals', '=', None):
|
|
context[var_name] = val
|
|
elif op in ('add', '+='):
|
|
curr = context.get(var_name, 0)
|
|
context[var_name] = curr + val
|
|
elif op in ('subtract', '-='):
|
|
curr = context.get(var_name, 0)
|
|
context[var_name] = curr - val
|
|
|
|
elif node_type == 'decision':
|
|
outcomes = node_data.get('outcomes', [])
|
|
conditions = node_data.get('conditions', [])
|
|
winning_handle = None
|
|
direct_target = None
|
|
|
|
if outcomes:
|
|
for outcome in outcomes:
|
|
if evaluate_outcome(outcome, context):
|
|
winning_handle = outcome.get('id')
|
|
break
|
|
elif conditions:
|
|
for cond in conditions:
|
|
field = cond.get('field')
|
|
op = cond.get('op') or cond.get('operator')
|
|
val = cond.get('value')
|
|
tgt = cond.get('target_node')
|
|
if evaluate_condition({'field': field, 'operator': op, 'value': val}, context):
|
|
direct_target = tgt
|
|
break
|
|
if not direct_target:
|
|
direct_target = node_data.get('default_target_node') or node_data.get('default_target')
|
|
|
|
# The decision routes only down the winning outcome handle or direct target
|
|
node_edges = outgoing_edges.get(node_id, [])
|
|
chosen_targets = set()
|
|
unchosen_targets = set()
|
|
for e in node_edges:
|
|
handle = e.get('sourceHandle')
|
|
target = e.get('target')
|
|
if direct_target:
|
|
if target == direct_target:
|
|
chosen_targets.add(target)
|
|
else:
|
|
unchosen_targets.add(target)
|
|
elif winning_handle:
|
|
if handle == winning_handle:
|
|
chosen_targets.add(target)
|
|
else:
|
|
unchosen_targets.add(target)
|
|
else:
|
|
unchosen_targets.add(target)
|
|
|
|
# For every unchosen target branch, disable it ONLY if it is not reachable
|
|
# from any chosen branch (handles diamond join topologies)
|
|
def get_all_reachable(starts):
|
|
reachable = set()
|
|
queue = list(starts)
|
|
while queue:
|
|
curr = queue.pop()
|
|
if curr not in reachable:
|
|
reachable.add(curr)
|
|
for nxt_edge in outgoing_edges.get(curr, []):
|
|
queue.append(nxt_edge.get('target'))
|
|
return reachable
|
|
|
|
chosen_reachable = get_all_reachable(chosen_targets)
|
|
unchosen_exclusive = get_all_reachable(unchosen_targets) - chosen_reachable
|
|
for d in unchosen_exclusive:
|
|
disabled_nodes.add(d)
|
|
|
|
elif node_type == 'subflow':
|
|
child_flow_id = node_data.get('flow_id') or node_data.get('subflow_id')
|
|
if not child_flow_id:
|
|
raise ValidationError("Subflow node requires flow_id.")
|
|
child_qs = FlowDefinition.objects.filter(pk=child_flow_id)
|
|
if user and not (user.is_superuser or user.is_staff):
|
|
child_qs = child_qs.filter(created_by=user)
|
|
child_flow = child_qs.first()
|
|
if not child_flow:
|
|
raise ValidationError(f"Referenced child flow {child_flow_id} not found or unauthorized.")
|
|
|
|
child_res = execute_flow_dag(
|
|
child_flow.graph,
|
|
initial_context=context,
|
|
depth=depth + 1,
|
|
user=user
|
|
)
|
|
if isinstance(child_res, dict):
|
|
context.update(child_res)
|
|
elif hasattr(child_res, 'context_data'):
|
|
context.update(child_res.context_data)
|
|
|
|
steps.append(step_record)
|
|
|
|
if log:
|
|
log.status = 'completed'
|
|
log.steps_executed = steps
|
|
log.context_data = context
|
|
log.completed_at = timezone.now()
|
|
log.save()
|
|
return log
|
|
|
|
return context
|
|
|
|
except Exception as e:
|
|
if log:
|
|
log.status = 'failed'
|
|
log.error_message = str(e)
|
|
log.completed_at = timezone.now()
|
|
log.save()
|
|
raise
|