From 6a1633bc2eddf9cb1f3bd151f1e3e14da1373602 Mon Sep 17 00:00:00 2001 From: Rob Best Date: Wed, 30 Nov 2022 08:46:15 +0000 Subject: [PATCH] Add project metrics Signed-off-by: Rob Best --- metrics.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ project.go | 1 + 2 files changed, 76 insertions(+) diff --git a/metrics.go b/metrics.go index b6c4ecc..f25c816 100644 --- a/metrics.go +++ b/metrics.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "time" + + "github.com/google/uuid" ) type PortfolioMetrics struct { @@ -42,6 +44,39 @@ type PortfolioMetrics struct { PolicyViolationsOperationalUnaudited int `json:"policyViolationsOperationalUnaudited"` } +type ProjectMetrics struct { + FirstOccurrence int `json:"firstOccurrence"` + LastOccurrence int `json:"lastOccurrence"` + InheritedRiskScore float64 `json:"inheritedRiskScore"` + Vulnerabilities int `json:"vulnerabilities"` + VulnerableComponents int `json:"vulnerableComponents"` + Components int `json:"components"` + Suppressed int `json:"suppressed"` + Critical int `json:"critical"` + High int `json:"high"` + Medium int `json:"medium"` + Low int `json:"low"` + Unassigned int `json:"unassigned"` + FindingsTotal int `json:"findingsTotal"` + FindingsAudited int `json:"findingsAudited"` + FindingsUnaudited int `json:"findingsUnaudited"` + PolicyViolationsTotal int `json:"policyViolationsTotal"` + PolicyViolationsFail int `json:"policyViolationsFail"` + PolicyViolationsWarn int `json:"policyViolationsWarn"` + PolicyViolationsInfo int `json:"policyViolationsInfo"` + PolicyViolationsAudited int `json:"policyViolationsAudited"` + PolicyViolationsUnaudited int `json:"policyViolationsUnaudited"` + PolicyViolationsSecurityTotal int `json:"policyViolationsSecurityTotal"` + PolicyViolationsSecurityAudited int `json:"policyViolationsSecurityAudited"` + PolicyViolationsSecurityUnaudited int `json:"policyViolationsSecurityUnaudited"` + PolicyViolationsLicenseTotal int `json:"policyViolationsLicenseTotal"` + PolicyViolationsLicenseAudited int `json:"policyViolationsLicenseAudited"` + PolicyViolationsLicenseUnaudited int `json:"policyViolationsLicenseUnaudited"` + PolicyViolationsOperationalTotal int `json:"policyViolationsOperationalTotal"` + PolicyViolationsOperationalAudited int `json:"policyViolationsOperationalAudited"` + PolicyViolationsOperationalUnaudited int `json:"policyViolationsOperationalUnaudited"` +} + type MetricsService struct { client *Client } @@ -85,3 +120,43 @@ func (ms MetricsService) RefreshPortfolioMetrics(ctx context.Context) (err error _, err = ms.client.doRequest(req, nil) return } + +func (ms MetricsService) LatestProjectMetrics(ctx context.Context, projectUUID uuid.UUID) (m ProjectMetrics, err error) { + req, err := ms.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/metrics/project/%s/current", projectUUID)) + if err != nil { + return + } + + _, err = ms.client.doRequest(req, &m) + return +} + +func (ms MetricsService) ProjectMetricsSince(ctx context.Context, projectUUID uuid.UUID, date time.Time) (m []ProjectMetrics, err error) { + req, err := ms.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/metrics/project/%s/since/%s", projectUUID, date.Format("20060102"))) + if err != nil { + return + } + + _, err = ms.client.doRequest(req, &m) + return +} + +func (ms MetricsService) ProjectMetricsSinceDays(ctx context.Context, projectUUID uuid.UUID, days uint) (m []ProjectMetrics, err error) { + req, err := ms.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/metrics/project/%s/days/%d", projectUUID, days)) + if err != nil { + return nil, err + } + + _, err = ms.client.doRequest(req, &m) + return +} + +func (ms MetricsService) RefreshProjectMetrics(ctx context.Context, projectUUID uuid.UUID) (err error) { + req, err := ms.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/metrics/project/%s/refresh", projectUUID)) + if err != nil { + return + } + + _, err = ms.client.doRequest(req, nil) + return +} diff --git a/project.go b/project.go index fde6114..22b4b27 100644 --- a/project.go +++ b/project.go @@ -24,6 +24,7 @@ type Project struct { Properties []ProjectProperty `json:"properties,omitempty"` Tags []Tag `json:"tags,omitempty"` Active bool `json:"active"` + Metrics ProjectMetrics `json:"metrics"` } type ProjectService struct {