Skip to content

Instantly share code, notes, and snippets.

@mpobrien
Created June 11, 2014 15:12
Show Gist options
  • Save mpobrien/eeaa4e4bdbe5e975361f to your computer and use it in GitHub Desktop.
Save mpobrien/eeaa4e4bdbe5e975361f to your computer and use it in GitHub Desktop.
from pymongo import MongoClient
import math
import datetime
from collections import namedtuple
import calendar
db = MongoClient()['mci']
NUM_THRESHOLD = 30
start = datetime.datetime(2014, 4, 1, 0, 0, 0, 0)
end = datetime.datetime(2014, 5, 1, 0, 0, 0, 0)
size_pricing = {
"m3.2xlarge" : 1.064,
"m3.xlarge" : 0.532,
"m3.large" : 0.266,
}
TERMINATED = -1
STARTED = 1
hosts = list(db.hosts.find({"started_by":"mci","creation_time":{"$gte":start, "$lt":end}, "distro_id":{"$in":["windows-64-compile", "windows-64-test"]}}, sort=[("creation_time", 1)]))
HostEvent = namedtuple('HostEvent', ['type', 'time', 'size', 'cost'])
all_host_events = []
for host in hosts:
hourly_cost = size_pricing[host['instance_type']]
billed_hours = math.ceil((host['termination_time'] - host['creation_time']).total_seconds() / 3600.)
cost = billed_hours * hourly_cost
started = HostEvent(STARTED, host['creation_time'], host['instance_type'], cost)
terminated = HostEvent(TERMINATED, host['termination_time'], host['instance_type'], 0)
all_host_events.append(started)
all_host_events.append(terminated)
all_host_events.sort(lambda h1,h2: int(math.ceil((h1.time - h2.time).total_seconds())))
cur_max = 0
total_hosts = 0
total_cost_estimate = 0
discounted_cost_estimate = 0
for h in all_host_events:
total_cost_estimate += h.cost
total_hosts += h.type
if total_hosts > cur_max:
cur_max = total_hosts
if total_hosts > NUM_THRESHOLD:
discounted_cost_estimate += h.cost
print total_cost_estimate
print discounted_cost_estimate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment