|
| 1 | +from .runtime_state import runtime_state, check_and_reload_data |
| 2 | +from flask import Blueprint, jsonify, request |
| 3 | +import graphviz |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | + |
| 8 | +task_subgraphs_bp = Blueprint('task_subgraphs', __name__, url_prefix='/api') |
| 9 | + |
| 10 | +@task_subgraphs_bp.route('/task-subgraphs') |
| 11 | +@check_and_reload_data() |
| 12 | +def get_task_subgraphs(): |
| 13 | + try: |
| 14 | + data = {} |
| 15 | + |
| 16 | + subgraph_id = request.args.get('subgraph_id') |
| 17 | + if not subgraph_id: |
| 18 | + return jsonify({'error': 'Subgraph ID is required'}), 400 |
| 19 | + try: |
| 20 | + subgraph_id = int(subgraph_id) |
| 21 | + except Exception: |
| 22 | + return jsonify({'error': 'Invalid subgraph ID'}), 400 |
| 23 | + |
| 24 | + plot_unsuccessful_task = request.args.get('plot_failed_task', 'true').lower() == 'true' |
| 25 | + plot_recovery_task = request.args.get('plot_recovery_task', 'true').lower() == 'true' |
| 26 | + |
| 27 | + subgraph = runtime_state.subgraphs.get(subgraph_id) |
| 28 | + if not subgraph: |
| 29 | + return jsonify({'error': 'Subgraph not found'}), 404 |
| 30 | + task_tries = list(subgraph) |
| 31 | + |
| 32 | + svg_file_path_without_suffix = os.path.join( |
| 33 | + runtime_state.data_parser.svg_files_dir, |
| 34 | + f'task-subgraph-{subgraph_id}-{plot_unsuccessful_task}-{plot_recovery_task}' |
| 35 | + ) |
| 36 | + svg_file_path = f'{svg_file_path_without_suffix}.svg' |
| 37 | + |
| 38 | + if not Path(svg_file_path).exists(): |
| 39 | + dot = graphviz.Digraph() |
| 40 | + |
| 41 | + def plot_task_node(dot, task): |
| 42 | + node_id = f'{task.task_id}-{task.task_try_id}' |
| 43 | + node_label = f'{task.task_id}' |
| 44 | + |
| 45 | + if task.when_failure_happens: |
| 46 | + node_label = f'{node_label} (unsuccessful)' |
| 47 | + style = 'dashed' |
| 48 | + color = '#FF0000' |
| 49 | + fontcolor = '#FF0000' |
| 50 | + else: |
| 51 | + style = 'solid' |
| 52 | + color = '#000000' |
| 53 | + fontcolor = '#000000' |
| 54 | + |
| 55 | + if task.is_recovery_task: |
| 56 | + node_label = f'{node_label} (recovery)' |
| 57 | + style = 'filled,dashed' |
| 58 | + fillcolor = '#FF69B4' |
| 59 | + else: |
| 60 | + fillcolor = '#FFFFFF' |
| 61 | + |
| 62 | + dot.node(node_id, node_label, shape='ellipse', style=style, color=color, fontcolor=fontcolor, fillcolor=fillcolor) |
| 63 | + |
| 64 | + def plot_file_node(dot, file): |
| 65 | + if len(file.producers) == 0: |
| 66 | + return |
| 67 | + file_name = file.filename |
| 68 | + dot.node(file_name, file_name, shape='box') |
| 69 | + |
| 70 | + def plot_task2file_edge(dot, task, file): |
| 71 | + if len(file.producers) == 0: |
| 72 | + return |
| 73 | + if task.when_failure_happens: |
| 74 | + return |
| 75 | + else: |
| 76 | + task_execution_time = task.time_worker_end - task.time_worker_start |
| 77 | + dot.edge(f'{task.task_id}-{task.task_try_id}', file.filename, label=f'{task_execution_time:.2f}s') |
| 78 | + |
| 79 | + def plot_file2task_edge(dot, file, task): |
| 80 | + if len(file.producers) == 0: |
| 81 | + return |
| 82 | + file_creation_time = float('inf') |
| 83 | + for producer_task_id, producer_task_try_id in file.producers: |
| 84 | + producer_task = runtime_state.tasks[(producer_task_id, producer_task_try_id)] |
| 85 | + if producer_task.time_worker_end: |
| 86 | + file_creation_time = min(file_creation_time, producer_task.time_worker_end) |
| 87 | + file_creation_time = file_creation_time - runtime_state.MIN_TIME |
| 88 | + |
| 89 | + dot.edge(file.filename, f'{task.task_id}-{task.task_try_id}', label=f'{file_creation_time:.2f}s') |
| 90 | + |
| 91 | + for (tid, try_id) in task_tries: |
| 92 | + task = runtime_state.tasks[(tid, try_id)] |
| 93 | + if task.is_recovery_task and not plot_recovery_task: |
| 94 | + continue |
| 95 | + if task.when_failure_happens and not plot_unsuccessful_task: |
| 96 | + continue |
| 97 | + plot_task_node(dot, task) |
| 98 | + for file_name in getattr(task, 'input_files', []): |
| 99 | + file = runtime_state.files[file_name] |
| 100 | + plot_file_node(dot, file) |
| 101 | + plot_file2task_edge(dot, file, task) |
| 102 | + for file_name in getattr(task, 'output_files', []): |
| 103 | + file = runtime_state.files[file_name] |
| 104 | + if len(file.transfers) == 0: |
| 105 | + continue |
| 106 | + plot_file_node(dot, file) |
| 107 | + plot_task2file_edge(dot, task, file) |
| 108 | + dot.attr(rankdir='TB') |
| 109 | + dot.engine = 'dot' |
| 110 | + dot.render(svg_file_path_without_suffix, format='svg', view=False) |
| 111 | + |
| 112 | + data['subgraph_id'] = subgraph_id |
| 113 | + data['num_task_tries'] = len(task_tries) |
| 114 | + data['subgraph_svg_content'] = open(svg_file_path, 'r').read() |
| 115 | + |
| 116 | + return jsonify(data) |
| 117 | + except Exception as e: |
| 118 | + runtime_state.logger.error(f'Error in get_task_subgraphs: {e}') |
| 119 | + return jsonify({'error': str(e)}), 500 |
0 commit comments