Skip to content

Commit

Permalink
Fixed provider meter value issue in default product cost allocation (#26
Browse files Browse the repository at this point in the history
)

* Fixed provider meter value issue in default product cost allocation

* Version 1.0.13
  • Loading branch information
marc-perreaut authored May 16, 2024
1 parent 7cf6c94 commit c7441c4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 65 deletions.
20 changes: 12 additions & 8 deletions cloud_cost_allocation/cloud_cost_allocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def allocate(self, consumer_cost_items: list[ConsumerCostItem], cloud_cost_items
cost_items.append(consumer_cost_item)

# Process cloud tag selectors
info("Processing cloud tag selectors, for date" + self.date_str)
info("Processing cloud tag selectors, for date " + self.date_str)
self.process_cloud_tag_selectors(cost_items, cloud_tag_selector_consumer_cost_items, cloud_cost_items)

# Create and add cloud consumer cost item from tags
Expand Down Expand Up @@ -492,12 +492,17 @@ def process_default_products(self,
new_consumer_cost_item = self.cost_item_factory.create_consumer_cost_item()
new_consumer_cost_item.copy(cost_item)
new_consumer_cost_item.provider_cost_allocation_type += "+DefaultProduct"
default_product_allocation_key_ratio =\
default_product_consumer_cost_item.allocation_keys[0] /\
default_product_allocation_keys[cost_item.provider_service]
for provider_meter in new_consumer_cost_item.provider_meters:
if "Value" in provider_meter:
value = float(provider_meter["Value"])
provider_meter["Value"] = str(value * default_product_allocation_key_ratio)
new_consumer_cost_item.product = default_product_consumer_cost_item.product
new_consumer_cost_item.product_dimensions =\
default_product_consumer_cost_item.product_dimensions.copy()
new_consumer_cost_item.allocation_keys[0] *=\
default_product_consumer_cost_item.allocation_keys[0] /\
default_product_allocation_keys[cost_item.provider_service]
new_consumer_cost_item.allocation_keys[0] *= default_product_allocation_key_ratio
new_cost_items.append(new_consumer_cost_item)
else:
new_cost_items.append(cost_item)
Expand All @@ -521,7 +526,6 @@ def process_default_products(self,
new_consumer_cost_item.service = service_instance.service
new_consumer_cost_item.instance = service_instance.instance
new_consumer_cost_item.provider_cost_allocation_type = "DefaultProduct"
new_consumer_cost_item.allocation_keys[0] = 1.0
if service_instance.service in default_product_consumer_cost_items:
for default_product_consumer_cost_item\
in default_product_consumer_cost_items[service_instance.service]:
Expand All @@ -530,12 +534,12 @@ def process_default_products(self,
new_consumer_cost_item_with_product.product = default_product_consumer_cost_item.product
new_consumer_cost_item_with_product.product_dimensions =\
default_product_consumer_cost_item.product_dimensions.copy()
new_consumer_cost_item_with_product.allocation_keys[0] *=\
default_product_consumer_cost_item.allocation_keys[0] /\
default_product_allocation_keys[service_instance.service]
new_consumer_cost_item_with_product.allocation_keys[0] =\
default_product_consumer_cost_item.allocation_keys[0]
new_cost_items.append(new_consumer_cost_item_with_product)
elif default_product:
new_consumer_cost_item.product = default_product
new_consumer_cost_item.allocation_keys[0] = 1.0
new_cost_items.append(new_consumer_cost_item)

# Return
Expand Down
7 changes: 4 additions & 3 deletions cloud_cost_allocation/cost_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from abc import ABC, abstractmethod
from io import StringIO
from logging import debug, info, error
import copy
import re
import sys

Expand Down Expand Up @@ -203,10 +204,10 @@ def copy(self, consumer_cost_item: 'ConsumerCostItem'):
self.allocation_keys = consumer_cost_item.allocation_keys.copy()
self.provider_cost_allocation_cloud_tag_selector =\
consumer_cost_item.provider_cost_allocation_cloud_tag_selector
self.provider_meters = consumer_cost_item.provider_meters.copy()
self.provider_meters = copy.deepcopy(consumer_cost_item.provider_meters)
self.product = consumer_cost_item.product
self.product_dimensions = consumer_cost_item.product_dimensions.copy()
self.product_meters = consumer_cost_item.product_meters.copy()
self.product_dimensions = copy.deepcopy(consumer_cost_item.product_dimensions.copy())
self.product_meters = copy.deepcopy(consumer_cost_item.product_meters)

def get_consumer_cost_item_provider_service_instance(self) -> 'ServiceInstance':
return self.provider_service_instance
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Setup
setup(
name='cloud-cost-allocation',
version='1.0.12',
version='1.0.13',
description='Python library for shared, hierarchical cost allocation based on user-defined usage metrics.',
long_description=readme,
long_description_content_type='text/markdown',
Expand Down
1 change: 1 addition & 0 deletions tests/test8/test8.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[General]
DateFormat = %%Y-%%m-%%d
NumberOfProviderMeters = 2
DefaultProduct = P0
NumberOfProductDimensions = 2

Expand Down
52 changes: 26 additions & 26 deletions tests/test8/test8_allocated_cost.csv
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Date,Service,Instance,Tags,AmortizedCost,OnDemandCost,Currency,ProviderService,ProviderInstance,ProviderTagSelector,ProviderCostAllocationType,ProviderCostAllocationKey,ProviderCostAllocationCloudTagSelector,Product,ProductAmortizedCost,ProductOnDemandCost,ProductDimensionName1,ProductDimensionElement1,ProductDimensionName2,ProductDimensionElement2,Cloud,IsFinalConsumption
2024-01-25,app1,app1,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app1,app1,,128.0,128.0,EUR,app1,app1,,Key,1.0,,p1,128.0,128.0,,,,,,Y
2024-01-25,app2,app2,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app2,app2,,128.0,128.0,EUR,app2,app2,,DefaultProduct,1.0,,p0,128.0,128.0,,,,,,Y
2024-01-25,app3,a3i1,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app3,a3i1,,64.0,64.0,EUR,plf1,plf1,,Key+DefaultProduct,1.0,,p9,64.0,64.0,p9d1,pd9e1,p9d2,p9e2,,N
2024-01-25,app3,a3i1,,192.0,192.0,EUR,app3,a3i1,,Key,1.0,,p3,128.0,128.0,,,,,,Y
2024-01-25,app3,a3i2,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app3,a3i2,,64.0,64.0,EUR,plf1,plf1,,Key+DefaultProduct,1.0,,p9,64.0,64.0,p9d1,pd9e1,p9d2,p9e2,,N
2024-01-25,app4,app4,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app4,app4,,192.0,192.0,EUR,app3,a3i2,,Key,1.0,,,,,,,,,,N
2024-01-25,app4,app4,,160.0,160.0,EUR,app4,app4,,DefaultProduct,0.5,,p4,128.0,128.0,p4d1,pd4e1,p4d2,p4e2,,Y
2024-01-25,app4,app4,,160.0,160.0,EUR,app4,app4,,DefaultProduct,0.5,,p5,128.0,128.0,p5d1,pd5e1,p5d2,p5e2,,Y
2024-01-25,app5,app5,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app5,app5,,64.0,64.0,EUR,plf2,plf2,,Key,1.0,,p2,64.0,64.0,,,,,,N
2024-01-25,app5,app5,,96.0,96.0,EUR,app5,app5,,Key+DefaultProduct,1.0,,p6,64.0,64.0,p6d1,pd6e1,p6d2,p6e2,,Y
2024-01-25,app6,app6,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,app6,app6,,64.0,64.0,EUR,plf2,plf2,,Key,1.0,,p2,64.0,64.0,,,,,,N
2024-01-25,app6,app6,,96.0,96.0,EUR,app5,app5,,Key+DefaultProduct,1.0,,p6,64.0,64.0,p6d1,pd6e1,p6d2,p6e2,,N
2024-01-25,app6,app6,,144.0,144.0,EUR,app6,app6,,Key+DefaultProduct,0.5,,p7,64.0,64.0,p7d1,pd7e1,p7d2,p7e2,,Y
2024-01-25,app6,app6,,144.0,144.0,EUR,app6,app6,,Key+DefaultProduct,0.5,,p8,64.0,64.0,p8d1,pd8e1,p8d2,p8e2,,Y
2024-01-25,container,container,"service:container,cloud_resource_id:/az/virtualmachines/vm1,",1280.0,1280.0,EUR,,,,,,,,,,,,,,az,N
2024-01-25,container,container,,128.0,128.0,EUR,container,container,,Key+DefaultProduct,1.0,,p0,128.0,128.0,,,,,,Y
2024-01-25,plf1,plf1,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
2024-01-25,plf2,plf2,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,,,,,,N
Date,Service,Instance,Tags,AmortizedCost,OnDemandCost,Currency,ProviderService,ProviderInstance,ProviderTagSelector,ProviderCostAllocationType,ProviderCostAllocationKey,ProviderCostAllocationCloudTagSelector,Product,ProductAmortizedCost,ProductOnDemandCost,ProviderMeterName1,ProviderMeterUnit1,ProviderMeterValue1,ProviderMeterName2,ProviderMeterUnit2,ProviderMeterValue2,ProductDimensionName1,ProductDimensionElement1,ProductDimensionName2,ProductDimensionElement2,Cloud,IsFinalConsumption
2024-01-25,app1,app1,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app1,app1,,128.0,128.0,EUR,app1,app1,,Key,1.0,,p1,128.0,128.0,transactions,trx,1000,documents,doc,100,,,,,,Y
2024-01-25,app2,app2,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app2,app2,,128.0,128.0,EUR,app2,app2,,DefaultProduct,1.0,,p0,128.0,128.0,,,,,,,,,,,,Y
2024-01-25,app3,a3i1,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app3,a3i1,,64.0,64.0,EUR,plf1,plf1,,Key+DefaultProduct,1.0,,p9,64.0,64.0,meter1,unit1,10.0,,,,p9d1,pd9e1,p9d2,p9e2,,N
2024-01-25,app3,a3i1,,192.0,192.0,EUR,app3,a3i1,,Key,1.0,,p3,128.0,128.0,transactions,trx,1000,documents,doc,100,,,,,,Y
2024-01-25,app3,a3i2,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app3,a3i2,,64.0,64.0,EUR,plf1,plf1,,Key+DefaultProduct,1.0,,p9,64.0,64.0,meter1,unit2,10.0,,,,p9d1,pd9e1,p9d2,p9e2,,N
2024-01-25,app4,app4,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app4,app4,,192.0,192.0,EUR,app3,a3i2,,Key,1.0,,,,,transactions,trx,1000,documents,doc,100,,,,,,N
2024-01-25,app4,app4,,160.0,160.0,EUR,app4,app4,,DefaultProduct,1.0,,p4,128.0,128.0,,,,,,,p4d1,pd4e1,p4d2,p4e2,,Y
2024-01-25,app4,app4,,160.0,160.0,EUR,app4,app4,,DefaultProduct,1.0,,p5,128.0,128.0,,,,,,,p5d1,pd5e1,p5d2,p5e2,,Y
2024-01-25,app5,app5,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app5,app5,,64.0,64.0,EUR,plf2,plf2,,Key,1.0,,p2,64.0,64.0,meter2,unit2,10,,,,,,,,,N
2024-01-25,app5,app5,,96.0,96.0,EUR,app5,app5,,Key+DefaultProduct,1.0,,p6,64.0,64.0,transactions,trx,1000.0,documents,doc,100.0,p6d1,pd6e1,p6d2,p6e2,,Y
2024-01-25,app6,app6,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,app6,app6,,64.0,64.0,EUR,plf2,plf2,,Key,1.0,,p2,64.0,64.0,meter2,unit2,10,,,,,,,,,N
2024-01-25,app6,app6,,96.0,96.0,EUR,app5,app5,,Key+DefaultProduct,1.0,,p6,64.0,64.0,transactions,trx,1000.0,documents,doc,100.0,p6d1,pd6e1,p6d2,p6e2,,N
2024-01-25,app6,app6,,144.0,144.0,EUR,app6,app6,,Key+DefaultProduct,0.5,,p7,64.0,64.0,transactions,trx,500.0,documents,doc,50.0,p7d1,pd7e1,p7d2,p7e2,,Y
2024-01-25,app6,app6,,144.0,144.0,EUR,app6,app6,,Key+DefaultProduct,0.5,,p8,64.0,64.0,transactions,trx,500.0,documents,doc,50.0,p8d1,pd8e1,p8d2,p8e2,,Y
2024-01-25,container,container,"service:container,cloud_resource_id:/az/virtualmachines/vm1,",1280.0,1280.0,EUR,,,,,,,,,,,,,,,,,,,,az,N
2024-01-25,container,container,,128.0,128.0,EUR,container,container,,Key+DefaultProduct,1.0,,p0,128.0,128.0,cpu,vcpu,1,memory,gb,4,,,,,,Y
2024-01-25,plf1,plf1,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
2024-01-25,plf2,plf2,,128.0,128.0,EUR,container,container,,Key,1.0,,,,,cpu,vcpu,1,memory,gb,4,,,,,,N
54 changes: 27 additions & 27 deletions tests/test8/test8_cost_allocation_keys.csv
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
Date,ProviderService,ProviderInstance,ProviderCostAllocationKey,ConsumerService,ConsumerInstance,Product,ProductDimensionName1,ProductDimensionElement1,ProductDimensionName2,ProductDimensionElement2,ProviderCostAllocationType
2024-01-25,container,container,1,app1,app1,,,,,,Key
2024-01-25,container,container,1,app2,app2,,,,,,Key
2024-01-25,container,container,1,app3,a3i1,,,,,,Key
2024-01-25,container,container,1,app3,a3i2,,,,,,Key
2024-01-25,container,container,1,app4,app4,,,,,,Key
2024-01-25,container,container,1,app5,app5,,,,,,Key
2024-01-25,container,container,1,app6,app6,,,,,,Key
2024-01-25,container,container,1,plf1,plf1,,,,,,Key
2024-01-25,container,container,1,plf2,plf2,,,,,,Key
2024-01-25,container,container,1,container,container,,,,,,Key
2024-01-25,app1,app1,1,app1,app1,p1,,,,,Key
2024-01-25,plf1,plf1,1,app3,a3i1,,,,,,Key
2024-01-25,plf1,plf1,1,app3,a3i2,,,,,,Key
2024-01-25,app3,a3i1,1,app3,a3i1,p3,,,,,Key
2024-01-25,app3,a3i2,1,app4,app4,,,,,,Key
2024-01-25,plf2,plf2,1,app5,app5,p2,,,,,Key
2024-01-25,plf2,plf2,1,app6,app6,p2,,,,,Key
2024-01-25,app5,app5,1,app5,app5,,,,,,Key
2024-01-25,app5,app5,1,app6,app6,,,,,,Key
2024-01-25,app6,app6,1,app6,app6,,,,,,Key
2024-01-25,app4,,1,,,p4,p4d1,pd4e1,p4d2,p4e2,DefaultProduct
2024-01-25,app4,,1,,,p5,p5d1,pd5e1,p5d2,p5e2,DefaultProduct
2024-01-25,app5,,1,,,p6,p6d1,pd6e1,p6d2,p6e2,DefaultProduct
2024-01-25,app6,,1,,,p7,p7d1,pd7e1,p7d2,p7e2,DefaultProduct
2024-01-25,app6,,1,,,p8,p8d1,pd8e1,p8d2,p8e2,DefaultProduct
2024-01-25,plf1,,1,,,p9,p9d1,pd9e1,p9d2,p9e2,DefaultProduct
Date,ProviderService,ProviderInstance,ProviderMeterName1,ProviderMeterUnit1,ProviderMeterValue1,ProviderMeterName2,ProviderMeterUnit2,ProviderMeterValue2,ProviderCostAllocationKey,ConsumerService,ConsumerInstance,Product,ProductDimensionName1,ProductDimensionElement1,ProductDimensionName2,ProductDimensionElement2,ProviderCostAllocationType
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app1,app1,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app2,app2,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app3,a3i1,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app3,a3i2,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app4,app4,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app5,app5,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,app6,app6,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,plf1,plf1,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,plf2,plf2,,,,,,Key
2024-01-25,container,container,cpu,vcpu,1,memory,gb,4,1,container,container,,,,,,Key
2024-01-25,app1,app1,transactions,trx,1000,documents,doc,100,1,app1,app1,p1,,,,,Key
2024-01-25,plf1,plf1,meter1,unit1,10,,,,1,app3,a3i1,,,,,,Key
2024-01-25,plf1,plf1,meter1,unit2,10,,,,1,app3,a3i2,,,,,,Key
2024-01-25,app3,a3i1,transactions,trx,1000,documents,doc,100,1,app3,a3i1,p3,,,,,Key
2024-01-25,app3,a3i2,transactions,trx,1000,documents,doc,100,1,app4,app4,,,,,,Key
2024-01-25,plf2,plf2,meter2,unit2,10,,,,1,app5,app5,p2,,,,,Key
2024-01-25,plf2,plf2,meter2,unit2,10,,,,1,app6,app6,p2,,,,,Key
2024-01-25,app5,app5,transactions,trx,1000,documents,doc,100,1,app5,app5,,,,,,Key
2024-01-25,app5,app5,transactions,trx,1000,documents,doc,100,1,app6,app6,,,,,,Key
2024-01-25,app6,app6,transactions,trx,1000,documents,doc,100,1,app6,app6,,,,,,Key
2024-01-25,app4,,,,,,,,1,,,p4,p4d1,pd4e1,p4d2,p4e2,DefaultProduct
2024-01-25,app4,,,,,,,,1,,,p5,p5d1,pd5e1,p5d2,p5e2,DefaultProduct
2024-01-25,app5,,,,,,,,1,,,p6,p6d1,pd6e1,p6d2,p6e2,DefaultProduct
2024-01-25,app6,,,,,,,,1,,,p7,p7d1,pd7e1,p7d2,p7e2,DefaultProduct
2024-01-25,app6,,,,,,,,1,,,p8,p8d1,pd8e1,p8d2,p8e2,DefaultProduct
2024-01-25,plf1,,,,,,,,1,,,p9,p9d1,pd9e1,p9d2,p9e2,DefaultProduct

0 comments on commit c7441c4

Please sign in to comment.