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 (
profileHost string
profileOutput string // --output flag
profileIds []string
profilePackages string
profileExtra string
profileHost string
profileOutput string // --output flag
profileIds []string
profileSkipPattern []string
profileExtra string
)
func init() {
@ -44,7 +44,7 @@ func init() {
add2Flags := func(f *pflag.FlagSet) {
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())
@ -63,7 +63,7 @@ var getProfileCmd = &cobra.Command{
}
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{

View File

@ -25,11 +25,11 @@ import (
"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)
profiles, err := gocClient.Profile().Get(ids,
profile.WithPackagePattern(packages),
profile.WithPackagePattern(skips),
profile.WithExtraPattern(extra))
if err != nil {
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 {
c *resty.Client
packagePattern string
extraPattern string
c *resty.Client
skipPatterns []string
extraPattern string
}
func NewProfileClient(c *resty.Client) *profileClient {
@ -49,9 +49,9 @@ func NewProfileClient(c *resty.Client) *profileClient {
type profileOption func(*profileClient)
func WithPackagePattern(pattern string) profileOption {
func WithPackagePattern(skips []string) profileOption {
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()
idQuery := strings.Join(ids, ",")
skipQuery := strings.Join(p.skipPatterns, ",")
req.QueryParam.Add("id", idQuery)
req.QueryParam.Add("pattern", p.packagePattern)
req.QueryParam.Add("skippattern", skipQuery)
req.QueryParam.Add("extra", p.extraPattern)
res := struct {
@ -105,9 +106,10 @@ func (p *profileClient) Delete(ids []string, opts ...profileOption) error {
req := p.c.R()
idQuery := strings.Join(ids, ",")
skipQuery := strings.Join(p.skipPatterns, ",")
req.QueryParam.Add("id", idQuery)
req.QueryParam.Add("pattern", p.packagePattern)
req.QueryParam.Add("skippattern", skipQuery)
req.QueryParam.Add("extra", p.extraPattern)
_, err := req.

View File

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