From 3f9356c6a77541668d9b83a8183231c1fed51f92 Mon Sep 17 00:00:00 2001 From: Laszlo Fogas Date: Thu, 6 Jun 2019 09:41:42 +0200 Subject: [PATCH] Doh, this wasn't a proper tree --- model/proc.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/model/proc.go b/model/proc.go index c547725e6..8740c0737 100644 --- a/model/proc.go +++ b/model/proc.go @@ -14,6 +14,8 @@ package model +import "fmt" + // ProcStore persists process information to storage. type ProcStore interface { ProcLoad(int64) (*Proc, error) @@ -57,18 +59,24 @@ func (p *Proc) Failing() bool { // Tree creates a process tree from a flat process list. func Tree(procs []*Proc) []*Proc { - var ( - nodes []*Proc - parent *Proc - ) + var nodes []*Proc for _, proc := range procs { if proc.PPID == 0 { nodes = append(nodes, proc) - parent = proc - continue } else { + parent, _ := findNode(nodes, proc.PPID) parent.Children = append(parent.Children, proc) } } return nodes } + +func findNode(nodes []*Proc, pid int) (*Proc, error) { + for _, node := range nodes { + if node.PID == pid { + return node, nil + } + } + + return nil, fmt.Errorf("Corrupt proc structure") +}