Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored pricecalculator to awspricecalculator in preparation for p…
…ackaging this code. Also, added setup.py for the same reason.
- Loading branch information
1 parent
a787e37
commit 232a3fd
Showing
28 changed files
with
391 additions
and
74 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#files that will not be committed | ||
*.pyc | ||
*.csv | ||
*.zip | ||
bin/* | ||
lib/* | ||
.idea/* | ||
include/* | ||
.serverless/* | ||
.Python | ||
pip-selfcheck.json | ||
|
||
|
||
#temporarily out | ||
awspricecalculator/s3* | ||
awspricecalculator/common/utils.py | ||
scripts/ec2-pricing.py | ||
scripts/rds-pricing.py | ||
scripts/s3-pricing.py | ||
scripts/lambda-pricing.py | ||
scripts/context.py | ||
|
||
|
||
test/events/constant-lambda-function-local.json | ||
test/events/constant-tag-local.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
recursive-include awspricecalculator/data *.csv *.json | ||
|
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import json | ||
import consts, models | ||
import datetime | ||
|
||
from ..ec2 import pricing as ec2pricing | ||
from ..s3 import pricing as s3pricing | ||
from ..rds import pricing as rdspricing | ||
from ..awslambda import pricing as lambdapricing | ||
|
||
|
||
|
||
#Creates a table with all the SKUs that are part of the total price | ||
def buildSkuTable(evaluated_sku_desc): | ||
result = {} | ||
sorted_descriptions = sorted(evaluated_sku_desc) | ||
result_table_header = "Price | Description | Price Per Unit | Usage | Rate Code" | ||
result_records = "" | ||
total = 0 | ||
for s in sorted_descriptions: | ||
result_records = result_records + "$" + str(s[0]) + "|" + str(s[1]) + "|" + str(s[2]) + "|" + str(s[3]) + "|" + s[4]+"\n" | ||
total = total + s[0] | ||
|
||
result['header']=result_table_header | ||
result['records']=result_records | ||
result['total']=total | ||
return result | ||
|
||
|
||
|
||
#It calculates price based on a variable price dimension. For example: by region, os, instance type, etc. | ||
def compare(**kwargs): | ||
service = kwargs['service'] | ||
sortCriteria = kwargs['sortCriteria'] | ||
result = [] | ||
cheapest_price = 0 | ||
criteria_array = () | ||
kwargs_key = "" | ||
|
||
#Sort by AWS Region - Total Cost and To-region (for sorting by destination - find which regions are cheaper for backups) | ||
if sortCriteria in [consts.SORT_CRITERIA_REGION, consts.SORT_CRITERIA_TO_REGION]: | ||
tableCriteriaHeader = "Sorted by total cost by region\nRegion code\tRegion name\t" | ||
if sortCriteria == consts.SORT_CRITERIA_TO_REGION: | ||
tableCriteriaHeader = "Sorted by data transfer destination from region ["+kwargs['region']+"] to other regions\nTo-Region code\tTo-Region name\t" | ||
|
||
for r in consts.SUPPORTED_REGIONS: | ||
if sortCriteria == consts.SORT_CRITERIA_TO_REGION: | ||
kwargs['toRegion']=r | ||
else: | ||
kwargs['region']=r | ||
if service == consts.SERVICE_EC2: | ||
p = ec2pricing.calculate(models.Ec2PriceDimension(**kwargs)) | ||
if service == consts.SERVICE_S3: | ||
p = s3pricing.calculate(models.S3PriceDimension(**kwargs)) | ||
if service == consts.SERVICE_RDS: | ||
p = rdspricing.calculate(models.RdsPriceDimension(**kwargs)) | ||
if service == consts.SERVICE_LAMBDA: | ||
p = lambdapricing.calculate(models.LambdaPriceDimension(**kwargs)) | ||
|
||
print (json.dumps(p, indent=True)) | ||
#Only append records for those combinations that exist in the PriceList API | ||
if p['pricingRecords']: result.append((p['totalCost'],r)) | ||
|
||
|
||
#Sort by EC2 Operating System | ||
if sortCriteria == consts.SORT_CRITERIA_OS: | ||
tableCriteriaHeader = "Sorted by total cost by Operating System in region ["+kwargs['region']+"]\nOS\t" | ||
for o in consts.SUPPORTED_EC2_OPERATING_SYSTEMS: | ||
kwargs['operatingSystem']=o | ||
if service == consts.SERVICE_EC2: | ||
p = ec2pricing.calculate(models.Ec2PriceDimension(**kwargs)) | ||
|
||
result.append((p['totalCost'],o)) | ||
|
||
|
||
#Sort by Lambda memory | ||
if sortCriteria == consts.SORT_CRITERIA_LAMBDA_MEMORY: | ||
tableCriteriaHeader = "Sorted by total Allocated Memory in region ["+kwargs['region']+"]\nMemory\t" | ||
for m in consts.LAMBDA_MEM_SIZES: | ||
kwargs['memoryMb']=m | ||
p = lambdapricing.calculate(models.LambdaPriceDimension(**kwargs)) | ||
if p['pricingRecords']: result.append((p['totalCost'],m)) | ||
|
||
|
||
#Sort by S3 Storage Class | ||
if sortCriteria == consts.SORT_CRITERIA_S3_STORAGE_CLASS: | ||
#TODO: Use criteria_array for all sort calculations | ||
tableCriteriaHeader = "Sorted by S3 Storage Class in region ["+kwargs['region']+"]\nStorage Class\t" | ||
criteria_array = consts.SUPPORTED_S3_STORAGE_CLASSES | ||
for c in criteria_array: | ||
kwargs['storageClass']=c | ||
p = s3pricing.calculate(models.S3PriceDimension(**kwargs)) | ||
if p['pricingRecords']: result.append((p['totalCost'],c)) | ||
|
||
|
||
sorted_result = sorted(result) | ||
print ("sorted_result: {}".format(sorted_result)) | ||
if sorted_result: | ||
cheapest_price = sorted_result[0][0] | ||
result = [] | ||
i = 0 | ||
#TODO: use a structured object (Class or dict) instead of using indexes for each field in the table | ||
for r in sorted_result: | ||
#Calculate the current record relative to the last record | ||
delta_last = 0 | ||
pct_to_last = 0 | ||
pct_to_cheapest = 0 | ||
#TODO:handle cases where cheapest is 0 | ||
if i >= 1: | ||
delta_last = sorted_result[i][0]-sorted_result[i-1][0] | ||
if sorted_result[i-1][0] > 0: | ||
pct_to_last = ((sorted_result[i][0]-sorted_result[i-1][0])/sorted_result[i-1][0])*100 | ||
if cheapest_price > 0: | ||
pct_to_cheapest = ((r[0]-cheapest_price)/cheapest_price)*100 | ||
|
||
result.append((r[0], r[1],pct_to_cheapest, pct_to_last,(r[0]-cheapest_price),delta_last)) | ||
|
||
i = i+1 | ||
|
||
print("Sorted cost table:") | ||
print(tableCriteriaHeader+"Cost(USD)\t% compared to cheapest\t% compared to previous\tdelta cheapest\tdelta previous") | ||
for r in result: | ||
rowCriteriaValues = "" | ||
if sortCriteria in [consts.SORT_CRITERIA_REGION, consts.SORT_CRITERIA_TO_REGION]: | ||
rowCriteriaValues = r[1]+"\t"+consts.REGION_MAP[r[1]]+"\t" | ||
if sortCriteria in [consts.SORT_CRITERIA_OS, consts.SORT_CRITERIA_LAMBDA_MEMORY, consts.SORT_CRITERIA_S3_STORAGE_CLASS]: | ||
rowCriteriaValues = str(r[1])+"\t" | ||
print(rowCriteriaValues+str(r[0])+"\t"+str(r[2])+"\t"+str(r[3])+"\t"+str(r[4])+"\t"+str(r[5])) | ||
|
||
|
||
|
||
return result | ||
|
||
|
||
def get_index_file_name(service, name, format): | ||
result = '../awspricecalculator/'+service+'/data/'+name+'.'+format | ||
return result | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
4 changes: 2 additions & 2 deletions
4
pricecalculator/data/ec2/index_metadata.json → ...cecalculator/data/ec2/index_metadata.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"OfferCode": "AmazonEC2", | ||
"FormatVersion": "v1.0", | ||
"Version": "20170324211955", | ||
"Publication Date": "2017-03-24T21:19:55Z", | ||
"Version": "20170526181956", | ||
"Publication Date": "2017-05-26T18:19:56Z", | ||
"Disclaimer": "This pricing list is for informational purposes only. All prices are subject to the additional terms included in the pricing pages on http://aws.amazon.com. All Free Tier prices are also subject to the terms included at https://aws.amazon.com/free/" | ||
} |
4 changes: 2 additions & 2 deletions
4
...alculator/data/lambda/index_metadata.json → ...alculator/data/lambda/index_metadata.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"OfferCode": "AWSLambda", | ||
"FormatVersion": "v1.0", | ||
"Version": "20161207235208", | ||
"Publication Date": "2016-12-07T23:52:08Z", | ||
"Version": "20170519011124", | ||
"Publication Date": "2017-05-19T01:11:24Z", | ||
"Disclaimer": "This pricing list is for informational purposes only. All prices are subject to the additional terms included in the pricing pages on http://aws.amazon.com. All Free Tier prices are also subject to the terms included at https://aws.amazon.com/free/" | ||
} |
4 changes: 2 additions & 2 deletions
4
pricecalculator/data/rds/index_metadata.json → ...cecalculator/data/rds/index_metadata.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"OfferCode": "AmazonRDS", | ||
"FormatVersion": "v1.0", | ||
"Version": "20170327042929", | ||
"Publication Date": "2017-03-27T04:29:29Z", | ||
"Version": "20170419200300", | ||
"Publication Date": "2017-04-19T20:03:00Z", | ||
"Disclaimer": "This pricing list is for informational purposes only. All prices are subject to the additional terms included in the pricing pages on http://aws.amazon.com. All Free Tier prices are also subject to the terms included at https://aws.amazon.com/free/" | ||
} |
4 changes: 2 additions & 2 deletions
4
pricecalculator/data/s3/index_metadata.json → ...icecalculator/data/s3/index_metadata.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"OfferCode": "AmazonS3", | ||
"FormatVersion": "v1.0", | ||
"Version": "20170329010048", | ||
"Publication Date": "2017-03-29T01:00:48Z", | ||
"Version": "20170424230114", | ||
"Publication Date": "2017-04-24T23:01:14Z", | ||
"Disclaimer": "This pricing list is for informational purposes only. All prices are subject to the additional terms included in the pricing pages on http://aws.amazon.com. All Free Tier prices are also subject to the terms included at https://aws.amazon.com/free/" | ||
} |
File renamed without changes.
Oops, something went wrong.