-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot_life_time.py
executable file
·95 lines (73 loc) · 2.11 KB
/
spot_life_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
import sys
from dateutil import parser
from datetime import datetime, timedelta
import json
def getmins(start_date, td):
timediff = td - start_date
min_tuple = divmod(timediff.days * 86400 + timediff.seconds, 60)
mins = min_tuple[0]
if min_tuple[1] > 0:
mins = mins + min_tuple[1]/60
return mins
in_prefix = sys.argv[1]
price_file = sys.argv[2]
#spot-c4-8xlarge.txt
#0.616, 2, 3, 4, 0.256, 0.338, 0.402, 0.884, 1, 5
#0.256, 0.338, 0.402, 0.616, 0.884, 2, 3, 4, 5
prices = []
timestamps = []
bid_list = []
with open(price_file) as infile:
lines = infile.readlines()
for line in lines:
bid_list.append(float(line.strip()))
mainf = "spot-"+in_prefix+".txt"
#"sample.txt" #
with open(mainf) as infile:
data = json.load(infile)
datalist = data["SpotPriceHistory"]
records = {}
#raise Exception(list(reversed(datalist)))
for record in datalist:
mytime = record["Timestamp"].replace('T', ' ')
mytime = mytime.replace('Z', '')
mytime = mytime.split("2016-")[1]
mytime = mytime.split(".000")[0]
timestamps.append(mytime)
prices.append(float(record["SpotPrice"]))
timestamps = list(reversed(timestamps))
prices = list(reversed(prices))
life_time = []
start_date = None
skip_date = ""
#i = len(prices)-1
avg_list = []
for bid in bid_list:
for i in range(0, len(prices)):
time, price = timestamps[i], prices[i]
#print timestamps[i], prices[i]
td = parser.parse(time)
if skip_date == str(td).split(" ")[0]:
#print "here"
continue
if start_date == None:
start_date = td
#print start_date
if start_date.day != td.day:
#print start_date.day, td.day
mins = getmins(start_date, td)
#life_time.append(str((str(mins), str(start_date))))
life_time.append(mins)
start_date = td
if price > bid or i+1 == len(prices):
mins = getmins(start_date, td)
#life_time.append(str((str(mins), str(start_date))))
life_time.append(mins)
skip_date = str(start_date).split(" ")[0]
start_date = None
sumn, avg = sum(life_time), 0
avg = sumn/len(life_time)
avg_list.append(str(avg))
with open(in_prefix+"_avg_lt", 'w') as outfile:
outfile.write("\n".join(avg_list))