debug.go 1.79 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
package autoradio

import "sort"

// MountStatus reports the configuration and status of a mount,
// including eventual transcoded mounts that source it.
type MountStatus struct {
	Mount       *Mount
	Listeners   int
	TransMounts []*MountStatus
}

func newMountStatus(m *Mount, nodes []*NodeStatus) *MountStatus {
	var listeners int
	for _, n := range nodes {
		for _, ims := range n.Mounts {
			if ims.Name == m.Name {
				listeners += ims.Listeners
				break
			}
		}
	}
	return &MountStatus{
		Mount:     m,
		Listeners: listeners,
	}
}

type mountStatusList []*MountStatus

func (l mountStatusList) Len() int      { return len(l) }
func (l mountStatusList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l mountStatusList) Less(i, j int) bool {
	return l[i].Mount.Name < l[j].Mount.Name
}

// MountsToStatus converts a list of mounts (and eventually the
// current list of nodes) to a nicely sorted and tree-aggregated list
// of MountStatus objects. The list of nodes can be nil, in which case
// listener statistics will be omitted.
func MountsToStatus(mounts []*Mount, nodes []*NodeStatus) []*MountStatus {
	// Aggregate stats, and create a tree of transcoding mounts.
	ms := make(map[string]*MountStatus)
	for _, m := range mounts {
		if m.HasTranscoder() {
			continue
		}
		ms[m.Name] = newMountStatus(m, nodes)
	}
	for _, m := range mounts {
		if !m.HasTranscoder() {
			continue
		}
		src := ms[m.Transcoding.SourceName]
		if src == nil {
			continue
		}
		src.TransMounts = append(src.TransMounts, newMountStatus(m, nodes))
	}
	msl := make([]*MountStatus, 0, len(ms))
	for _, m := range ms {
		msl = append(msl, m)
	}

	// Sort everything (including transcoded mounts).
	sort.Sort(mountStatusList(msl))
	for _, s := range msl {
		if s.TransMounts != nil {
			sort.Sort(mountStatusList(s.TransMounts))
		}
	}
	return msl
}