add skip pattern (#268)

* add skip pattern

* fix

* fix
This commit is contained in:
Li Yiyang 2022-03-15 21:45:30 +08:00 committed by GitHub
parent 0a5062f61b
commit ee63433ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 34 deletions

View File

@ -27,11 +27,11 @@ var profileCmd = &cobra.Command{
} }
var ( var (
profileHost string profileHost string
profileOutput string // --output flag profileOutput string // --output flag
profileIds []string profileIds []string
profilePackages string profileSkipPattern []string
profileExtra string profileExtra string
) )
func init() { func init() {
@ -44,7 +44,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.StringVar(&profilePackages, "packages", "", "specify the regex expression of packages, only profile of these packages will be downloaded") f.StringSliceVar(&profileSkipPattern, "skip", nil, "skip specific packages in the profile")
} }
add1Flags(getProfileCmd.Flags()) add1Flags(getProfileCmd.Flags())
@ -63,7 +63,7 @@ var getProfileCmd = &cobra.Command{
} }
func getProfile(cmd *cobra.Command, args []string) { func getProfile(cmd *cobra.Command, args []string) {
client.GetProfile(profileHost, profileIds, profilePackages, profileExtra, profileOutput) client.GetProfile(profileHost, profileIds, profileSkipPattern, profileExtra, profileOutput)
} }
var clearProfileCmd = &cobra.Command{ var clearProfileCmd = &cobra.Command{

View File

@ -25,11 +25,11 @@ import (
"github.com/qiniu/goc/v2/pkg/log" "github.com/qiniu/goc/v2/pkg/log"
) )
func GetProfile(host string, ids []string, packages string, extra string, output string) { func GetProfile(host string, ids []string, skips []string, extra string, output string) {
gocClient := rest.NewV2Client(host) gocClient := rest.NewV2Client(host)
profiles, err := gocClient.Profile().Get(ids, profiles, err := gocClient.Profile().Get(ids,
profile.WithPackagePattern(packages), profile.WithPackagePattern(skips),
profile.WithExtraPattern(extra)) profile.WithExtraPattern(extra))
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

@ -36,9 +36,9 @@ type ProfileInterface interface {
} }
type profileClient struct { type profileClient struct {
c *resty.Client c *resty.Client
packagePattern string skipPatterns []string
extraPattern string extraPattern string
} }
func NewProfileClient(c *resty.Client) *profileClient { func NewProfileClient(c *resty.Client) *profileClient {
@ -49,9 +49,9 @@ func NewProfileClient(c *resty.Client) *profileClient {
type profileOption func(*profileClient) type profileOption func(*profileClient)
func WithPackagePattern(pattern string) profileOption { func WithPackagePattern(skips []string) profileOption {
return func(pc *profileClient) { return func(pc *profileClient) {
pc.packagePattern = pattern pc.skipPatterns = skips
} }
} }
@ -69,9 +69,10 @@ func (p *profileClient) Get(ids []string, opts ...profileOption) (string, error)
req := p.c.R() req := p.c.R()
idQuery := strings.Join(ids, ",") idQuery := strings.Join(ids, ",")
skipQuery := strings.Join(p.skipPatterns, ",")
req.QueryParam.Add("id", idQuery) req.QueryParam.Add("id", idQuery)
req.QueryParam.Add("pattern", p.packagePattern) req.QueryParam.Add("skippattern", skipQuery)
req.QueryParam.Add("extra", p.extraPattern) req.QueryParam.Add("extra", p.extraPattern)
res := struct { res := struct {
@ -105,9 +106,10 @@ func (p *profileClient) Delete(ids []string, opts ...profileOption) error {
req := p.c.R() req := p.c.R()
idQuery := strings.Join(ids, ",") idQuery := strings.Join(ids, ",")
skipQuery := strings.Join(p.skipPatterns, ",")
req.QueryParam.Add("id", idQuery) req.QueryParam.Add("id", idQuery)
req.QueryParam.Add("pattern", p.packagePattern) req.QueryParam.Add("skippattern", skipQuery)
req.QueryParam.Add("extra", p.extraPattern) req.QueryParam.Add("extra", p.extraPattern)
_, err := req. _, err := req.

View File

@ -101,7 +101,12 @@ func (gs *gocServer) getProfiles(c *gin.Context) {
idQuery := c.Query("id") idQuery := c.Query("id")
ifInIdMap := idMaps(idQuery) ifInIdMap := idMaps(idQuery)
pattern := c.Query("pattern") skippatternRaw := c.Query("skippattern")
var skippattern []string
if skippatternRaw != "" {
skippattern = strings.Split(skippatternRaw, ",")
}
extra := c.Query("extra") extra := c.Query("extra")
isExtra := filterExtra(extra) isExtra := filterExtra(extra)
@ -174,12 +179,8 @@ func (gs *gocServer) getProfiles(c *gin.Context) {
return return
} }
// check if pattern matches // check if skippattern matches
newProfile, err := filterProfileByPattern(pattern, profile) newProfile := filterProfileByPattern(skippattern, profile)
if err != nil {
log.Errorf("%v", err)
return
}
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
@ -314,26 +315,28 @@ func (gs *gocServer) watchProfileUpdate(c *gin.Context) {
<-gwc.exitCh <-gwc.exitCh
} }
func filterProfileByPattern(pattern string, profiles []*cover.Profile) ([]*cover.Profile, error) { func filterProfileByPattern(skippattern []string, profiles []*cover.Profile) []*cover.Profile {
if strings.TrimSpace(pattern) == "" { if len(skippattern) == 0 {
return profiles, nil return profiles
} }
var out = make([]*cover.Profile, 0) var out = make([]*cover.Profile, 0)
for _, profile := range profiles { for _, profile := range profiles {
matched, err := regexp.MatchString(pattern, profile.FileName) skip := false
if err != nil { for _, pattern := range skippattern {
return nil, fmt.Errorf("filterProfile failed with pattern %s for profile %s, err: %v", pattern, profile.FileName, err) if strings.Contains(profile.FileName, pattern) {
} skip = true
if matched { break
out = append(out, profile) }
break // no need to check again for the file
} }
if !skip {
out = append(out, profile)
}
} }
return out, nil return out
} }
func idMaps(idQuery string) func(key string) bool { func idMaps(idQuery string) func(key string) bool {