Search in sources :

Example 6 with ProvInstancePrice

use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov by ligoj.

the class ProvQuoteInstanceResource method lookup.

private QuoteInstanceLookup lookup(final ProvQuote configuration, final double cpu, final int ram, final Boolean constant, final VmOs osName, final String type, final boolean ephemeral, final String location, final String usageName) {
    final String node = configuration.getSubscription().getNode().getId();
    final int subscription = configuration.getSubscription().getId();
    // Resolve
    final VmOs os = Optional.ofNullable(osName).map(VmOs::toPricingOs).orElse(null);
    // Resolve the location to use
    final String locationR = location == null ? configuration.getLocation().getName() : location;
    // Compute the rate to use
    final ProvUsage usage = usageName == null ? ObjectUtils.defaultIfNull(configuration.getUsage(), USAGE_DEFAULT) : resource.findConfiguredByName(usageRepository, usageName, subscription);
    final double rate = usage.getRate() / 100d;
    final int duration = usage.getDuration();
    // Resolve the required instance type
    final Integer typeId = type == null ? null : assertFound(itRepository.findByName(subscription, type), type).getId();
    // Return only the first matching instance
    // Template instance
    final QuoteInstanceLookup template = ipRepository.findLowestPrice(node, cpu, ram, constant, os, typeId, ephemeral, locationR, rate, duration, PageRequest.of(0, 1)).stream().findFirst().map(ip -> newPrice((ProvInstancePrice) ip[0], (double) ip[2])).orElse(null);
    // Custom instance
    final QuoteInstanceLookup custom = ipRepository.findLowestCustomPrice(node, Math.ceil(cpu), Math.ceil(ram / 1024), constant, os, locationR, PageRequest.of(0, 1)).stream().findFirst().map(ip -> newPrice((ProvInstancePrice) ip[0], rate * (double) ip[1])).orElse(null);
    // Select the best price term
    if (template == null) {
        return custom;
    }
    if (custom == null) {
        return template;
    }
    return custom.getCost() < template.getCost() ? custom : template;
}
Also used : CsvForBean(org.ligoj.bootstrap.core.csv.CsvForBean) Produces(javax.ws.rs.Produces) ProvInstancePriceRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceRepository) Path(javax.ws.rs.Path) ProvUsageRepository(org.ligoj.app.plugin.prov.dao.ProvUsageRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ByteArrayInputStream(java.io.ByteArrayInputStream) Consumes(javax.ws.rs.Consumes) ProvLocation(org.ligoj.app.plugin.prov.model.ProvLocation) ProvInstanceTypeRepository(org.ligoj.app.plugin.prov.dao.ProvInstanceTypeRepository) DataTableAttributes(org.ligoj.bootstrap.core.json.datatable.DataTableAttributes) Map(java.util.Map) Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) DefaultValue(javax.ws.rs.DefaultValue) Subscription(org.ligoj.app.model.Subscription) DescribedBean(org.ligoj.bootstrap.core.DescribedBean) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) Transactional(javax.transaction.Transactional) PageRequest(org.springframework.data.domain.PageRequest) Reader(java.io.Reader) StandardCharsets(java.nio.charset.StandardCharsets) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) Objects(java.util.Objects) ProvQuoteStorageRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteStorageRepository) IOUtils(org.apache.commons.io.IOUtils) Slf4j(lombok.extern.slf4j.Slf4j) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) TableItem(org.ligoj.bootstrap.core.json.TableItem) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) HashMap(java.util.HashMap) ArrayUtils(org.apache.commons.lang3.ArrayUtils) BooleanUtils(org.apache.commons.lang3.BooleanUtils) ProvQuoteInstance(org.ligoj.app.plugin.prov.model.ProvQuoteInstance) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Service(org.springframework.stereotype.Service) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage) POST(javax.ws.rs.POST) SequenceInputStream(java.io.SequenceInputStream) ProvQuoteInstanceRepository(org.ligoj.app.plugin.prov.dao.ProvQuoteInstanceRepository) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) ProvInstancePriceTermRepository(org.ligoj.app.plugin.prov.dao.ProvInstancePriceTermRepository) VmOs(org.ligoj.app.plugin.prov.model.VmOs) StringReader(java.io.StringReader) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm) PUT(javax.ws.rs.PUT) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) BufferedReader(java.io.BufferedReader) ProvInstanceType(org.ligoj.app.plugin.prov.model.ProvInstanceType) InputStream(java.io.InputStream) VmOs(org.ligoj.app.plugin.prov.model.VmOs) ProvUsage(org.ligoj.app.plugin.prov.model.ProvUsage)

Example 7 with ProvInstancePrice

use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov by ligoj.

the class ProvQuoteInstanceResource method getCost.

