| Conditions | 6 |
| Total Lines | 36 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | """ |
||
| 16 | def processes_graph_model(): |
||
| 17 | """ |
||
| 18 | Create a graph model representing processes and their parent-child relationships. |
||
| 19 | |||
| 20 | Returns: |
||
| 21 | GraphModel: A graph model representing processes and their parent-child relationships. |
||
| 22 | """ |
||
| 23 | |||
| 24 | graph_model = graphinate.model("Processes Graph") |
||
| 25 | |||
| 26 | def processes() -> Iterable[psutil.Process]: |
||
| 27 | for pid in psutil.pids(): |
||
| 28 | if psutil.pid_exists(pid): |
||
| 29 | yield psutil.Process(pid) |
||
| 30 | |||
| 31 | processes_list = [ |
||
| 32 | { |
||
| 33 | 'pid': p.pid, |
||
| 34 | 'name': p.name(), |
||
| 35 | 'parent_pid': p.parent().pid if p.parent() else None |
||
| 36 | } |
||
| 37 | for p in processes() |
||
| 38 | ] |
||
| 39 | |||
| 40 | @graph_model.node(key=operator.itemgetter('pid'), label=operator.itemgetter('name')) |
||
| 41 | def process(): |
||
| 42 | yield from processes_list |
||
| 43 | |||
| 44 | @graph_model.edge() |
||
| 45 | def edge(): |
||
| 46 | for p in processes_list: |
||
| 47 | parent_pid = p.get('parent_pid') |
||
| 48 | if parent_pid: |
||
| 49 | yield {'source': p.get('pid'), 'target': parent_pid} |
||
| 50 | |||
| 51 | return graph_model |
||
| 52 | |||
| 71 |