...

Source file src/sigs.k8s.io/cli-utils/pkg/print/stats/stats.go

Documentation: sigs.k8s.io/cli-utils/pkg/print/stats

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package stats
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"sigs.k8s.io/cli-utils/pkg/apply/event"
    10  )
    11  
    12  // Stats captures the summarized numbers from apply/prune/delete and
    13  // reconciliation of resources. Each item in a stats list represents the stats
    14  // from all the events in a single action group.
    15  type Stats struct {
    16  	ApplyStats  ApplyStats
    17  	PruneStats  PruneStats
    18  	DeleteStats DeleteStats
    19  	WaitStats   WaitStats
    20  }
    21  
    22  // FailedActuationSum returns the number of resources that failed actuation.
    23  func (s *Stats) FailedActuationSum() int {
    24  	return s.ApplyStats.Failed + s.PruneStats.Failed + s.DeleteStats.Failed
    25  }
    26  
    27  // FailedReconciliationSum returns the number of resources that failed reconciliation.
    28  func (s *Stats) FailedReconciliationSum() int {
    29  	return s.WaitStats.Failed + s.WaitStats.Timeout
    30  }
    31  
    32  // Handle updates the stats based on an event.
    33  func (s *Stats) Handle(e event.Event) {
    34  	switch e.Type {
    35  	case event.ApplyType:
    36  		s.ApplyStats.Inc(e.ApplyEvent.Status)
    37  	case event.PruneType:
    38  		s.PruneStats.Inc(e.PruneEvent.Status)
    39  	case event.DeleteType:
    40  		s.DeleteStats.Inc(e.DeleteEvent.Status)
    41  	case event.WaitType:
    42  		s.WaitStats.Inc(e.WaitEvent.Status)
    43  	}
    44  }
    45  
    46  type ApplyStats struct {
    47  	Successful int
    48  	Skipped    int
    49  	Failed     int
    50  }
    51  
    52  func (a *ApplyStats) Inc(op event.ApplyEventStatus) {
    53  	switch op {
    54  	case event.ApplySuccessful:
    55  		a.Successful++
    56  	case event.ApplySkipped:
    57  		a.Skipped++
    58  	case event.ApplyFailed:
    59  		a.Failed++
    60  	default:
    61  		panic(fmt.Errorf("invalid apply status %s", op.String()))
    62  	}
    63  }
    64  
    65  func (a *ApplyStats) IncFailed() {
    66  	a.Failed++
    67  }
    68  
    69  func (a *ApplyStats) Sum() int {
    70  	return a.Successful + a.Skipped + a.Failed
    71  }
    72  
    73  type PruneStats struct {
    74  	Successful int
    75  	Skipped    int
    76  	Failed     int
    77  }
    78  
    79  func (p *PruneStats) Inc(op event.PruneEventStatus) {
    80  	switch op {
    81  	case event.PruneSuccessful:
    82  		p.Successful++
    83  	case event.PruneSkipped:
    84  		p.Skipped++
    85  	case event.PruneFailed:
    86  		p.Failed++
    87  	default:
    88  		panic(fmt.Errorf("invalid prune status %s", op.String()))
    89  	}
    90  }
    91  
    92  func (p *PruneStats) IncFailed() {
    93  	p.Failed++
    94  }
    95  
    96  func (p *PruneStats) Sum() int {
    97  	return p.Successful + p.Skipped + p.Failed
    98  }
    99  
   100  type DeleteStats struct {
   101  	Successful int
   102  	Skipped    int
   103  	Failed     int
   104  }
   105  
   106  func (d *DeleteStats) Inc(op event.DeleteEventStatus) {
   107  	switch op {
   108  	case event.DeleteSuccessful:
   109  		d.Successful++
   110  	case event.DeleteSkipped:
   111  		d.Skipped++
   112  	case event.DeleteFailed:
   113  		d.Failed++
   114  	default:
   115  		panic(fmt.Errorf("invalid delete status %s", op.String()))
   116  	}
   117  }
   118  
   119  func (d *DeleteStats) IncFailed() {
   120  	d.Failed++
   121  }
   122  
   123  func (d *DeleteStats) Sum() int {
   124  	return d.Successful + d.Skipped + d.Failed
   125  }
   126  
   127  type WaitStats struct {
   128  	Successful int
   129  	Timeout    int
   130  	Failed     int
   131  	Skipped    int
   132  }
   133  
   134  func (w *WaitStats) Inc(status event.WaitEventStatus) {
   135  	switch status {
   136  	case event.ReconcilePending:
   137  		// ignore - should be replaced by one of the others before the WaitTask exits
   138  	case event.ReconcileSuccessful:
   139  		w.Successful++
   140  	case event.ReconcileSkipped:
   141  		w.Skipped++
   142  	case event.ReconcileTimeout:
   143  		w.Timeout++
   144  	case event.ReconcileFailed:
   145  		w.Failed++
   146  	default:
   147  		panic(fmt.Errorf("invalid wait status %s", status.String()))
   148  	}
   149  }
   150  
   151  func (w *WaitStats) Sum() int {
   152  	return w.Successful + w.Skipped + w.Failed + w.Timeout
   153  }
   154  

View as plain text