Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report Usage - Improve Errors Messages #979

Merged
merged 2 commits into from
Sep 28, 2023
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
40 changes: 9 additions & 31 deletions utils/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package usage

import (
"fmt"
"golang.org/x/sync/errgroup"

"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
Expand All @@ -12,7 +11,7 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
ecosysusage "github.com/jfrog/jfrog-client-go/utils/usage"
xrayusage "github.com/jfrog/jfrog-client-go/xray/usage"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -47,7 +46,6 @@ func NewUsageReporter(productId string, serverDetails *config.ServerDetails) *Us
serverDetails: serverDetails,
reportWaitGroup: new(errgroup.Group),
sendToEcosystem: true,
sendToXray: true,
sendToArtifactory: true,
}
}
Expand Down Expand Up @@ -110,20 +108,23 @@ func (ur *UsageReporter) WaitForResponses() (err error) {

func (ur *UsageReporter) reportToEcosystem(features ...ReportFeature) (err error) {
if ur.serverDetails.Url == "" {
err = errorutils.CheckErrorf("platform Url is not set")
err = errorutils.CheckErrorf("platform URL is not set")
return
}
reports, err := ur.convertAttributesToEcosystemReports(features...)
if len(reports) == 0 || err != nil {
err = errorutils.CheckErrorf("Nothing to send.")
if err != nil {
return
}
if len(reports) == 0 {
err = errorutils.CheckErrorf("nothing to send")
return
}
return ecosysusage.SendEcosystemUsageReports(reports...)
}

func (ur *UsageReporter) reportToArtifactory(features ...ReportFeature) (err error) {
if ur.serverDetails.ArtifactoryUrl == "" {
err = errorutils.CheckErrorf("Artifactory Url is not set..")
err = errorutils.CheckErrorf("Artifactory URL is not set")
return
}
serviceManager, err := utils.CreateServiceManager(ur.serverDetails, -1, 0, false)
Expand All @@ -132,7 +133,7 @@ func (ur *UsageReporter) reportToArtifactory(features ...ReportFeature) (err err
}
converted := ur.convertAttributesToArtifactoryFeatures(features...)
if len(converted) == 0 {
err = errorutils.CheckErrorf("Nothing to send.")
err = errorutils.CheckErrorf("nothing to send")
return
}
return usage.ReportUsageToArtifactory(ur.ProductId, serviceManager, converted...)
Expand Down Expand Up @@ -163,29 +164,6 @@ func (ur *UsageReporter) convertAttributesToArtifactoryFeatures(reportFeatures .
return
}

func (ur *UsageReporter) convertAttributesToXrayEvents(reportFeatures ...ReportFeature) (events []xrayusage.ReportXrayEventData) {
for _, feature := range reportFeatures {
convertedAttributes := []xrayusage.ReportUsageAttribute{}
for _, attribute := range feature.Attributes {
convertedAttributes = append(convertedAttributes, xrayusage.ReportUsageAttribute{
AttributeName: attribute.AttributeName,
AttributeValue: attribute.AttributeValue,
})
}
if feature.ClientId != "" {
// Add clientId as attribute
convertedAttributes = append(convertedAttributes, xrayusage.ReportUsageAttribute{
AttributeName: clientIdAttributeName,
AttributeValue: feature.ClientId,
})
}
events = append(events, xrayusage.CreateUsageEvent(
ur.ProductId, feature.FeatureId, convertedAttributes...,
))
}
return
}

func (ur *UsageReporter) convertAttributesToEcosystemReports(reportFeatures ...ReportFeature) (reports []ecosysusage.ReportEcosystemUsageData, err error) {
accountId := ur.serverDetails.Url
clientToFeaturesMap := map[string][]string{}
Expand Down
32 changes: 1 addition & 31 deletions utils/usage/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/artifactory/usage"
ecosysusage "github.com/jfrog/jfrog-client-go/utils/usage"
xrayusage "github.com/jfrog/jfrog-client-go/xray/usage"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -40,28 +39,6 @@ var (
{FeatureId: "featureId", ClientId: "clientId2"},
{FeatureId: "featureId"},
}
xrayEvents = []xrayusage.ReportXrayEventData{
{
ProductId: productName,
EventId: "server_" + productName + "_featureId2",
Attributes: map[string]string{
"clientId": "clientId",
"attribute": "value",
},
Origin: "API_CLI",
},
{
ProductId: productName,
EventId: "server_" + productName + "_featureId",
Attributes: map[string]string{"clientId": "clientId2"},
Origin: "API_CLI",
},
{
ProductId: productName,
EventId: "server_" + productName + "_featureId",
Origin: "API_CLI",
},
}
ecosystemData = []ecosysusage.ReportEcosystemUsageData{
{
ProductId: productName,
Expand Down Expand Up @@ -90,13 +67,6 @@ func TestConvertToArtifactoryUsage(t *testing.T) {
}
}

func TestConvertToXrayUsage(t *testing.T) {
reporter := NewUsageReporter(productName, &config.ServerDetails{XrayUrl: serverUrl + "/"})
for i := 0; i < len(features); i++ {
assert.Equal(t, xrayEvents[i], reporter.convertAttributesToXrayEvents(features[i])[0])
}
}

func TestConvertToEcosystemUsage(t *testing.T) {
reporter := NewUsageReporter(productName, &config.ServerDetails{Url: serverUrl})
for i := 0; i < len(features); i++ {
Expand Down Expand Up @@ -173,7 +143,7 @@ func TestReportEcosystemUsageError(t *testing.T) {
}

func create404UsageHandler(t *testing.T) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
}
}
Loading