Example usage for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryResult getSpotPriceHistory

List of usage examples for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryResult getSpotPriceHistory

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryResult getSpotPriceHistory.

Prototype


public java.util.List<SpotPrice> getSpotPriceHistory() 

Source Link

Document

The historical Spot prices.

Usage

From source file:imperial.modaclouds.monitoring.datacollectors.monitors.EC2SpotPriceMonitor.java

License:BSD License

@Override
public void run() {

    String accessKeyId = null;/*from  w ww.ja  va 2 s.c  om*/

    String secretKey = null;

    long startTime = 0;

    while (!spmt.isInterrupted()) {

        if (System.currentTimeMillis() - startTime > 10000) {
            spotInstanceVec = new ArrayList<SpotInstance>();

            for (String metric : getProvidedMetrics()) {
                try {
                    VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId());
                    if (dcAgent.shouldMonitor(resource, metric)) {

                        Map<String, String> parameters = dcAgent.getParameters(resource, metric);

                        String endpoint = null;
                        String productDes = null;
                        String instanceType = null;

                        accessKeyId = parameters.get("accessKey");
                        secretKey = parameters.get("secretKey");
                        endpoint = parameters.get("endPoint");
                        productDes = parameters.get("productDescription");
                        instanceType = parameters.get("productDescription");
                        period = Integer.valueOf(parameters.get("samplingTime")) * 1000;
                        samplingProb = Double.valueOf(parameters.get("samplingProbability"));

                        SpotInstance spotInstance = new SpotInstance();
                        spotInstance.productDes = new ArrayList<String>();
                        spotInstance.instanceType = new ArrayList<String>();

                        spotInstance.endpoint = endpoint;
                        spotInstance.productDes.add(productDes);
                        spotInstance.instanceType.add(instanceType);

                        spotInstanceVec.add(spotInstance);
                    }
                } catch (NumberFormatException | ConfigurationException e) {
                    e.printStackTrace();
                }
            }

            startTime = System.currentTimeMillis();
        }

        AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey);
        AmazonEC2 ec2 = new AmazonEC2Client(credentials);

        //////

        boolean isSent = false;
        if (Math.random() < this.samplingProb) {
            isSent = true;
        }

        for (int i = 0; i < spotInstanceVec.size(); i++) {
            ec2.setEndpoint(spotInstanceVec.get(i).endpoint);

            DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest();

            request.setProductDescriptions(spotInstanceVec.get(i).productDes);
            request.setInstanceTypes(spotInstanceVec.get(i).instanceType);
            //request.setStartTime(startTime);
            //request.setMaxResults(maxResult);

            String nextToken = "";
            //do {
            request.withNextToken(nextToken);

            DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory(request);

            List<String> splitString = Arrays.asList(result.getSpotPriceHistory().get(0).toString().split(","));

            if (isSent) {
                for (String temp : splitString) {
                    if (temp.contains("SpotPrice")) {
                        temp = temp.replace("SpotPrice: ", "");
                        System.out.println(temp);
                        try {
                            logger.info("Sending datum: {} {} {}", temp, "SpotPrice", monitoredTarget);
                            dcAgent.send(
                                    new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId()),
                                    "SpotPrice", temp);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            //for (int j = 0; j < result.getSpotPriceHistory().size(); j++) {
            //System.out.println(result.getSpotPriceHistory().get(0));
            //break;
            //}

            //result = result.withNextToken(result.getNextToken());
            //nextToken = result.getNextToken();
            //} while(!nextToken.isEmpty());
        }
        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }

    }
}

From source file:org.excalibur.service.aws.ec2.EC2.java

License:Open Source License

public List<SpotPriceHistory> getSpotPriceHistory(SpotPriceHistoryRequest request) {
    List<SpotPriceHistory> history = new ArrayList<SpotPriceHistory>();

    DescribeSpotPriceHistoryRequest spotPriceRequest = new DescribeSpotPriceHistoryRequest()
            .withEndTime(request.getUntil()).withStartTime(request.getFrom())
            .withAvailabilityZone(request.getRegion() != null ? request.getRegion().getName() : null)
            .withMaxResults(request.getMaxResult()).withProductDescriptions(request.getImageTypes());

    List<InstanceType> types = request.getInstanceTypes();
    String[] instanceTypes = new String[types.size()];

    for (int i = 0; i < instanceTypes.length; i++) {
        instanceTypes[i] = types.get(i).getName();
    }/*from www. j  a  v  a 2 s .  c  o  m*/

    spotPriceRequest.withInstanceTypes(instanceTypes);

    DescribeSpotPriceHistoryResult describeSpotPriceHistory = ec2_.describeSpotPriceHistory(spotPriceRequest);

    for (SpotPrice spot : describeSpotPriceHistory.getSpotPriceHistory()) {
        history.add(new SpotPriceHistory().withInstanceType(new InstanceType().setName(spot.getInstanceType()))
                .withPrice(spot.getSpotPrice()).withImageTypeDescription(spot.getProductDescription())
                .withRegion(new Region(spot.getAvailabilityZone())).withTime(spot.getTimestamp()));
    }

    return history;
}

From source file:org.finra.dm.dao.impl.Ec2DaoImpl.java

License:Apache License

/**
 * This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
 * then filters the returned list to only contain the latest spot price for each instance type.
 *///from   w ww .j a  va  2  s .c o  m
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes,
        AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
    describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
    describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
    DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations
            .describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
    List<SpotPrice> spotPrices = new ArrayList<>();
    Set<String> instanceTypesFound = new HashSet<>();
    for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory()) {
        if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType())) {
            spotPrices.add(spotPriceHistoryEntry);
        }
    }
    return spotPrices;
}

From source file:org.finra.herd.dao.impl.Ec2DaoImpl.java

License:Apache License

/**
 * This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
 * then filters the returned list to only contain the latest spot price for each instance type.
 *//*from  ww w .ja  va  2s  . co m*/
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes,
        Collection<String> productDescriptions, AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
    describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
    describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
    describeSpotPriceHistoryRequest.setProductDescriptions(productDescriptions);
    DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations
            .describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
    List<SpotPrice> spotPrices = new ArrayList<>();
    Set<String> instanceTypesFound = new HashSet<>();
    for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory()) {
        if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType())) {
            spotPrices.add(spotPriceHistoryEntry);
        }
    }
    return spotPrices;
}