@Override
protected FloatingCost getCost(final ProvQuoteInstance qi) {
    // Fixed price + custom price
    final ProvInstancePrice ip = qi.getPrice();
    final double rate;
    if (ip.getTerm().getPeriod() == 0) {
        // Related term has a period lesser than the month, rate applies
        rate = getRate(qi) / 100d;
    } else {
        rate = 1d;
    }
    return computeFloat(rate * (ip.getCost() + (ip.getType().isCustom() ? getCustomCost(qi.getCpu(), qi.getRam(), ip) : 0)), qi);
}
Also used : ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice)

Example 8 with ProvInstancePrice

use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov-azure by ligoj.

the class ProvAzurePriceImportResource method installInstancePrice.

private ProvInstancePrice installInstancePrice(final UpdateContext context, final ProvInstancePriceTerm term, final VmOs os, final String globalCode, final ProvInstanceType type, final String region) {
    final Map<String, ProvInstancePrice> previous = term.getName().equals("lowpriority") ? context.getPreviousLowPriority() : context.getPrevious();
    final ProvInstancePrice price = previous.computeIfAbsent(region + "-" + globalCode, code -> {
        // New instance price (not update mode)
        final ProvInstancePrice newPrice = new ProvInstancePrice();
        newPrice.setCode(code);
        newPrice.setLocation(context.getRegions().get(region));
        newPrice.setOs(os);
        newPrice.setTerm(term);
        newPrice.setTenancy(dedicatedTypes.contains(type.getName()) ? ProvTenancy.DEDICATED : ProvTenancy.SHARED);
        newPrice.setType(type);
        return newPrice;
    });
    return price;
}
Also used : ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice)

Example 9 with ProvInstancePrice

use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov-azure by ligoj.

the class ProvAzurePriceImportResourceTest method installOnLine.

@Test
public void installOnLine() throws Exception {
    configuration.delete(ProvAzurePriceImportResource.CONF_API_PRICES);
    configuration.saveOrUpdate(ProvAzurePriceImportResource.CONF_REGIONS, "europe-north");
    // Check the reserved
    final QuoteVo quote = installAndConfigure();
    Assertions.assertTrue(quote.getCost().getMin() > 150);
    // Check the spot
    final QuoteInstanceLookup lookup = qiResource.lookup(subscription, 8, 26000, true, VmOs.LINUX, "ds4v2", false, null, "36month");
    Assertions.assertTrue(lookup.getCost() > 100d);
    final ProvInstancePrice instance2 = lookup.getPrice();
    Assertions.assertEquals("base-three-year", instance2.getTerm().getName());
    Assertions.assertEquals("ds4v2", instance2.getType().getName());
}
Also used : QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) QuoteInstanceLookup(org.ligoj.app.plugin.prov.QuoteInstanceLookup) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 10 with ProvInstancePrice

use of org.ligoj.app.plugin.prov.model.ProvInstancePrice in project plugin-prov-azure by ligoj.

the class ProvAzurePriceImportResourceTest method checkInstance.

private ProvQuoteInstance checkInstance(final ProvQuoteInstance instance, final double cost) {
    Assertions.assertEquals(cost, instance.getCost(), DELTA);
    final ProvInstancePrice price = instance.getPrice();
    Assertions.assertNull(price.getInitialCost());
    Assertions.assertEquals(VmOs.LINUX, price.getOs());
    Assertions.assertEquals(ProvTenancy.SHARED, price.getTenancy());
    Assertions.assertEquals(150.28, price.getCost(), DELTA);
    Assertions.assertEquals(0.2053, price.getCostPeriod(), DELTA);
    final ProvInstancePriceTerm priceType = price.getTerm();
    Assertions.assertEquals("base-three-year", priceType.getName());
    Assertions.assertFalse(priceType.isEphemeral());
    Assertions.assertEquals(36, priceType.getPeriod());
    Assertions.assertEquals("ds4v2", price.getType().getName());
    return instance;
}
Also used : ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ProvInstancePriceTerm(org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)

Aggregations

ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)16 Test (org.junit.jupiter.api.Test)7 ProvInstancePriceTerm (org.ligoj.app.plugin.prov.model.ProvInstancePriceTerm)6 AbstractAppTest (org.ligoj.app.AbstractAppTest)5 ProvInstanceType (org.ligoj.app.plugin.prov.model.ProvInstanceType)5 ProvQuoteInstance (org.ligoj.app.plugin.prov.model.ProvQuoteInstance)4 ProvStoragePrice (org.ligoj.app.plugin.prov.model.ProvStoragePrice)4 ProvStorageType (org.ligoj.app.plugin.prov.model.ProvStorageType)3 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Slf4j (lombok.extern.slf4j.Slf4j)2 IOUtils (org.apache.commons.io.IOUtils)2 Node (org.ligoj.app.model.Node)2 ProvLocation (org.ligoj.app.plugin.prov.model.ProvLocation)2 CurlProcessor (org.ligoj.app.resource.plugin.CurlProcessor)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1