Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Update metrics for input flags (#930)
Browse files Browse the repository at this point in the history
* update metrics.go and metrics_test.go for input flags

Signed-off-by: GuillaumeFalourd <guillaume.falourd@zup.com.br>

* update metrics.go and metrics_test.go for input flags

Signed-off-by: GuillaumeFalourd <guillaume.falourd@zup.com.br>

* comment unused regexFlag

Signed-off-by: GuillaumeFalourd <guillaume.falourd@zup.com.br>
  • Loading branch information
GuillaumeFalourd authored and github-actions committed May 21, 2021
1 parent 1ead82f commit 1269dcd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pkg/metric/data_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
88 changes: 88 additions & 0 deletions pkg/metric/data_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
}
1 change: 1 addition & 0 deletions pkg/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1269dcd

Please sign in to comment.