feat: add needed file filter

This commit is contained in:
liruichen 2023-07-28 11:48:05 +08:00
parent ee63433ca4
commit ca1ec49eeb
4 changed files with 43 additions and 9 deletions

View File

@ -32,6 +32,7 @@ var (
profileIds []string profileIds []string
profileSkipPattern []string profileSkipPattern []string
profileExtra string profileExtra string
profileNeedPattern []string
) )
func init() { func init() {
@ -45,6 +46,7 @@ func init() {
add2Flags := func(f *pflag.FlagSet) { add2Flags := func(f *pflag.FlagSet) {
f.StringVarP(&profileOutput, "output", "o", "", "download cover profile") f.StringVarP(&profileOutput, "output", "o", "", "download cover profile")
f.StringSliceVar(&profileSkipPattern, "skip", nil, "skip specific packages in the profile") f.StringSliceVar(&profileSkipPattern, "skip", nil, "skip specific packages in the profile")
f.StringSliceVarP(&profileNeedPattern, "need", "n", nil, "find specific packages in the profile")
} }
add1Flags(getProfileCmd.Flags()) add1Flags(getProfileCmd.Flags())
@ -63,7 +65,7 @@ var getProfileCmd = &cobra.Command{
} }
func getProfile(cmd *cobra.Command, args []string) { func getProfile(cmd *cobra.Command, args []string) {
client.GetProfile(profileHost, profileIds, profileSkipPattern, profileExtra, profileOutput) client.GetProfile(profileHost, profileIds, profileSkipPattern, profileExtra, profileOutput, profileNeedPattern)
} }
var clearProfileCmd = &cobra.Command{ var clearProfileCmd = &cobra.Command{

View File

@ -25,12 +25,13 @@ import (
"github.com/qiniu/goc/v2/pkg/log" "github.com/qiniu/goc/v2/pkg/log"
) )
func GetProfile(host string, ids []string, skips []string, extra string, output string) { func GetProfile(host string, ids []string, skips []string, extra string, output string, need []string) {
gocClient := rest.NewV2Client(host) gocClient := rest.NewV2Client(host)
profiles, err := gocClient.Profile().Get(ids, profiles, err := gocClient.Profile().Get(ids,
profile.WithPackagePattern(skips), profile.WithPackagePattern(skips),
profile.WithExtraPattern(extra)) profile.WithExtraPattern(extra),
profile.WithNeed(need))
if err != nil { if err != nil {
log.Fatalf("fail to get profile from the goc server: %v, response: %v", err, profiles) log.Fatalf("fail to get profile from the goc server: %v, response: %v", err, profiles)
} }

View File

@ -39,6 +39,7 @@ type profileClient struct {
c *resty.Client c *resty.Client
skipPatterns []string skipPatterns []string
extraPattern string extraPattern string
needPattern []string
} }
func NewProfileClient(c *resty.Client) *profileClient { func NewProfileClient(c *resty.Client) *profileClient {
@ -61,6 +62,12 @@ func WithExtraPattern(pattern string) profileOption {
} }
} }
func WithNeed(need []string) profileOption {
return func(pc *profileClient) {
pc.needPattern = need
}
}
func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error) { func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error) {
for _, opt := range opts { for _, opt := range opts {
opt(p) opt(p)
@ -70,10 +77,12 @@ func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error)
idQuery := strings.Join(ids, ",") idQuery := strings.Join(ids, ",")
skipQuery := strings.Join(p.skipPatterns, ",") skipQuery := strings.Join(p.skipPatterns, ",")
needPattern := strings.Join(p.needPattern, ",")
req.QueryParam.Add("id", idQuery) req.QueryParam.Add("id", idQuery)
req.QueryParam.Add("skippattern", skipQuery) req.QueryParam.Add("skippattern", skipQuery)
req.QueryParam.Add("extra", p.extraPattern) req.QueryParam.Add("extra", p.extraPattern)
req.QueryParam.Add("needpattern", needPattern)
res := struct { res := struct {
Data string `json:"profile,omitempty"` Data string `json:"profile,omitempty"`

View File

@ -106,6 +106,11 @@ func (gs *gocServer) getProfiles(c *gin.Context) {
if skippatternRaw != "" { if skippatternRaw != "" {
skippattern = strings.Split(skippatternRaw, ",") skippattern = strings.Split(skippatternRaw, ",")
} }
neerpatternRaw := c.Query("needpattern")
var neerpattern []string
if neerpatternRaw != "" {
neerpattern = strings.Split(neerpatternRaw, ",")
}
extra := c.Query("extra") extra := c.Query("extra")
isExtra := filterExtra(extra) isExtra := filterExtra(extra)
@ -180,7 +185,7 @@ func (gs *gocServer) getProfiles(c *gin.Context) {
} }
// check if skippattern matches // check if skippattern matches
newProfile := filterProfileByPattern(skippattern, profile) newProfile := filterProfileByPattern(skippattern, neerpattern, profile)
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@ -315,13 +320,14 @@ func (gs *gocServer) watchProfileUpdate(c *gin.Context) {
<-gwc.exitCh <-gwc.exitCh
} }
func filterProfileByPattern(skippattern []string, profiles []*cover.Profile) []*cover.Profile { func filterProfileByPattern(skippattern []string, needpattern []string, profiles []*cover.Profile) []*cover.Profile {
if len(skippattern) == 0 {
return profiles
}
var out = make([]*cover.Profile, 0) var out = make([]*cover.Profile, 0)
if len(skippattern) == 0 {
goto need
}
for _, profile := range profiles { for _, profile := range profiles {
skip := false skip := false
for _, pattern := range skippattern { for _, pattern := range skippattern {
@ -335,6 +341,22 @@ func filterProfileByPattern(skippattern []string, profiles []*cover.Profile) []*
out = append(out, profile) out = append(out, profile)
} }
} }
need:
if len(needpattern) == 0 {
return profiles
}
for _, profile := range profiles {
need := false
for _, pattern := range needpattern {
if strings.Contains(profile.FileName, pattern) {
need = true
break
}
}
if need {
out = append(out, profile)
}
}
return out return out
} }