use of org.ligoj.app.plugin.prov.model.ProvUsage 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.ProvUsage in project plugin-prov by ligoj.
the class ProvUsageResource method delete.
/**
* Delete an usage. When the usage is associated to a quote or a resource, it is replaced by a <code>null</code>
* reference.
*
* @param subscription
* The subscription identifier, will be used to filter the usages from the associated provider.
* @param name
* The {@link ProvUsage} name.
* @return The updated cost. Only relevant when at least one resource was associated to this usage.
*/
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Path("{subscription:\\d+}/usage/{name}")
public UpdatedCost delete(@PathParam("subscription") final int subscription, @PathParam("name") final String name) {
final ProvUsage entity = resource.findConfiguredByName(repository, name, subscription);
final ProvQuote quote = entity.getConfiguration();
final UpdatedCost cost = new UpdatedCost();
// Prepare the updated cost of updated instances
final Map<Integer, FloatingCost> costs = cost.getRelatedCosts();
cost.setRelatedCosts(costs);
// Update the cost of all related instances
if (entity.equals(quote.getUsage())) {
// Update cost of all instances without explicit usage
quote.setUsage(null);
quote.getInstances().stream().filter(i -> i.getUsage() == null).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
}
quote.getInstances().stream().filter(i -> entity.equals(i.getUsage())).peek(i -> i.setUsage(null)).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
// All references are deleted, delete the usage entity
repository.delete(entity);
// Save and update the costs
cost.setTotalCost(resource.toFloatingCost(entity.getConfiguration()));
return cost;
}
use of org.ligoj.app.plugin.prov.model.ProvUsage in project plugin-prov by ligoj.
the class ProvQuoteInstanceResourceTest method lookupInstanceVariableDuration.
/**
* Low usage rate, cheaper than 1y
*/
@Test
public void lookupInstanceVariableDuration() {
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Dev"), "C9", "instance2", 58.56, "on-demand2");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Full Time 11 month"), "C11", "instance2", 102.48, "1y");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Full Time 12 month"), "C11", "instance2", 102.48, "1y");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Full Time 13 month"), "C9", "instance2", 117.12, "on-demand2");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Full Time 23 month"), "C11", "instance2", 102.48, "1y");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Full Time 24 month"), "C11", "instance2", 102.48, "1y");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Full Time 25 month"), "C9", "instance2", 117.12, "on-demand2");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Dev 11 month"), "C9", "instance2", 29.28, "on-demand2");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Dev 12 month"), "C9", "instance2", 29.28, "on-demand2");
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Dev 13 month"), "C9", "instance2", 29.28, "on-demand2");
ProvUsage usage = usageRepository.findByName("Dev 11 month");
usage.setRate(90);
usageRepository.saveAndFlush(usage);
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Dev 11 month"), "C9", "instance2", 105.408, "on-demand2");
usage.setRate(98);
usageRepository.saveAndFlush(usage);
assertPrice(qiResource.lookup(subscription, 1, 2000, null, VmOs.LINUX, null, true, null, "Dev 11 month"), "C11", "instance2", 102.48, "1y");
}
use of org.ligoj.app.plugin.prov.model.ProvUsage in project plugin-prov by ligoj.
the class ProvUsageResource method create.
/**
* Create the usage inside a quote. No cost are updated during this operation since this new {@link ProvUsage} is
* not yet used.
*
* @param subscription
* The subscription identifier, will be used to filter the usages from the associated provider.
* @param vo
* The quote usage.
* @return The created usage identifier.
*/
@POST
@Path("{subscription:\\d+}/usage")
@Consumes(MediaType.APPLICATION_JSON)
public int create(@PathParam("subscription") final int subscription, final UsageEditionVo vo) {
final ProvQuote configuration = resource.getQuoteFromSubscription(subscription);
final ProvUsage entity = new ProvUsage();
entity.setConfiguration(configuration);
return saveOrUpdate(entity, vo).getId();
}
use of org.ligoj.app.plugin.prov.model.ProvUsage in project plugin-prov by ligoj.
the class ProvUsageResource method saveOrUpdate.
/**
* Save or update the given usage entity from the {@link UsageEditionVo}. The computed cost are recursively updated
* from the related instances to the quote total cost.<br>
* The cost of all instances related to this usage will be updated to get the new term and related price.<br>
* An instance related to this usage is either an instance explicitly linked to this usage, either an instance
* linked to a quote having this usage as default.
*/
private UpdatedCost saveOrUpdate(final ProvUsage entity, final UsageEditionVo vo) {
// Check the associations and copy attributes to the entity
entity.setRate(vo.getRate());
entity.setDuration(vo.getDuration());
entity.setName(vo.getName());
final UpdatedCost cost = new UpdatedCost();
if (entity.getId() != null) {
final ProvQuote quote = entity.getConfiguration();
// Prepare the updated cost of updated instances
final Map<Integer, FloatingCost> costs = cost.getRelatedCosts();
cost.setRelatedCosts(costs);
// Update the cost of all related instances
if (entity.equals(quote.getUsage())) {
// Update cost of all instances without explicit usage
quote.getInstances().stream().filter(i -> i.getUsage() == null).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
}
quote.getInstances().stream().filter(i -> entity.equals(i.getUsage())).forEach(i -> costs.put(i.getId(), instanceResource.addCost(i, instanceResource::refresh)));
// Save and update the costs
cost.setRelatedCosts(costs);
}
repository.saveAndFlush(entity);
cost.setId(entity.getId());
cost.setTotalCost(resource.toFloatingCost(entity.getConfiguration()));
return cost;
}
Aggregations