diff options
author | Valerij Fredriksen <valerijf@yahooinc.com> | 2023-10-11 10:54:16 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@yahooinc.com> | 2023-10-11 10:54:16 +0200 |
commit | 9909280794849eb8a98dbb18f8f756ae7fff781f (patch) | |
tree | 4129c5ddf0266cf95a391873f0428cec957bb13e | |
parent | abac62c851c27907c7a77c6ca5c7566fdbe64b75 (diff) |
Return amounts with 2 decimals
3 files changed, 16 insertions, 15 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MockPricingController.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MockPricingController.java index 871f51689d5..fb48694607a 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MockPricingController.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/MockPricingController.java @@ -8,7 +8,6 @@ import com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingControll import com.yahoo.vespa.hosted.controller.api.integration.pricing.PricingInfo; import java.math.BigDecimal; -import java.math.RoundingMode; import java.util.List; public class MockPricingController implements PricingController { @@ -20,11 +19,10 @@ public class MockPricingController implements PricingController { (resources.nodeResources().vcpu() * 1000 + resources.nodeResources().memoryGb() * 100 + resources.nodeResources().diskGb() * 10)) - .sum()) - .setScale(2, RoundingMode.HALF_UP); - BigDecimal volumeDiscount = new BigDecimal("-5.00"); + .sum()); + BigDecimal volumeDiscount = new BigDecimal("-5.64315634"); BigDecimal committedAmountDiscount = new BigDecimal("0.00"); - BigDecimal enclaveDiscount = new BigDecimal("0.00"); + BigDecimal enclaveDiscount = pricingInfo.enclave() ? new BigDecimal("-15.1234") : BigDecimal.ZERO; BigDecimal totalAmount = listPrice.add(volumeDiscount); return new PriceInformation(listPrice, volumeDiscount, committedAmountDiscount, enclaveDiscount, totalAmount); } diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java index b6b3c8584fd..955941b11cc 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandler.java @@ -22,13 +22,13 @@ import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses; import com.yahoo.yolean.Exceptions; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.logging.Logger; -import java.util.stream.Collectors; import static com.yahoo.jdisc.http.HttpRequest.Method.GET; import static com.yahoo.restapi.ErrorResponse.methodNotAllowed; @@ -46,7 +46,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; public class PricingApiHandler extends ThreadedHttpRequestHandler { private static final Logger log = Logger.getLogger(PricingApiHandler.class.getName()); - private static final BigDecimal SCALED_ZERO = BigDecimal.ZERO.setScale(2); private final Controller controller; @@ -104,7 +103,7 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler { case "resources" -> clusterResources.add(clusterResources(entry.getSecond())); } } - if (clusterResources.size() < 1) throw new IllegalArgumentException("No cluster resources found in query"); + if (clusterResources.isEmpty()) throw new IllegalArgumentException("No cluster resources found in query"); PricingInfo pricingInfo = new PricingInfo(enclave, supportLevel, committedSpend); return controller.serviceRegistry().pricingController().price(clusterResources, pricingInfo, plan); @@ -143,7 +142,7 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler { if (index <= 0 ) throw new IllegalArgumentException("Error in query parameter, expected '=' between key and value: " + element); return new Pair<>(element.substring(0, index), element.substring(index + 1)); }) - .collect(Collectors.toList()); + .toList(); } private Optional<Plan> plan(String element) { @@ -160,7 +159,7 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler { addItem(array, "Volume discount", priceInfo.volumeDiscount()); addItem(array, "Committed spend", priceInfo.committedAmountDiscount()); - cursor.setString("totalAmount", priceInfo.totalAmount().toPlainString()); + setBigDecimal(cursor, "totalAmount", priceInfo.totalAmount()); return new SlimeJsonResponse(slime); } @@ -169,8 +168,12 @@ public class PricingApiHandler extends ThreadedHttpRequestHandler { if (amount.compareTo(BigDecimal.ZERO) != 0) { var o = array.addObject(); o.setString("description", name); - o.setString("amount", SCALED_ZERO.add(amount).toPlainString()); + setBigDecimal(o, "amount", amount); } } + private static void setBigDecimal(Cursor cursor, String name, BigDecimal value) { + cursor.setString(name, value.setScale(2, RoundingMode.HALF_UP).toPlainString()); + } + } diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java index 09ff6bbc4b1..21f04acf941 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/pricing/PricingApiHandlerTest.java @@ -28,9 +28,9 @@ public class PricingApiHandlerTest extends ControllerContainerCloudTest { { "priceInfo": [ {"description": "List price", "amount": "2400.00"}, - {"description": "Volume discount", "amount": "-5.00"} + {"description": "Volume discount", "amount": "-5.64"} ], - "totalAmount": "2395.00" + "totalAmount": "2394.36" } """, 200); @@ -41,7 +41,7 @@ public class PricingApiHandlerTest extends ControllerContainerCloudTest { ContainerTester tester = new ContainerTester(container, responseFiles); assertEquals(SystemName.Public, tester.controller().system()); - var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformationWithMissingValueInResourcs()); + var request = request("/pricing/v1/pricing?" + urlEncodedPriceInformationWithMissingValueInResources()); tester.assertJsonResponse(request, "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Error in query parameter, expected '=' between key and value: resources\"}", 400); @@ -58,7 +58,7 @@ public class PricingApiHandlerTest extends ControllerContainerCloudTest { "&resources=" + resources; } - String urlEncodedPriceInformationWithMissingValueInResourcs() { + String urlEncodedPriceInformationWithMissingValueInResources() { return URLEncoder.encode("supportLevel=basic&committedSpend=0&enclave=false&resources", UTF_8); } |