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;
}
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);
}
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;
}
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());
}
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;
}
Aggregations