-
Notifications
You must be signed in to change notification settings - Fork 585
/
detail_pages_cache.py
323 lines (266 loc) · 10.8 KB
/
detail_pages_cache.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import mako.template
import mako.lookup
import mako.exceptions
import io
import json
import datetime
import os
import csv
import bisect
import yaml
import re
cache_engine_mapping = {
"Memcached": "Memcached",
"Redis": "Redis",
}
def initial_prices(i, instance_type):
try:
od = i["Pricing"]["us-east-1"]["Redis"]["ondemand"]
except:
# If prices are not available for us-east-1 it means this is a custom instance of some kind
return ["'N/A'", "'N/A'", "'N/A'"]
try:
_1yr = i["Pricing"]["us-east-1"]["Redis"]["_1yr"]["Standard.noUpfront"]
_3yr = i["Pricing"]["us-east-1"]["Redis"]["_3yr"]["Standard.noUpfront"]
except:
# If we can't get a reservation, likely a previous generation
_1yr = "'N/A'"
_3yr = "'N/A'"
return [od, _1yr, _3yr]
def description(id, defaults):
name = id["Amazon"][1]["value"]
family_category = id["Amazon"][2]["value"].lower()
cpus = id["Compute"][0]["value"]
memory = id["Compute"][1]["value"]
bandwidth = id["Networking"][0]["value"]
if (
"low" in bandwidth.lower()
or "moderate" in bandwidth.lower()
or "high" in bandwidth.lower()
):
bandwidth = " and {} network performance".format(bandwidth.lower())
else:
bandwidth.strip("Gigabit")
bandwidth = " and {} Gibps of bandwidth".format(bandwidth.lower())
return "The {} instance is in the {} family and has {} vCPUs, {} GiB of memory{} starting at ${} per hour.".format(
name, family_category, cpus, memory, bandwidth, defaults[0]
)
def unavailable_instances(instance_details, all_regions):
denylist = []
instance_regions = instance_details["Pricing"].keys()
# If there is no price for a region and os, then it is unavailable
for r in all_regions:
if r not in instance_regions:
denylist.append([all_regions[r], r, "All", "*"])
else:
instance_regions_oss = instance_details["Pricing"][r].keys()
for os in cache_engine_mapping.values():
if os not in instance_regions_oss:
denylist.append([all_regions[r], r, os, os])
return denylist
def assemble_the_families(instances):
# Build 2 lists - one where we can lookup what family an instance belongs to
# and another where we can get the family and see what the members are
instance_fam_map = {}
families = {}
variant_families = {}
for i in instances:
name = i["instance_type"]
itype = name.split(".")[1]
suffix = "".join(name.split(".")[2:])
variant = itype[0:2]
if variant not in variant_families:
variant_families[variant] = [[itype, name]]
else:
dupe = 0
for v, _ in variant_families[variant]:
if v == itype:
dupe = 1
if not dupe:
variant_families[variant].append([itype, name])
member = {"name": name, "cpus": int(i["vcpu"]), "memory": float(i["memory"])}
if itype not in instance_fam_map:
instance_fam_map[itype] = [member]
else:
instance_fam_map[itype].append(member)
# The second list, where we will get the family from knowing the instance
families[name] = itype
# Order the families by number of cpus so they display this way on the webpage
for f, ilist in instance_fam_map.items():
ilist.sort(key=lambda x: x["cpus"])
instance_fam_map[f] = ilist
return instance_fam_map, families, variant_families
def prices(pricing):
display_prices = {}
for region, p in pricing.items():
display_prices[region] = {}
for os, _p in p.items():
if os in cache_engine_mapping:
os = cache_engine_mapping[os]
display_prices[region][os] = {}
# Doing a lot of work to deal with prices having up to 6 places
# after the decimal, as well as prices not existing for all regions
# and operating systems.
try:
display_prices[region][os]["ondemand"] = _p["ondemand"]
except KeyError:
display_prices[region][os]["ondemand"] = "N/A"
# In the next 2 blocks, we need to split out the list of 1 year,
# 3 year, upfront, partial, and no upfront RI prices into 2 sets
# of prices: _1yr (all, partial, no) and _3yr (all, partial, no)
# These are then rendered into the 2 bottom pricing dropdowns
try:
reserved = {}
for k, v in _p["reserved"].items():
if "Term1" in k:
key = k[7:]
reserved[key] = v
display_prices[region][os]["_1yr"] = reserved
except KeyError:
display_prices[region][os]["_1yr"] = "N/A"
try:
reserved = {}
for k, v in _p["reserved"].items():
if "Term3" in k:
key = k[7:]
reserved[key] = v
display_prices[region][os]["_3yr"] = reserved
except KeyError:
display_prices[region][os]["_3yr"] = "N/A"
return display_prices
def load_service_attributes():
special_attrs = [
"pricing",
"cache_parameters",
"regions",
]
data_file = "meta/service_attributes_cache.csv"
display_map = {}
with open(data_file, "r") as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
cloud_key = row[0]
if i == 0:
# Skip the header
continue
elif cloud_key in special_attrs:
category = "Coming Soon"
else:
category = row[2]
display_map[cloud_key] = {
"cloud_key": cloud_key,
"display_name": row[1],
"category": category,
"order": row[3],
"style": row[4],
"regex": row[5],
"value": None,
"variant_family": row[1][0:2],
}
return display_map
def format_attribute(display):
if display["regex"]:
# Use a regex extract the value to display
toparse = str(display["value"])
regex = str(display["regex"])
match = re.search(regex, toparse)
if match:
display["value"] = match.group()
# else:
# print("No match found for {} with regex {}".format(toparse, regex))
if display["style"]:
# Make boolean values have fancy CSS
v = str(display["value"]).lower()
if display["cloud_key"] == "currentGeneration" and v == "yes":
display["style"] = "value value-current"
display["value"] = "current"
elif v == "false" or v == "0" or v == "none":
display["style"] = "value value-false"
elif v == "true" or v == "1" or v == "yes":
display["style"] = "value value-true"
elif display["cloud_key"] == "currentGeneration" and v == "no":
display["style"] = "value value-previous"
display["value"] = "previous"
elif display["style"] == "bytes":
display["value"] = round(int(v) / 1048576)
return display
def map_cache_attributes(i, imap):
# Transform keys (instance attributes like vCPUs) and values from instances.json
# into human readable names and nicely formatted values
categories = [
"Compute",
"Networking",
"Storage",
"Amazon",
"Not Shown",
]
# Nested attributes in instances.json that we handle differently
special_attributes = [
"pricing",
"regions",
]
instance_details = {}
for c in categories:
instance_details[c] = []
# For up to date display names, inspect meta/service_attributes_cache.csv
for attr_name, attr_val in i.items():
try:
if attr_name not in special_attributes:
# This is one row on a detail page
display = imap[attr_name]
display["value"] = attr_val
instance_details[display["category"]].append(format_attribute(display))
except KeyError:
print(
"An instances.json attribute {} does not appear in meta/service_attributes_cache.csv and cannot be formatted".format(
attr_name
)
)
# Sort the instance attributes in each category alphabetically,
# another general-purpose option could be to sort by value data type
for c in categories:
instance_details[c].sort(key=lambda x: int(x["order"]))
return instance_details
def build_detail_pages_cache(instances, all_regions):
subdir = os.path.join("www", "aws", "elasticache")
ifam, fam_lookup, variants = assemble_the_families(instances)
imap = load_service_attributes()
lookup = mako.lookup.TemplateLookup(directories=["."])
template = mako.template.Template(
filename="in/instance-type-cache.html.mako", lookup=lookup
)
# To add more data to a single instance page, do so inside this loop
could_not_render = []
sitemap = []
for i in instances:
instance_type = i["instance_type"]
instance_page = os.path.join(subdir, instance_type + ".html")
instance_details = map_cache_attributes(i, imap)
instance_details["Pricing"] = prices(i["pricing"])
fam = fam_lookup[instance_type]
fam_members = ifam[fam]
denylist = unavailable_instances(instance_details, all_regions)
defaults = initial_prices(instance_details, instance_type)
idescription = description(instance_details, defaults)
print("Rendering %s to detail page %s..." % (instance_type, instance_page))
with io.open(instance_page, "w+", encoding="utf-8") as fh:
try:
fh.write(
template.render(
i=instance_details,
family=fam_members,
description=idescription,
unavailable=denylist,
defaults=defaults,
variants=variants[instance_type[6:8]],
regions=all_regions,
)
)
sitemap.append(instance_page)
except:
render_err = mako.exceptions.text_error_template().render()
err = {"e": "ERROR for " + instance_type, "t": render_err}
could_not_render.append(err)
[print(err["e"], "{}".format(err["t"])) for err in could_not_render]
[print(page["e"]) for page in could_not_render]
return sitemap