Skip to content

Commit

Permalink
Add missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Dec 20, 2024
1 parent 0310518 commit 26fe468
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions utils/usage/visibility_system_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package usage

import (
"encoding/json"
"os"

"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
)

type VisibilitySystemManager struct {
serverDetails *config.ServerDetails
}

func NewVisibilitySystemManager(serverDetails *config.ServerDetails) *VisibilitySystemManager {
return &VisibilitySystemManager{
serverDetails: serverDetails,
}
}

type labels struct {
ProductID string `json:"product_id"`
FeatureID string `json:"feature_id"`
OIDCUsed string `json:"oidc_used"`
JobID string `json:"job_id"`
RunID string `json:"run_id"`
GitRepo string `json:"git_repo"`
GhTokenForCodeScanningAlertsProvided string `json:"gh_token_for_code_scanning_alerts_provided"`
}

type visibilityMetric struct {
Value int `json:"value"`
MetricsName string `json:"metrics_name"`
Labels labels `json:"labels"`
}

func createMetric(commandName string) ([]byte, error) {
metricLabels := labels{
ProductID: coreutils.GetCliUserAgentName(),
FeatureID: commandName,
OIDCUsed: os.Getenv("JFROG_CLI_USAGE_OIDC_USED"),
JobID: os.Getenv("JFROG_CLI_USAGE_JOB_ID"),
RunID: os.Getenv("JFROG_CLI_USAGE_RUN_ID"),
GitRepo: os.Getenv("JFROG_CLI_USAGE_GIT_REPO"),
GhTokenForCodeScanningAlertsProvided: os.Getenv("JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED"),
}

metric := visibilityMetric{
Value: 1,
MetricsName: "jfcli_commands_countaa",
Labels: metricLabels,
}

return json.Marshal(metric)
}

func (vsm *VisibilitySystemManager) SendUsage(commandName string) error {
manager, err := utils.CreateJfConnectServiceManager(vsm.serverDetails)
if err != nil {
return err
}
metric, err := createMetric(commandName)
if err != nil {
return err
}
return manager.LogMetric(metric)
}

0 comments on commit 26fe468

Please sign in to comment.