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