Created
April 28, 2021 19:17
-
-
Save mssrinivas/f78a8d68c795e2bd291598cd86b36461 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import defaultdict | |
storageDict = {} | |
output = {} | |
globalMinimum = float('inf') | |
elements = [] | |
k=0 | |
class Solution(object): | |
def combinationSum(self, candidates, target, totalMemory, computeDict): | |
ret = [] | |
self.dfs(candidates, target, [], ret, totalMemory, computeDict) | |
return ret | |
def dfs(self, candidates, target, combination, ret, totalMemory, computeDict): | |
if target < 0: | |
return | |
if target == 0: | |
if Solution().memoryReq(combination, totalMemory, computeDict) == True: | |
Solution().calculatePricing(combination, computeDict) | |
ret.append(combination) | |
return | |
for i in range(len(candidates)): | |
self.dfs(candidates[i:], target-candidates[i], combination+[candidates[i]], ret, totalMemory, computeDict) | |
def memoryReq(self, combination, totalMemory, computeDict): | |
compute_total_memory = 0 | |
for i in range(len(combination)): | |
for key, value in computeDict.items(): | |
k = key.split(":") | |
if int(k[0]) == combination[i]: | |
compute_total_memory = compute_total_memory + int(k[1]) | |
if compute_total_memory < totalMemory: | |
return False | |
return True | |
def calculatePricing(self, combination, computeDict): | |
aws_cost = Solution().providerPricing(combination, computeDict, "AWS") | |
azure_cost = Solution().providerPricing(combination, computeDict, "Azure") | |
gcp_cost = Solution().providerPricing(combination, computeDict, "GCP") | |
p = "" | |
for ele in combination: | |
p += str(ele) | |
output[p].append(aws_cost) | |
output[p].append(azure_cost) | |
output[p].append(gcp_cost) | |
def providerPricing(self, combination, computeDict, provider): | |
price = 0.0 | |
for i in range(len(combination)): | |
for key, value in computeDict.items(): | |
k = key.split(":") | |
if int(k[0]) == combination[i]: | |
for m in range(len(value)): | |
if value[m][0] == provider : | |
price = price + value[m][1] | |
return price | |
def bestPrice(self, output, index): | |
minimum = float('inf') | |
path = "" | |
li = [] | |
for key, value in output.items(): | |
if value[index] < minimum : | |
minimum = value[index] | |
path = key | |
li.append(path) | |
li.append(str(minimum)) | |
return li | |
def decoupleConfig(self, config): | |
c = "" | |
for ch in config: | |
if ch == '1': | |
c += ch+":4," | |
elif ch == '2': | |
c += ch+":8," | |
elif ch == '4': | |
c += ch+":8," | |
elif ch == '8': | |
c += ch+":32," | |
elif ch == '16': | |
c += ch+":64," | |
elif ch == '32': | |
c += ch+":128," | |
elif ch == '48': | |
c += ch+":192," | |
elif ch == '64': | |
c += ch+":256," | |
return c | |
if __name__ == "__main__": | |
computeDict = defaultdict(list) | |
output = defaultdict(list) | |
vCPU = [1, 2, 4, 8, 16, 32, 48, 64] | |
with open('Pricing.json') as json_file: | |
data = json.load(json_file) | |
for i in range(len(data)): | |
if data[i]['Type'] == "Storage": | |
storageDict[data[i]['Provider']] = data[i]['Pricing in USD (monthly)'] | |
if data[i]['Type'] == "Compute": | |
elements.append([]) | |
elements[k].append(data[i]['Provider']) | |
elements[k].append(data[i]['Pricing in USD (monthly)']) | |
computeDict[data[i]['Configuration']].append(elements[k]) | |
k=k+1 | |
targetvCPU = 57 | |
totalMemory = 400 | |
storage = 4096 | |
multiprovider = True | |
provider = "" | |
azure_storage_cost = (storage)/(1024)*storageDict["Azure"] | |
aws_storage_cost = (storage)/(1024)*storageDict["AWS"] | |
gcp_storage_cost = (storage)/(1024)*storageDict["GCP"] | |
while True: | |
result = Solution().combinationSum(vCPU, targetvCPU, totalMemory, computeDict) | |
if len(result) > 0: | |
break | |
targetvCPU += 1 | |
bestAWSPrice = Solution().bestPrice(output, 0) | |
bestAzurePrice = Solution().bestPrice(output, 1) | |
bestGCPPrice = Solution().bestPrice(output, 2) | |
p = "" | |
for ele in result[len(result)-1]: | |
p += str(ele) | |
bestFitPricing = output[p] | |
bestFitAWS = bestFitPricing[0] | |
bestFitAzure = bestFitPricing[1] | |
bestFitGCP = bestFitPricing[2] | |
best = min(float(bestAWSPrice[1]),float(bestAzurePrice[1]),float(bestGCPPrice[1])) | |
res=json.dumps( | |
{ | |
"Compute" : { | |
"AWS": | |
{ | |
"config" : Solution().decoupleConfig(bestAWSPrice[0]), | |
"bestprice" : float(bestAWSPrice[1]), | |
"bestfitprice": float(bestFitAWS), | |
"bestfitconfig": Solution().decoupleConfig(p), | |
"best" : float(best), | |
"difference": float(bestAWSPrice[1])-float(best) | |
}, | |
"Azure": { | |
"config" : Solution().decoupleConfig(bestAzurePrice[0]), | |
"bestprice" : bestAzurePrice[1], | |
"bestfitprice": float(bestFitAzure), | |
"bestfitconfig": Solution().decoupleConfig(p), | |
"best" : float(best), | |
"difference": float(bestAzurePrice[1])-float(best) | |
}, | |
"GCP": { | |
"config" : Solution().decoupleConfig(bestGCPPrice[0]), | |
"bestprice" : bestGCPPrice[1], | |
"bestfitprice": float(bestFitGCP), | |
"bestfitconfig": Solution().decoupleConfig(p), | |
"best" : float(best), | |
"difference": float(bestGCPPrice[1])-float(best) | |
}, | |
}, | |
"Storage": { | |
"AWS": | |
{ | |
"bestprice": aws_storage_cost, | |
"overallbestprice": azure_storage_cost, | |
"bestprovider": "Azure", | |
"difference": aws_storage_cost-azure_storage_cost | |
}, | |
"Azure": | |
{ | |
"bestprice": azure_storage_cost, | |
"overallbestprice": azure_storage_cost, | |
"bestprovider": "Azure", | |
"difference": azure_storage_cost-azure_storage_cost | |
}, | |
"GCP": | |
{ | |
"bestprice": gcp_storage_cost, | |
"overallbestprice": azure_storage_cost, | |
"bestprovider": "Azure", | |
"difference": gcp_storage_cost-azure_storage_cost | |
}, | |
} | |
} | |
) | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment