summaryrefslogtreecommitdiff
path: root/machine.sort.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-21 19:00:08 -0600
committerJeff Carr <[email protected]>2024-11-21 19:00:08 -0600
commit1f0a18a0a8e21a831dbc002b8b4110298739b875 (patch)
tree0b8981422d4722f76590f762b1a0a93f437b5cb5 /machine.sort.go
parent5581a4eb9c500b9fede39cb5b0202b53ba66fbc2 (diff)
prepare to autogen these files
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'machine.sort.go')
-rw-r--r--machine.sort.go146
1 files changed, 146 insertions, 0 deletions
diff --git a/machine.sort.go b/machine.sort.go
new file mode 100644
index 0000000..3766ae7
--- /dev/null
+++ b/machine.sort.go
@@ -0,0 +1,146 @@
+package zoopb
+
+// this is becoming a standard format
+// todo: autogenerate this from the .proto file?
+
+import (
+ "fmt"
+ "os"
+ "sort"
+ sync "sync"
+ "time"
+)
+
+// bad global lock until I figure out some other plan
+var machinesLock sync.RWMutex
+
+type MachineIterator struct {
+ sync.RWMutex
+
+ packs []*Machine
+ index int
+}
+
+// NewMachineIterator initializes a new iterator.
+func NewMachineIterator(packs []*Machine) *MachineIterator {
+ return &MachineIterator{packs: packs}
+}
+
+// Scan moves to the next element and returns false if there are no more packs.
+func (it *MachineIterator) Scan() bool {
+ if it.index >= len(it.packs) {
+ return false
+ }
+ it.index++
+ return true
+}
+
+// Machine returns the current repo.
+func (it *MachineIterator) Machine() *Machine {
+ if it.packs[it.index-1] == nil {
+ for i, d := range it.packs {
+ fmt.Println("i =", i, d)
+ }
+ fmt.Println("len =", len(it.packs))
+ fmt.Println("repo == nil", it.index, it.index-1)
+ os.Exit(-1)
+ }
+ return it.packs[it.index-1]
+}
+
+// Use Scan() in a loop, similar to a while loop
+//
+// for iterator.Scan() {
+// d := iterator.Machine()
+// fmt.Println("Machine UUID:", d.Uuid)
+// }
+
+func (r *Machines) All() *MachineIterator {
+ repoPointers := r.selectAllMachines()
+
+ iterator := NewMachineIterator(repoPointers)
+ return iterator
+}
+
+func (r *Machines) SortByName() *MachineIterator {
+ packs := r.selectAllMachines()
+
+ sort.Sort(ByMachineName(packs))
+
+ iterator := NewMachineIterator(packs)
+ return iterator
+}
+
+// enforces no duplicate package names
+func (r *Machines) Append(newP *Machine) bool {
+ machinesLock.Lock()
+ defer machinesLock.Unlock()
+
+ for _, p := range r.Machines {
+ if p.Hostname == newP.Hostname {
+ return false
+ }
+ }
+
+ r.Machines = append(r.Machines, newP)
+ return true
+}
+
+// returns time.Duration since last Update()
+func (r *Machine) Age(newP *Machine) time.Duration {
+ t := time.Since(r.Laststamp.AsTime())
+ return t
+}
+
+// find a machine by name
+func (r *Machines) FindByName(name string) *Machine {
+ machinesLock.RLock()
+ defer machinesLock.RUnlock()
+
+ for _, p := range r.Machines {
+ if p.Hostname == name {
+ return p
+ }
+ }
+
+ return nil
+}
+
+// find a package by name
+func (m *Machine) FindPackageByName(name string) *Package {
+ if m == nil {
+ return nil
+ }
+ if m.Packages == nil {
+ return nil
+ }
+ return m.Packages.FindByName(name)
+}
+
+func (r *Machines) Len() int {
+ machinesLock.RLock()
+ defer machinesLock.RUnlock()
+
+ return len(r.Machines)
+}
+
+type ByMachineName []*Machine
+
+func (a ByMachineName) Len() int { return len(a) }
+func (a ByMachineName) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname }
+func (a ByMachineName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+
+// safely returns a slice of pointers to the Machine protobufs
+func (r *Machines) selectAllMachines() []*Machine {
+ machinesLock.RLock()
+ defer machinesLock.RUnlock()
+
+ // Create a new slice to hold pointers to each Machine
+ var allPacks []*Machine
+ allPacks = make([]*Machine, len(r.Machines))
+ for i, p := range r.Machines {
+ allPacks[i] = p // Copy pointers for safe iteration
+ }
+
+ return allPacks
+}