Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import decimal
- # Price for each flavor. These are equivalent to hashmap field mappings.
- flavors_id_price = {
- '251f07b7-e40c-4d7c-b025-20cc3036be7a': decimal.Decimal(0.65),
- '8af61796-e4be-4b97-9ded-37a047b35e7c': decimal.Decimal(0.35),
- 'a6ec9bb0-4be8-4651-84ff-df745aa9525e': decimal.Decimal(1.67),
- 'c565b92c-cceb-49d9-971c-9ec4d53c7ec4': decimal.Decimal(1.1),
- 'dbe2de22-305d-4831-92fe-95d801aa5f89': decimal.Decimal(1.2),
- 'f3f987e2-044c-47bb-a20a-c9712e3ecb9e': decimal.Decimal(1.3)
- }
- # Price per MB / GB for images and volumes. These are equivalent to
- # hashmap service mappings.
- image_mb_price = decimal.Decimal(0.002)
- volume_gb_price = decimal.Decimal(0.35)
- # These functions return the price of a service usage on a collect period.
- # The price is always equivalent to the price per unit multiplied by
- # the quantity.
- def get_compute_price(item):
- if not item['desc']['flavor_id'] in flavors_id_price:
- return 0
- elif item['desc']['cpu'] > 0:
- return (decimal.Decimal(flavors_id_price[item['desc']['flavor_id']]))
- """
- def get_image_price(item):
- if not item['vol']['qty']:
- return 0
- else:
- return decimal.Decimal(item['vol']['qty']) * image_mb_price
- """
- def get_volume_price(item):
- if not item['desc']['disk.root.size']:
- return 0
- else:
- return (decimal.Decimal(item['desc']['disk.root.size']) * volume_gb_price)
- # Mapping each service to its price calculation function
- services = {
- 'compute': get_compute_price,
- 'volume': get_volume_price
- # 'image': get_image_price
- }
- def process(data):
- # The 'data' parameter is a list of dictionaries containing a
- # "usage" and a "period" field
- for d in data:
- usage = d['usage']
- for service_name, service_data in usage.items():
- # Do not calculate the price if the service has no
- # price calculation function
- if service_name in services.keys():
- # A service can have several items. For example,
- # each running instance is an item of the compute service
- for item in service_data:
- item['rating'] = {'price': services[service_name](item)}
- return data
- # 'data' is passed as a global variable. The script is supposed to set the
- # 'rating' element of each item in each service
- data = process(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement