Advertisement
paltaa

ck

Feb 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import decimal
  2.  
  3.  
  4. # Price for each flavor. These are equivalent to hashmap field mappings.
  5. flavors_id_price = {
  6. '251f07b7-e40c-4d7c-b025-20cc3036be7a': decimal.Decimal(0.65),
  7. '8af61796-e4be-4b97-9ded-37a047b35e7c': decimal.Decimal(0.35),
  8. 'a6ec9bb0-4be8-4651-84ff-df745aa9525e': decimal.Decimal(1.67),
  9. 'c565b92c-cceb-49d9-971c-9ec4d53c7ec4': decimal.Decimal(1.1),
  10. 'dbe2de22-305d-4831-92fe-95d801aa5f89': decimal.Decimal(1.2),
  11. 'f3f987e2-044c-47bb-a20a-c9712e3ecb9e': decimal.Decimal(1.3)
  12. }
  13.  
  14. # Price per MB / GB for images and volumes. These are equivalent to
  15. # hashmap service mappings.
  16. image_mb_price = decimal.Decimal(0.002)
  17. volume_gb_price = decimal.Decimal(0.35)
  18.  
  19. # These functions return the price of a service usage on a collect period.
  20. # The price is always equivalent to the price per unit multiplied by
  21. # the quantity.
  22.  
  23.  
  24. def get_compute_price(item):
  25. if not item['desc']['flavor_id'] in flavors_id_price:
  26. return 0
  27. elif item['desc']['cpu'] > 0:
  28. return (decimal.Decimal(flavors_id_price[item['desc']['flavor_id']]))
  29.  
  30.  
  31. """
  32. def get_image_price(item):
  33. if not item['vol']['qty']:
  34. return 0
  35. else:
  36. return decimal.Decimal(item['vol']['qty']) * image_mb_price
  37.  
  38. """
  39.  
  40.  
  41. def get_volume_price(item):
  42. if not item['desc']['disk.root.size']:
  43. return 0
  44. else:
  45. return (decimal.Decimal(item['desc']['disk.root.size']) * volume_gb_price)
  46.  
  47.  
  48. # Mapping each service to its price calculation function
  49. services = {
  50. 'compute': get_compute_price,
  51. 'volume': get_volume_price
  52. # 'image': get_image_price
  53. }
  54.  
  55.  
  56. def process(data):
  57. # The 'data' parameter is a list of dictionaries containing a
  58. # "usage" and a "period" field
  59. for d in data:
  60. usage = d['usage']
  61. for service_name, service_data in usage.items():
  62. # Do not calculate the price if the service has no
  63. # price calculation function
  64. if service_name in services.keys():
  65. # A service can have several items. For example,
  66. # each running instance is an item of the compute service
  67. for item in service_data:
  68. item['rating'] = {'price': services[service_name](item)}
  69. return data
  70.  
  71.  
  72. # 'data' is passed as a global variable. The script is supposed to set the
  73. # 'rating' element of each item in each service
  74. data = process(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement