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
profileSkipPattern []string
profileExtra string
profileNeedPattern []string
)
func init() {
@ -45,6 +46,7 @@ func init() {
add2Flags := func(f *pflag.FlagSet) {
f.StringVarP(&profileOutput, "output", "o", "", "download cover 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())
@ -63,7 +65,7 @@ var getProfileCmd = &cobra.Command{
}
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{

View File

@ -25,12 +25,13 @@ import (
"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)
profiles, err := gocClient.Profile().Get(ids,
profile.WithPackagePattern(skips),
profile.WithExtraPattern(extra))
profile.WithExtraPattern(extra),
profile.WithNeed(need))
if err != nil {
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
skipPatterns []string
extraPattern string
needPattern []string
}
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) {
for _, opt := range opts {
opt(p)
@ -70,10 +77,12 @@ func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error)
idQuery := strings.Join(ids, ",")
skipQuery := strings.Join(p.skipPatterns, ",")
needPattern := strings.Join(p.needPattern, ",")
req.QueryParam.Add("id", idQuery)
req.QueryParam.Add("skippattern", skipQuery)
req.QueryParam.Add("extra", p.extraPattern)
req.QueryParam.Add("needpattern", needPattern)
res := struct {
Data string `json:"profile,omitempty"`

View File

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