示例#1
0
 def get_spot_history(self, instance_type, start=None, end=None, plot=False,
                      plot_server_interface="localhost",
                      plot_launch_browser=True, plot_web_browser=None,
                      plot_shutdown_server=True):
     if start and not utils.is_iso_time(start):
         raise exception.InvalidIsoDate(start)
     if end and not utils.is_iso_time(end):
         raise exception.InvalidIsoDate(end)
     pdesc = "Linux/UNIX"
     hist = self.conn.get_spot_price_history(start_time=start, end_time=end,
                                             instance_type=instance_type,
                                             product_description=pdesc)
     if not hist:
         raise exception.SpotHistoryError(start, end)
     dates = []
     prices = []
     data = []
     for item in hist:
         timestamp = utils.iso_to_javascript_timestamp(item.timestamp)
         price = item.price
         dates.append(timestamp)
         prices.append(price)
         data.append([timestamp, price])
     maximum = max(prices)
     avg = sum(prices) / float(len(prices))
     log.info("Current price: $%.2f" % prices[-1])
     log.info("Max price: $%.2f" % maximum)
     log.info("Average price: $%.2f" % avg)
     if plot:
         xaxisrange = dates[-1] - dates[0]
         xpanrange = [dates[0] - xaxisrange / 2.,
                      dates[-1] + xaxisrange / 2.]
         xzoomrange = [0.1, xpanrange[-1] - xpanrange[0]]
         minimum = min(prices)
         yaxisrange = maximum - minimum
         ypanrange = [minimum - yaxisrange / 2., maximum + yaxisrange / 2.]
         yzoomrange = [0.1, ypanrange[-1] - ypanrange[0]]
         context = dict(instance_type=instance_type,
                        start=start, end=end,
                        time_series_data=str(data),
                        shutdown=plot_shutdown_server,
                        xpanrange=xpanrange, ypanrange=ypanrange,
                        xzoomrange=xzoomrange, yzoomrange=yzoomrange)
         log.info("", extra=dict(__raw__=True))
         log.info("Starting StarCluster Webserver...")
         s = webtools.get_template_server('web', context=context,
                                          interface=plot_server_interface)
         base_url = "http://%s:%s" % s.server_address
         shutdown_url = '/'.join([base_url, 'shutdown'])
         spot_url = "http://%s:%s/spothistory.html" % s.server_address
         log.info("Server address is %s" % base_url)
         log.info("(use CTRL-C or navigate to %s to shutdown server)" %
                  shutdown_url)
         if plot_launch_browser:
             webtools.open_browser(spot_url, plot_web_browser)
         else:
             log.info("Browse to %s to view the spot history plot" %
                      spot_url)
         s.serve_forever()
     return data
示例#2
0
 def get_spot_history(self, instance_type, start=None, end=None, plot=False):
     if not utils.is_iso_time(start):
         raise exception.InvalidIsoDate(start)
     if not utils.is_iso_time(end):
         raise exception.InvalidIsoDate(end)
     hist = self.conn.get_spot_price_history(start_time=start, 
                                     end_time=end,
                                     instance_type=instance_type, 
                                     product_description="Linux/UNIX")
     if not hist:
         raise exception.SpotHistoryError(start,end)
     dates = [ utils.iso_to_datetime_tuple(i.timestamp) for i in hist]
     prices = [ i.price for i in hist ]
     maximum = max(prices)
     avg = sum(prices)/len(prices)
     log.info("Current price: $%.2f" % hist[-1].price)
     log.info("Max price: $%.2f" % maximum)
     log.info("Average price: $%.2f" % avg)
     if plot:
         try:
             import pylab
             pylab.plot_date(pylab.date2num(dates), prices, linestyle='-') 
             pylab.xlabel('date')
             pylab.ylabel('price (cents)')
             pylab.title('%s Price vs Date (%s - %s)' % (instance_type, start, end))
             pylab.grid(True)
             pylab.show()
         except ImportError,e:
             log.error("Error importing pylab:")
             log.error(str(e)) 
             log.error("please check that matplotlib is installed and that:")
             log.error("   $ python -c 'import pylab'")
             log.error("completes without error")
示例#3
0
 def get_spot_history(self, instance_type,
                      start=None, end=None, plot=False):
     if not utils.is_iso_time(start):
         raise exception.InvalidIsoDate(start)
     if not utils.is_iso_time(end):
         raise exception.InvalidIsoDate(end)
     hist = self.conn.get_spot_price_history(start_time=start,
                                     end_time=end,
                                     instance_type=instance_type,
                                     product_description="Linux/UNIX")
     if not hist:
         raise exception.SpotHistoryError(start, end)
     dates = [utils.iso_to_datetime_tuple(i.timestamp) for i in hist]
     prices = [i.price for i in hist]
     maximum = max(prices)
     avg = sum(prices) / len(prices)
     log.info("Current price: $%.2f" % hist[-1].price)
     log.info("Max price: $%.2f" % maximum)
     log.info("Average price: $%.2f" % avg)
     if plot:
         try:
             import pylab
             pylab.plot_date(pylab.date2num(dates), prices, linestyle='-')
             pylab.xlabel('date')
             pylab.ylabel('price (cents)')
             pylab.title('%s Price vs Date (%s - %s)' % (instance_type,
                                                         start, end))
             pylab.grid(True)
             pylab.show()
         except ImportError, e:
             log.error("Error importing pylab:")
             log.error(str(e))
             log.error("please ensure matplotlib is installed and that:")
             log.error("   $ python -c 'import pylab'")
             log.error("completes without error")
示例#4
0
 def _iso_timestamp(self, option, opt_str, value, parser):
     if not utils.is_iso_time(value):
         parser.error("option %s must be an iso8601 formatted timestamp" %
                      opt_str)
     setattr(parser.values, option.dest, value)