Skip to content

Commit

Permalink
Restructure visibility metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi committed Jan 15, 2025
1 parent 43fec6d commit bfbba9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
43 changes: 43 additions & 0 deletions jfconnect/services/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package services

import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"net/http"
)

const LogMetricApiEndpoint = "api/v1/backoffice/metrics/log"

type JfConnectService struct {
client *jfroghttpclient.JfrogHttpClient
serviceDetails *auth.ServiceDetails
}

func NewJfConnectService(serviceDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *JfConnectService {
return &JfConnectService{serviceDetails: &serviceDetails, client: client}
}

func (jcs *JfConnectService) GetJfConnectDetails() auth.ServiceDetails {
return *jcs.serviceDetails
}

func (jcs *JfConnectService) PostVisibilityMetric(metric VisibilityMetric) error {
metricJson, err := json.Marshal(metric)
if err != nil {
return errorutils.CheckError(err)
}
details := jcs.GetJfConnectDetails()
httpClientDetails := details.CreateHttpClientDetails()
httpClientDetails.SetContentTypeApplicationJson()

url := clientutils.AddTrailingSlashIfNeeded(details.GetUrl())
url += LogMetricApiEndpoint
resp, body, err := jcs.client.SendPost(url, metricJson, &httpClientDetails)
if err != nil {
return err
}
return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusCreated, http.StatusOK)
}
46 changes: 6 additions & 40 deletions jfconnect/services/metrics.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,19 @@
package services

import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"net/http"
)

const LogMetricApiEndpoint = "api/v1/backoffice/metrics/log"

type VisibilityMetric interface {
Value() int
MetricsName() string
MetricValue() int
MetricName() string
}

type Metric struct {
Value int `json:"value"`
Name string `json:"metrics_name"`
}

type JfConnectService struct {
client *jfroghttpclient.JfrogHttpClient
serviceDetails *auth.ServiceDetails
}

func NewJfConnectService(serviceDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *JfConnectService {
return &JfConnectService{serviceDetails: &serviceDetails, client: client}
func (m *Metric) MetricValue() int {
return m.Value
}

func (jcs *JfConnectService) GetJfConnectDetails() auth.ServiceDetails {
return *jcs.serviceDetails
}

func (jcs *JfConnectService) PostVisibilityMetric(metric VisibilityMetric) error {
metricJson, err := json.Marshal(metric)
if err != nil {
return errorutils.CheckError(err)
}
details := jcs.GetJfConnectDetails()
httpClientDetails := details.CreateHttpClientDetails()
httpClientDetails.SetContentTypeApplicationJson()

url := clientutils.AddTrailingSlashIfNeeded(details.GetUrl())
url += LogMetricApiEndpoint
resp, body, err := jcs.client.SendPost(url, metricJson, &httpClientDetails)
if err != nil {
return err
}
return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusCreated, http.StatusOK)
func (m *Metric) MetricName() string {
return m.Name
}

0 comments on commit bfbba9e

Please sign in to comment.