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

Metrics check #405

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"os"
"time"

"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/git/gitlab"

"k8s.io/kubectl/pkg/util/templates"

"github.com/ZupIT/ritchie-cli/pkg/credential"
Expand All @@ -34,6 +31,9 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/formula/repo"
"github.com/ZupIT/ritchie-cli/pkg/formula/runner"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/git/github"
"github.com/ZupIT/ritchie-cli/pkg/git/gitlab"
"github.com/ZupIT/ritchie-cli/pkg/metrics"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"

"github.com/ZupIT/ritchie-cli/pkg/upgrade"
Expand Down Expand Up @@ -116,7 +116,7 @@ func buildCommands() *cobra.Command {
tutorialFinder := rtutorial.NewFinder(ritchieHomeDir, fileManager)
tutorialSetter := rtutorial.NewSetter(ritchieHomeDir, fileManager)
tutorialFindSetter := rtutorial.NewFindSetter(ritchieHomeDir, tutorialFinder, tutorialSetter)

metricsChecker := metrics.NewChecker(fileManager)
formBuildMake := builder.NewBuildMake()
formBuildBat := builder.NewBuildBat(fileManager)
formBuildDocker := builder.NewBuildDocker()
Expand Down Expand Up @@ -154,7 +154,7 @@ func buildCommands() *cobra.Command {
updateCmd := cmd.NewUpdateCmd()
buildCmd := cmd.NewBuildCmd()
upgradeCmd := cmd.NewUpgradeCmd(defaultUpgradeResolver, upgradeManager, defaultUrlFinder)
metricsCmd := cmd.NewMetricsCmd(fileManager, inputList)
metricsCmd := cmd.NewMetricsCmd(fileManager, inputList, metricsChecker)
tutorialCmd := cmd.NewTutorialCmd(ritchieHomeDir, inputList, tutorialFindSetter)

// level 2
Expand Down
25 changes: 14 additions & 11 deletions pkg/cmd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/metric"
"github.com/ZupIT/ritchie-cli/pkg/metrics"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

type metricsCmd struct {
stream.FileWriteReadExister
prompt.InputList
metrics.Checker
}

func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList) *cobra.Command {
func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList, checker metrics.Checker) *cobra.Command {
m := &metricsCmd{
FileWriteReadExister: file,
InputList: inList,
Checker: checker,
}

cmd := &cobra.Command{
Expand All @@ -32,33 +34,34 @@ func NewMetricsCmd(file stream.FileWriteReadExister, inList prompt.InputList) *c

func (m metricsCmd) run() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
if !m.FileWriteReadExister.Exists(metric.MetricsPath()) {
path := metrics.FilePath
if !m.FileWriteReadExister.Exists(path) {
victor-schumacher marked this conversation as resolved.
Show resolved Hide resolved
options := []string{"yes", "no"}
choose, err := m.InputList.List("You want to send anonymous data about the product, feature use, statistics and crash reports?", options)
if err != nil {
return err
}

err = m.FileWriteReadExister.Write(metric.MetricsPath(), []byte(choose))
err = m.FileWriteReadExister.Write(path, []byte(choose))
if err != nil {
return err
}
return nil
}

metricsStatus, err := m.FileWriteReadExister.Read(metric.MetricsPath())
metricsStatus, err := m.Check()
if err != nil {
return err
}

changeTo := "no"
message := "You are no longer sending anonymous metrics."
if string(metricsStatus) == changeTo {
changeTo = "yes"
message = "You are now sending anonymous metrics. Thank you!"
changeTo := "yes"
message := "You are now sending anonymous metrics. Thank you!"
if metricsStatus {
changeTo = "no"
message = "You are no longer sending anonymous metrics."
}

err = m.FileWriteReadExister.Write(metric.MetricsPath(), []byte(changeTo))
err = m.FileWriteReadExister.Write(path, []byte(changeTo))
if err != nil {
return err
}
Expand Down
36 changes: 22 additions & 14 deletions pkg/cmd/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

func Test_metricsCmd_runPrompt(t *testing.T) {
type in struct {
file stream.FileWriteReadExister
InputList prompt.InputList
file stream.FileWriteReadExister
InputList prompt.InputList
checkerMock CheckerMock
}

var tests = []struct {
Expand Down Expand Up @@ -89,30 +90,30 @@ func Test_metricsCmd_runPrompt(t *testing.T) {
ExistsMock: func(path string) bool {
return true
},
ReadMock: func(path string) ([]byte, error) {
return []byte("no"), nil
},
WriteMock: func(path string, content []byte) error {
return nil
},
},
checkerMock: CheckerMock{func() (bool, error) {
return true, nil
}},
},
wantErr: false,
},
{
name: "fail on read when metrics file exist",
name: "fail on check when metrics file exist",
in: in{
file: sMocks.FileWriteReadExisterCustomMock{
ExistsMock: func(path string) bool {
return true
},
ReadMock: func(path string) ([]byte, error) {
return []byte("no"), errors.New("error reading file")
},
WriteMock: func(path string, content []byte) error {
return nil
},
},
checkerMock: CheckerMock{func() (bool, error) {
return false, errors.New("error reading file")
}},
},
wantErr: true,
},
Expand All @@ -123,25 +124,32 @@ func Test_metricsCmd_runPrompt(t *testing.T) {
ExistsMock: func(path string) bool {
return true
},
ReadMock: func(path string) ([]byte, error) {
return []byte("no"), nil
},
WriteMock: func(path string, content []byte) error {
return errors.New("error writing file")
},
},
checkerMock: CheckerMock{func() (bool, error) {
return false, nil
}},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metricsCmd := NewMetricsCmd(tt.in.file, tt.in.InputList)
metricsCmd := NewMetricsCmd(tt.in.file, tt.in.InputList, tt.in.checkerMock)
if err := metricsCmd.Execute(); (err != nil) != tt.wantErr {
t.Errorf("metrics command error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("metrics command error = %v | error wanted: %v", err, tt.wantErr)
}
})
}
}

type CheckerMock struct {
CheckMock func() (bool, error)
}

func (cm CheckerMock) Check() (bool, error) {
return cm.CheckMock()
}
48 changes: 48 additions & 0 deletions pkg/metrics/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package metrics

import (
"github.com/ZupIT/ritchie-cli/pkg/stream"
)

type CheckManager struct {
file stream.FileReadExister
}

func NewChecker(file stream.FileReadExister) CheckManager {
return CheckManager{file: file}
}

func (c CheckManager) Check() (bool, error) {
if !c.file.Exists(FilePath) {
return false, nil
}

bytes, err := c.file.Read(FilePath)
if err != nil {
return false, err
}

result := true
if string(bytes) == "no" {
result = false
}

return result, nil
}

110 changes: 110 additions & 0 deletions pkg/metrics/checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package metrics

import (
"errors"
"testing"

"github.com/ZupIT/ritchie-cli/pkg/stream"
sMocks "github.com/ZupIT/ritchie-cli/pkg/stream/mocks"
)

func Test_Check(t *testing.T) {
type in struct {
file stream.FileReadExister
}

var tests = []struct {
name string
wantErr bool
expectedResult bool
in in
}{
{
name: "success case expecting true",
wantErr: false,
expectedResult: true,
in: in{
file: sMocks.FileReadExisterCustomMock{
ReadMock: func(path string) ([]byte, error) {
return []byte("yes"), nil
},
ExistsMock: func(path string) bool {
return true
},
},
},
},
{
name: "success case expecting false",
wantErr: false,
expectedResult: false,
in: in{
file: sMocks.FileReadExisterCustomMock{
ReadMock: func(path string) ([]byte, error) {
return []byte("no"), nil
},
ExistsMock: func(path string) bool {
return true
},
},
},
},
{
name: "success case when metrics file doesn't exist",
wantErr: false,
expectedResult: false,
in: in{
file: sMocks.FileReadExisterCustomMock{
ExistsMock: func(path string) bool {
return false
},
},
},
},
{
name: "fail case error on reading file",
wantErr: true,
expectedResult: false,
in: in{
file: sMocks.FileReadExisterCustomMock{
ReadMock: func(path string) ([]byte, error) {
return nil, errors.New("error reading file")
},
ExistsMock: func(path string) bool {
return true
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
check := NewChecker(tt.in.file)
result, err := check.Check()
if (err != nil) != tt.wantErr {
t.Errorf("execution test failed: %s\nwant error: %t | got: %s", tt.name, tt.wantErr, err)
}
if result != tt.expectedResult {
t.Errorf("behavior test failed: %s\nwant: %t | got: %t", tt.name, tt.expectedResult, result)
}
})
}
}

15 changes: 9 additions & 6 deletions pkg/metric/metric.go → pkg/metrics/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package metric

package metrics

import (
"os"
"path/filepath"
"time"

"github.com/ZupIT/ritchie-cli/pkg/api"
)

func MetricsPath()string {
hd, _ := os.UserHomeDir()
return filepath.Join(hd, ".rit", "metrics")
}
var FilePath = filepath.Join(api.RitchieHomeDir(), "metrics")

type Id string

Expand All @@ -44,3 +43,7 @@ type Sender interface {
type UserIdGenerator interface {
Generate() (UserId, error)
}

type Checker interface {
Check() (bool, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package metric

package metrics

import (
"crypto/sha256"
Expand Down
Loading