github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/domain/pprof/summary.go (about)

     1  package pprof
     2  
     3  import (
     4  	"sort"
     5  
     6  	"github.com/GuanceCloud/cliutils/pprofparser/domain/events"
     7  	"github.com/GuanceCloud/cliutils/pprofparser/domain/languages"
     8  	"github.com/GuanceCloud/cliutils/pprofparser/domain/quantity"
     9  )
    10  
    11  type SummaryValueType struct {
    12  	Type events.Type    `json:"type"`
    13  	Unit *quantity.Unit `json:"unit"`
    14  }
    15  
    16  type EventSummary struct {
    17  	*SummaryValueType
    18  	Value int64 `json:"value"`
    19  }
    20  
    21  func (es *EventSummary) ConvertToDefaultUnit() {
    22  	if es.Unit == nil {
    23  		return
    24  	}
    25  	es.Value = es.Unit.ConvertToDefaultUnit(es.Value)
    26  	es.Unit = es.Unit.Kind.DefaultUnit
    27  }
    28  
    29  type SummaryCollection []*EventSummary
    30  
    31  func (s SummaryCollection) SortByType(lang languages.Lang) {
    32  	cs := &CollectionSort{
    33  		collection: s,
    34  		lessFunc: func(a, b *EventSummary) bool {
    35  			return a.Type.GetSort(lang) < b.Type.GetSort(lang)
    36  		},
    37  	}
    38  	sort.Sort(cs)
    39  }
    40  
    41  func (s SummaryCollection) SortByValue() {
    42  	cs := &CollectionSort{
    43  		collection: s,
    44  		lessFunc: func(a, b *EventSummary) bool {
    45  			return a.Value > b.Value
    46  		},
    47  	}
    48  	sort.Sort(cs)
    49  }
    50  
    51  type LessFunc func(a, b *EventSummary) bool
    52  
    53  type CollectionSort struct {
    54  	collection SummaryCollection
    55  	lessFunc   LessFunc
    56  }
    57  
    58  func (c *CollectionSort) Len() int {
    59  	return len(c.collection)
    60  }
    61  
    62  func (c *CollectionSort) Less(i, j int) bool {
    63  	return c.lessFunc(c.collection[i], c.collection[j])
    64  }
    65  
    66  func (c *CollectionSort) Swap(i, j int) {
    67  	c.collection[i], c.collection[j] = c.collection[j], c.collection[i]
    68  }