github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/store/summary.go (about)

     1  package store
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"k8s.io/apimachinery/pkg/types"
     8  )
     9  
    10  // Represents all the IDs of a particular type of resource
    11  // that have changed.
    12  type ChangeSet struct {
    13  	Changes map[types.NamespacedName]bool
    14  }
    15  
    16  func NewChangeSet(names ...types.NamespacedName) ChangeSet {
    17  	cs := ChangeSet{}
    18  	for _, name := range names {
    19  		cs.Add(name)
    20  	}
    21  	return cs
    22  }
    23  
    24  func (s *ChangeSet) Empty() bool {
    25  	return len(s.Changes) == 0
    26  }
    27  
    28  // Add a changed resource name.
    29  func (s *ChangeSet) Add(nn types.NamespacedName) {
    30  	if s.Changes == nil {
    31  		s.Changes = make(map[types.NamespacedName]bool)
    32  	}
    33  	s.Changes[nn] = true
    34  }
    35  
    36  // Merge another change set into this one.
    37  func (s *ChangeSet) AddAll(other ChangeSet) {
    38  	if len(other.Changes) > 0 {
    39  		if s.Changes == nil {
    40  			s.Changes = make(map[types.NamespacedName]bool)
    41  		}
    42  		for k, v := range other.Changes {
    43  			s.Changes[k] = v
    44  		}
    45  	}
    46  }
    47  
    48  // Summarize the changes to the EngineState since the last change.
    49  type ChangeSummary struct {
    50  	// True if we saw one or more legacy actions that don't know how
    51  	// to summarize their changes.
    52  	Legacy bool
    53  
    54  	// True if this change added logs.
    55  	Log bool
    56  
    57  	// Cmds with their specs changed.
    58  	CmdSpecs ChangeSet
    59  
    60  	UISessions  ChangeSet
    61  	UIResources ChangeSet
    62  	UIButtons   ChangeSet
    63  
    64  	Clusters ChangeSet
    65  
    66  	// If non-zero, that means we tried to apply this change and got
    67  	// an error.
    68  	LastBackoff time.Duration
    69  }
    70  
    71  func (s ChangeSummary) IsLogOnly() bool {
    72  	return cmp.Equal(s, ChangeSummary{Log: true})
    73  }
    74  
    75  func (s *ChangeSummary) Add(other ChangeSummary) {
    76  	s.Legacy = s.Legacy || other.Legacy
    77  	s.Log = s.Log || other.Log
    78  	s.CmdSpecs.AddAll(other.CmdSpecs)
    79  	s.UISessions.AddAll(other.UISessions)
    80  	s.UIResources.AddAll(other.UIResources)
    81  	s.Clusters.AddAll(other.Clusters)
    82  	if other.LastBackoff > s.LastBackoff {
    83  		s.LastBackoff = other.LastBackoff
    84  	}
    85  }
    86  
    87  func LegacyChangeSummary() ChangeSummary {
    88  	return ChangeSummary{Legacy: true}
    89  }
    90  
    91  type Summarizer interface {
    92  	Action
    93  
    94  	Summarize(summary *ChangeSummary)
    95  }