Skip to content

Instantly share code, notes, and snippets.

@PauloMigAlmeida
Created July 17, 2018 20:16
Show Gist options
  • Save PauloMigAlmeida/ec57f5b7f0ebbe9004be9f367e412297 to your computer and use it in GitHub Desktop.
Save PauloMigAlmeida/ec57f5b7f0ebbe9004be9f367e412297 to your computer and use it in GitHub Desktop.
glacier_basic_calculator.py
# ap-souteast-2 prices only for now
import math
import sys
upload_requests_price_per_thousand = 0.06
storage_price_per_gb = 0.005
standard_retrieval_price_per_gb = 0.012
standard_retrieval_price_per_1000_requests = 0.06
free_tier_retrieve_per_month = 10
hours_in_a_month = 750
# Days of storage must be >= 90, otherwise it incur in pro-rated charge equal to the storage charge for the remaining days
def calculate(gb_storage, number_of_files, retrieval_time_in_hours, debug=True):
# Validation
temp_retrieval_time_in_hours = 0
if number_of_files < 4:
temp_retrieval_time_in_hours = 4
elif retrieval_time_in_hours > number_of_files:
return
else:
temp_retrieval_time_in_hours = retrieval_time_in_hours
# Common
files_per_gb_ratio = math.ceil(number_of_files / gb_storage)
storage_cost = gb_storage * storage_price_per_gb
if debug:
print('storage_cost','USD' , storage_cost)
upload_requests_cost = math.ceil(number_of_files/1000) * upload_requests_price_per_thousand
if debug:
print('upload_requests_cost','USD' , upload_requests_cost)
retrieval_requests_cost = (number_of_files * standard_retrieval_price_per_gb) + ((number_of_files/1000) * standard_retrieval_price_per_1000_requests)
if debug:
print('retrieval_requests_cost','USD' , retrieval_requests_cost)
# Glacier offers a 10 GB retrieval free tier
retrieval_requests_time_cost = (max((gb_storage - (free_tier_retrieve_per_month * (temp_retrieval_time_in_hours/hours_in_a_month))),0) / temp_retrieval_time_in_hours * standard_retrieval_price_per_gb * hours_in_a_month)
if debug:
print('retrieval_requests_time_cost','USD' , retrieval_requests_time_cost)
total_cost = storage_cost + upload_requests_cost + retrieval_requests_cost + retrieval_requests_time_cost
if debug:
print('Total: ','USD' , total_cost)
else:
print('{},{},{}'.format(number_of_files, retrieval_time_in_hours ,total_cost))
if __name__ == "__main__":
if len(sys.argv) == 4 :
# Variables
gb_storage = int(sys.argv[1])
number_of_files = int(sys.argv[2])
retrieval_time_in_hours = int(sys.argv[3])
calculate(gb_storage, number_of_files, retrieval_time_in_hours)
elif len(sys.argv) == 2 :
# Variables
gb_storage = int(sys.argv[1])
for files_n in range(1,10000, 1):
for hours_n in range(4,5000, 10):
calculate(gb_storage, files_n, hours_n, debug=False)
else:
print("Iiiiii kuza1.. errou rude hein")
# Removing duplicates:
# cat /dev/null > glacier_cost.csv | python script.py 559 > glacier_cost.csv && awk '{if (++dup[$0] == 1) print $0;}' glacier_cost.csv > glacier_cost_sorted.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment