Ejemplo n.º 1
0
    def test_price_data_too_old(self):
        downloader = SpotPriceDownloader()
        open('fake_file.pkl', 'a').close()

        oldtime = arrow.utcnow().replace(hours=-5).timestamp
        newtime = arrow.utcnow().timestamp

        os.utime('fake_file.pkl',(oldtime,oldtime))
        self.assertTrue(downloader._price_data_is_old(filename='fake_file.pkl'))

        os.utime('fake_file.pkl',(newtime,newtime))
        self.assertFalse(downloader._price_data_is_old(filename='fake_file.pkl'))
Ejemplo n.º 2
0
class SpotPrices:
    def __init__(self):
        self.log = logging.getLogger(__name__)
        self.log.setLevel(logging.INFO)
        self.downloader = SpotPriceDownloader()

    def datapoint_is_too_old(self,timestamp,start_time):
        timestamp = arrow.get(timestamp)
        return start_time.replace(minutes=-60) > timestamp

    def get_cheapest(self, instance_type='c4.xlarge', region = None, filter_products_by=None):
        utcnow = arrow.utcnow()
        prices = self.downloader.get_current_prices(instance_type=instance_type)
        minimum = {'Timestamp': utcnow, 'AvailabilityZone':'fake-region', 'SpotPrice': 999.00}
        for region in prices:
            datapoints = region[1]
            prices_for_instance_type = [datapoint for datapoint in datapoints if not self.datapoint_is_too_old(datapoint['Timestamp'],utcnow)] or None
            if not prices_for_instance_type:
                self.log.warning('no prices in {0} for {1}'.format(region[0],instance_type))
                continue

            if filter_products_by:
                latest_price = [price for price in prices_for_instance_type if filter_products_by in price['ProductDescription']][-1]
            else:
                latest_price = prices_for_instance_type[-1]


            if float(latest_price['SpotPrice']) < minimum['SpotPrice']:
                minimum = {'Timestamp': latest_price['Timestamp'],
                           'AvailabilityZone': latest_price['AvailabilityZone'],
                           'SpotPrice': float(latest_price['SpotPrice']),
                           'InstanceType': instance_type,
                           'ProductDescription': latest_price['ProductDescription']}
                self.log.info('New minimum at',minimum)
            else:
                self.log.info('Minimum in {0} was {1}, not beating current best price of {2}'.format(region[0], latest_price['SpotPrice'], arrow.get(latest_price['Timestamp']).humanize(), minimum['SpotPrice']))
        return minimum

    def pretty_print_price(self, cheapest_object):
        print('Cheapest price was {0} for a {1} running {2} in {3}, at {4} ({5})'.format(cheapest_object['SpotPrice'],
                                                                                         cheapest_object['InstanceType'],
                                                                                         cheapest_object['ProductDescription'] ,
                                                                                         cheapest_object['AvailabilityZone'],
                                                                                         arrow.get(cheapest_object['Timestamp']).to('local'),
                                                                                         arrow.get(cheapest_object['Timestamp']).humanize()))
Ejemplo n.º 3
0
 def test_get_prices(self):
     downloader = SpotPriceDownloader()
     downloader.get_current_prices()
Ejemplo n.º 4
0
 def test_get_regions(self):
     downloader = SpotPriceDownloader()
     regions = downloader._get_regions()
     self.assertTrue(os.path.exists('regions.pkl'))
     self.assertIn('us-west-1',regions)
Ejemplo n.º 5
0
 def __init__(self):
     self.log = logging.getLogger(__name__)
     self.log.setLevel(logging.INFO)
     self.downloader = SpotPriceDownloader()