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

     1  package parameter
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/GuanceCloud/cliutils/pprofparser/domain/languages"
     7  	"github.com/GuanceCloud/cliutils/pprofparser/tools/jsontoolkit"
     8  )
     9  
    10  const (
    11  	FromTrace   = "trace"
    12  	FromProfile = "profile"
    13  )
    14  
    15  const (
    16  	FilterBySpanTime = "spanTime"
    17  	FilterByFull     = "full"
    18  )
    19  
    20  const (
    21  	MinTimestampMicro = 1640966400000000
    22  	MaxTimestampMicro = 2147483647000000
    23  	MinTimestampNano  = 1640966400000000000
    24  )
    25  
    26  type BaseRequestParam struct {
    27  	WorkspaceUUID string `json:"workspace_uuid" binding:"required"`
    28  }
    29  
    30  type WithTypeRequestParam struct {
    31  	BaseRequestParam
    32  	Type string `json:"type" binding:"required"`
    33  }
    34  
    35  type Profile struct {
    36  	Language          languages.Lang `json:"language" binding:"required"`
    37  	EsDocID           string         `json:"__docid"`
    38  	ProfileID         string         `json:"profile_id" binding:"required"`
    39  	ProfileStart      interface{}    `json:"profile_start" binding:"required"` // , min=1640966400000000000
    40  	ProfileEnd        interface{}    `json:"profile_end" binding:"required"`   // , min=1640966400000000000
    41  	internalProfStart int64
    42  	internalProfEnd   int64
    43  }
    44  
    45  func (p *Profile) StartTime() (int64, error) {
    46  	if p.internalProfStart > 0 {
    47  		return p.internalProfStart, nil
    48  	}
    49  	start, err := jsontoolkit.IFaceCast2Int64(p.ProfileStart)
    50  	if err != nil {
    51  		return 0, err
    52  	}
    53  	if start <= 0 {
    54  		return 0, errors.New("illegal profile_start parameter")
    55  	}
    56  	p.internalProfStart = start
    57  	return start, err
    58  }
    59  
    60  func (p *Profile) EndTime() (int64, error) {
    61  	if p.internalProfEnd > 0 {
    62  		return p.internalProfEnd, nil
    63  	}
    64  	end, err := jsontoolkit.IFaceCast2Int64(p.ProfileEnd)
    65  	if err != nil {
    66  		return 0, err
    67  	}
    68  	if end <= 0 {
    69  		return 0, errors.New("illegal profile_end parameter")
    70  	}
    71  	p.internalProfEnd = end
    72  	return end, err
    73  }
    74  
    75  type Span struct {
    76  	TraceID   string `json:"trace_id"`
    77  	SpanID    string `json:"span_id"`
    78  	SpanStart int64  `json:"span_start"`
    79  	SpanEnd   int64  `json:"span_end"`
    80  }
    81  
    82  type SummaryParam struct {
    83  	BaseRequestParam
    84  	Span
    85  	From     string     `json:"from"`
    86  	FilterBy string     `json:"filter_by"`
    87  	Profiles []*Profile `json:"profiles" binding:"required"`
    88  }
    89  
    90  type ParseParam struct {
    91  	WithTypeRequestParam
    92  	Profile
    93  }
    94  
    95  type LookupParam struct {
    96  	WithTypeRequestParam
    97  	Span
    98  	FilterBy string     `json:"filter_by"`
    99  	Profiles []*Profile `json:"profiles" binding:"required"`
   100  }
   101  
   102  type DownloadParam struct {
   103  	BaseRequestParam
   104  	Profiles []*Profile `json:"profiles" binding:"required"`
   105  }
   106  
   107  // VerifyLanguage 校验多个profiles中的language是否相同
   108  func VerifyLanguage(profiles []*Profile) (languages.Lang, error) {
   109  
   110  	if len(profiles) == 0 {
   111  		return languages.Unknown, fmt.Errorf("empty profiles param")
   112  	}
   113  
   114  	//lang := profiles[0].Language
   115  
   116  	// 对比是否是同一个language
   117  	//for i := 1; i < len(profiles); i++ {
   118  	//	if !lang.Is(profiles[i].Language) {
   119  	//		return languages.Unknown, fmt.Errorf("the languages are not same")
   120  	//	}
   121  	//}
   122  
   123  	return profiles[0].Language, nil
   124  }