from collections import deque from django.utils import timezone from django.core.exceptions import ValidationError 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', []) 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 execute_flow_dag(flow_or_graph, initial_context=None, idempotency_key=None, depth=0): 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(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_type = node.get('type') node_data = node.get('data', {}) 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 field = node_data.get('field') val = node_data.get('value') record_id = context.get('record_id') record_type = context.get('record_type') if field and val is not None 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: inst = model_cls.objects.filter(pk=record_id).first() if inst and hasattr(inst, field): setattr(inst, field, val) inst.save() elif node_type == 'assignment': 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', []) winning_handle = None for outcome in outcomes: if evaluate_outcome(outcome, context): winning_handle = outcome.get('id') break # Disable paths from unchosen handles node_edges = outgoing_edges.get(node_id, []) for e in node_edges: handle = e.get('sourceHandle') target = e.get('target') if handle != winning_handle: # Disable target and all downstream exclusively reached nodes downstream = [target] visited_sub = set() while downstream: d = downstream.pop() if d not in visited_sub: visited_sub.add(d) disabled_nodes.add(d) for nxt_edge in outgoing_edges.get(d, []): downstream.append(nxt_edge.get('target')) elif node_type == 'subflow': child_flow_id = node_data.get('flow_id') if not child_flow_id: raise ValidationError("Subflow node requires flow_id.") child_flow = FlowDefinition.objects.filter(pk=child_flow_id).first() if not child_flow: raise ValidationError(f"Referenced child flow {child_flow_id} not found.") child_res = execute_flow_dag( child_flow.graph, initial_context=context, depth=depth + 1 ) 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