Skip to content

Commit

Permalink
fix setting quota id when quota is unlimited
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed Oct 31, 2024
1 parent a525a97 commit 3980abf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
55 changes: 21 additions & 34 deletions pkg/transaction/metric/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
metricStr = "metric"
volumeStr = "volume"
countStr = "count"
defaultUnit = "transactions"
)

var exitMetricInit = false
Expand Down Expand Up @@ -271,6 +272,7 @@ func (c *collector) AddMetric(apiDetails models.APIDetails, statusCode string, d
func (c *collector) AddMetricDetail(metricDetail Detail) {
c.AddMetric(metricDetail.APIDetails, metricDetail.StatusCode, metricDetail.Duration, metricDetail.Bytes, metricDetail.APIDetails.Name)
c.createOrUpdateHistogram(metricDetail)

}

// AddAPIMetricDetail - add metric details for several response codes and transactions
Expand All @@ -284,15 +286,15 @@ func (c *collector) AddAPIMetricDetail(detail MetricDetail) {
AppDetails: detail.AppDetails,
Status: detail.StatusCode,
}
newMetric := c.createMetric(transactionCtx)
metric := c.createMetric(transactionCtx)

// update the new metric with all the necessary details
newMetric.Units.Transactions.Count = detail.Count
newMetric.Units.Transactions.Response = &detail.Response
newMetric.Observation = &detail.Observation
metric.Units.Transactions.Count = detail.Count
metric.Units.Transactions.Response = &detail.Response
metric.Observation = &detail.Observation

c.updateStartTime()
c.addMetric(newMetric)
c.addMetric(metric)
}

// AddCustomMetricDetail - add custom unit metric details for an api/app combo
Expand Down Expand Up @@ -421,7 +423,7 @@ func (c *collector) createMetric(detail transactionContext) *centralMetric {
me.Units = &Units{
Transactions: &Transactions{
UnitCount: UnitCount{
Quota: c.getQuota(accessRequest, ""),
Quota: c.getQuota(accessRequest, defaultUnit),
},
Status: c.getStatusText(detail.Status),
},
Expand Down Expand Up @@ -662,40 +664,25 @@ func (c *collector) getQuota(accessRequest *management.AccessRequest, unitName s
if accessRequest == nil {
return nil
}

if unitName == "" && accessRequest.Spec.Quota == nil {
// no quota on transactions
return nil
if unitName == "" {
unitName = defaultUnit
}

quotaName := ""
if unitName == "" && accessRequest.Spec.Quota != nil {
// get transactions quota
for _, r := range accessRequest.References {
rMap := r.(map[string]interface{})
if rMap["kind"].(string) != catalog.QuotaGVK().Kind {
continue
}
if _, ok := rMap["unit"]; !ok {
// no unit is transactions
quotaName = strings.Split(rMap["name"].(string), "/")[2]
break
}

// get quota for unit
for _, r := range accessRequest.References {
rMap := r.(map[string]interface{})
if rMap["kind"].(string) != catalog.QuotaGVK().Kind {
continue
}
} else {
// get custom unit quota
for _, r := range accessRequest.References {
rMap := r.(map[string]interface{})
if rMap["kind"].(string) != catalog.QuotaGVK().Kind {
continue
}
if unit, ok := rMap["unit"]; ok && unitName == unit.(string) {
// no unit is transactions
quotaName = strings.Split(rMap["name"].(string), "/")[2]
break
}
if unit, ok := rMap["unit"]; ok && unit.(string) == unitName {
// no unit is transactions
quotaName = strings.Split(rMap["name"].(string), "/")[2]
break
}
}

if quotaName == "" {
return nil
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/transaction/metric/metricscollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ var (
Name: "111",
ConsumerOrgID: "org-id-111",
}
appDetails2 = models.AppDetails{
ID: "222",
Name: "222",
ConsumerOrgID: "org-id-222",
}
)

func getFutureTime() time.Time {
Expand Down

0 comments on commit 3980abf

Please sign in to comment.