Skip to content

Commit

Permalink
Change "swatch_producer_metered_total" in metric units
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Feb 6, 2025
1 parent 5ee872d commit 6cf175e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package com.redhat.swatch.aws.service;

import static com.redhat.swatch.configuration.registry.SubscriptionDefinition.getBillingFactor;

import com.redhat.swatch.aws.exception.AwsMissingCredentialsException;
import com.redhat.swatch.aws.exception.AwsThrottlingException;
import com.redhat.swatch.aws.exception.AwsUnprocessedRecordsException;
Expand Down Expand Up @@ -345,16 +347,16 @@ private void emitSuccessfulStatusOnUsage(BillableUsageAggregate usage) {
usage.setStatus(BillableUsage.Status.SUCCEEDED);
usage.setErrorCode(null);
usage.setBilledOn(OffsetDateTime.now());
billableUsageStatusProducer.emitStatus(usage);
incrementMeteredTotal(usage);
billableUsageStatusProducer.emitStatus(usage);
}

private void emitErrorStatusOnUsage(
BillableUsageAggregate usage, BillableUsage.ErrorCode errorCode) {
usage.setStatus(BillableUsage.Status.FAILED);
usage.setErrorCode(errorCode);
billableUsageStatusProducer.emitStatus(usage);
incrementMeteredTotal(usage);
billableUsageStatusProducer.emitStatus(usage);
}

private void incrementMeteredTotal(BillableUsageAggregate usage) {
Expand All @@ -380,7 +382,10 @@ private void incrementMeteredTotal(BillableUsageAggregate usage) {
aggregateKey.getProductId(),
"metric_id",
MetricId.fromString(aggregateKey.getMetricId()).getValue())
.increment(usage.getTotalValue().doubleValue());
.increment(
usage.getTotalValue().doubleValue()
/ getBillingFactor(
usage.getAggregateKey().getProductId(), usage.getAggregateKey().getMetricId()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.redhat.swatch.azure.service;

import static com.redhat.swatch.azure.configuration.Channels.BILLABLE_USAGE_HOURLY_AGGREGATE;
import static com.redhat.swatch.configuration.registry.SubscriptionDefinition.getBillingFactor;

import com.redhat.swatch.azure.exception.AzureMarketplaceRequestFailedException;
import com.redhat.swatch.azure.exception.AzureUnprocessedRecordsException;
Expand Down Expand Up @@ -282,16 +283,16 @@ private void emitSuccessfulStatusOnUsage(BillableUsageAggregate usage) {
usage.setStatus(BillableUsage.Status.SUCCEEDED);
usage.setErrorCode(null);
usage.setBilledOn(OffsetDateTime.now());
billableUsageStatusProducer.emitStatus(usage);
incrementMeteredTotal(usage);
billableUsageStatusProducer.emitStatus(usage);
}

private void emitErrorStatusOnUsage(
BillableUsageAggregate usage, BillableUsage.ErrorCode errorCode) {
usage.setStatus(BillableUsage.Status.FAILED);
usage.setErrorCode(errorCode);
billableUsageStatusProducer.emitStatus(usage);
incrementMeteredTotal(usage);
billableUsageStatusProducer.emitStatus(usage);
}

private void incrementMeteredTotal(BillableUsageAggregate usage) {
Expand All @@ -316,6 +317,9 @@ private void incrementMeteredTotal(BillableUsageAggregate usage) {
usage.getAggregateKey().getProductId(),
"metric_id",
MetricId.tryGetValueFromString(usage.getAggregateKey().getMetricId()))
.increment(usage.getTotalValue().doubleValue());
.increment(
usage.getTotalValue().doubleValue()
/ getBillingFactor(
usage.getAggregateKey().getProductId(), usage.getAggregateKey().getMetricId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.redhat.swatch.azure.configuration.Channels.BILLABLE_USAGE_STATUS;
import static com.redhat.swatch.azure.service.AzureBillableUsageAggregateConsumer.METERED_TOTAL_METRIC;
import static com.redhat.swatch.azure.test.resources.InMemoryMessageBrokerKafkaResource.IN_MEMORY_CONNECTOR;
import static com.redhat.swatch.configuration.registry.SubscriptionDefinition.getBillingFactor;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -228,7 +229,8 @@ private void thenMeteredTotalMetricIsPopulated() {
assertTrue(metric.isPresent());
assertEquals(
metric.get().measure().iterator().next().getValue(),
EXPECTED_VALUE.doubleValue());
EXPECTED_VALUE.doubleValue()
/ getBillingFactor(PRODUCT_ID, MetricIdUtils.getCores().getValue()));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ public List<SubscriptionDefinitionGranularity> getSupportedGranularity() {
return granularity;
}

public static double getBillingFactor(String tag, String metricId) {
var metricOptional =
Variant.findByTag(tag)
.map(Variant::getSubscription)
.flatMap(
subscriptionDefinition ->
subscriptionDefinition.getMetric(MetricId.fromString(metricId).getValue()));
return metricOptional.map(Metric::getBillingFactor).orElse(1.0);
}

public static boolean supportsGranularity(SubscriptionDefinition sub, String granularity) {
return sub.getSupportedGranularity().stream()
.map(x -> x.toString().toLowerCase())
Expand Down

0 comments on commit 6cf175e

Please sign in to comment.