From bfbba9e884e6d800046716d530a06a44fc145810 Mon Sep 17 00:00:00 2001 From: Omer Zidkoni <50792403+omerzi@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:11:43 +0200 Subject: [PATCH] Restructure visibility metrics --- jfconnect/services/connect.go | 43 ++++++++++++++++++++++++++++++++ jfconnect/services/metrics.go | 46 +++++------------------------------ 2 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 jfconnect/services/connect.go diff --git a/jfconnect/services/connect.go b/jfconnect/services/connect.go new file mode 100644 index 000000000..85a3f3e4e --- /dev/null +++ b/jfconnect/services/connect.go @@ -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) +} diff --git a/jfconnect/services/metrics.go b/jfconnect/services/metrics.go index 54f12e9eb..5a04216ca 100644 --- a/jfconnect/services/metrics.go +++ b/jfconnect/services/metrics.go @@ -1,19 +1,8 @@ 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 { @@ -21,33 +10,10 @@ type Metric struct { 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 }