From 1269dcd7abdda6f880375a7391f8456bd71a4451 Mon Sep 17 00:00:00 2001 From: Guillaume Falourd Date: Fri, 21 May 2021 15:15:14 -0300 Subject: [PATCH] Update metrics for input flags (#930) * update metrics.go and metrics_test.go for input flags Signed-off-by: GuillaumeFalourd * update metrics.go and metrics_test.go for input flags Signed-off-by: GuillaumeFalourd * comment unused regexFlag Signed-off-by: GuillaumeFalourd --- pkg/metric/data_collector.go | 33 ++++++++++-- pkg/metric/data_collector_test.go | 88 +++++++++++++++++++++++++++++++ pkg/metric/metric.go | 1 + 3 files changed, 119 insertions(+), 3 deletions(-) diff --git a/pkg/metric/data_collector.go b/pkg/metric/data_collector.go index 08917efef..7e3a22be8 100644 --- a/pkg/metric/data_collector.go +++ b/pkg/metric/data_collector.go @@ -21,6 +21,7 @@ import ( "math" "os" "path/filepath" + "regexp" "runtime" "strings" "time" @@ -33,6 +34,8 @@ var ( _ Collector = DataCollectorManager{} CommonsRepoAdded = "" RepoName = "" + regexCoreFlag = regexp.MustCompile(`--docker|--local|--stdin|--version|--verbose|--default|--help`) + //regexFlag = regexp.MustCompile("--(.*)=") ) type DataCollectorManager struct { @@ -70,10 +73,11 @@ func (d DataCollectorManager) Collect( CommonsRepoAdded: CommonsRepoAdded, CommandExecutionTime: commandExecutionTime, FormulaRepo: d.repoData(), + Flags: flags(), } metric := APIData{ - Id: Id(metricID()), + Id: Id(metricId()), UserId: userId, Os: runtime.GOOS, RitVersion: ritVersion, @@ -98,8 +102,31 @@ func (d DataCollectorManager) repoData() formula.Repo { return formula.Repo{} } -func metricID() string { +func metricId() string { args := os.Args args[0] = "rit" - return strings.Join(args, "_") + var metricID []string + for _, element := range args { + if !strings.Contains(element, "--") { + metricID = append(metricID, element) + } + } + return strings.Join(metricID, "_") +} + +func flags() []string { + args := os.Args + var flags []string + for _, element := range args { + switch { + case regexCoreFlag.MatchString(element): + element = strings.Replace(element, "--", "", -1) + flags = append(flags, element) + // Remove comment to keep input flags keys on datas + // case strings.Contains(element, "--"): + // flag := regexFlag.FindStringSubmatch(element)[1] + // flags = append(flags, flag) + } + } + return flags } diff --git a/pkg/metric/data_collector_test.go b/pkg/metric/data_collector_test.go index 2713ba0e6..312fa0a5f 100644 --- a/pkg/metric/data_collector_test.go +++ b/pkg/metric/data_collector_test.go @@ -18,10 +18,12 @@ package metric import ( "errors" + "os" "testing" "github.com/ZupIT/ritchie-cli/pkg/stream" sMocks "github.com/ZupIT/ritchie-cli/pkg/stream/mocks" + "github.com/stretchr/testify/assert" ) func Test_Collector(t *testing.T) { @@ -102,3 +104,89 @@ type UserIdGeneratorMock struct { func (us UserIdGeneratorMock) Generate() (UserId, error) { return us.GenerateMock() } + +func TestMetricId(t *testing.T) { + + tests := []struct { + name string + in []string + out string + }{ + { + name: "success filter input flag with docker flag", + in: []string{"cmd", "test", "login", "--username=dennis", "--password=123456", "--docker"}, + out: "rit_test_login", + }, + { + name: "success filter input flag for credential", + in: []string{"cmd", "set", "credential", "--provider=github", "--fields=username,token", "--values=\"$USERNAME_CREDENTIAL\",\"$GITHUB_TOKEN\""}, + out: "rit_set_credential", + }, + { + name: "success filter input flag for core command", + in: []string{"cmd", "add", "repo", "--provider=\"Github\"", "--name=\"formulas-insights\"", "--repoUrl=\"https://github.com/ZupIT/ritchie-formulas\"", "--priority=1"}, + out: "rit_add_repo", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + oldArgs := os.Args + os.Args = tt.in + defer func() { os.Args = oldArgs }() + + got := metricId() + if got != tt.out { + t.Errorf("Unexpected return: got %v want %v", got, tt.out) + } + + assert.Equal(t, got, tt.out) + }) + } +} + +func TestFlags(t *testing.T) { + + tests := []struct { + name string + in []string + out []string + }{ + { + name: "success filter input flag", + in: []string{"cmd", "create", "formula"}, + out: nil, + //out: []string{"username", "password", "docker"}, + }, + { + name: "success filter input flag with docker flag", + in: []string{"cmd", "test", "login", "--username=dennis", "--password=123456", "--docker"}, + out: []string{"docker"}, + //out: []string{"username", "password", "docker"}, + }, + { + name: "success filter input flag for credential", + in: []string{"cmd", "set", "credential", "--provider=github", "--fields=username,token", "--values=\"$USERNAME_CREDENTIAL\",\"$GITHUB_TOKEN\""}, + out: nil, + //out: []string{"provider", "fields", "values"}, + }, + { + name: "success filter input flag for core command", + in: []string{"cmd", "add", "repo", "--provider=\"Github\"", "--name=\"formulas-insights\"", "--repoUrl=\"https://github.com/ZupIT/ritchie-formulas\"", "--priority=1"}, + out: nil, + //out: []string{"provider", "name", "repoUrl", "priority"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + oldArgs := os.Args + os.Args = tt.in + defer func() { os.Args = oldArgs }() + + got := flags() + + assert.Equal(t, got, tt.out) + }) + } +} diff --git a/pkg/metric/metric.go b/pkg/metric/metric.go index 0ec6460db..698396c98 100644 --- a/pkg/metric/metric.go +++ b/pkg/metric/metric.go @@ -57,6 +57,7 @@ type Data struct { CommandExecutionTime float64 `json:"commandExecutionTime"` MetricsAcceptance string `json:"metricsAcceptance,omitempty"` FormulaRepo formula.Repo `json:"repo,omitempty"` + Flags []string `json:"flags,omitempty"` } type Sender interface {