-
Notifications
You must be signed in to change notification settings - Fork 10
/
region-optimizer.js
173 lines (149 loc) · 5.21 KB
/
region-optimizer.js
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
/*
Copyright 2021 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const normalizedSuffix = "_normalized";
const cfeAttr = "cfe";
const carbonIntensityAttr = "gCO2_kWh";
const priceAttr = "gce";
const distanceAttr = "distance";
function distance(destination, origin) {
// Thanks https://www.movable-type.co.uk/scripts/latlong.html
const R = 6371e3; // metres
const φ1 = origin.latitude * Math.PI / 180; // φ, λ in radians
const φ2 = destination.latitude * Math.PI / 180;
const Δφ = (destination.latitude - origin.latitude) * Math.PI / 180;
const Δλ = (destination.longitude - origin.longitude) * Math.PI / 180;
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // in metres
}
/**
* Normalizes a certain attribute of the given region map.
* Creates a new attribute "ATTRIBUTE_nornalized".
* @param {Object} map: a map of <region, data>
* @param {String} attribute: the attribute in data to normalize against
*/
function normalizeAttributes(map, attribute) {
let min = Infinity;
let max = -Infinity;
for (const region in map) {
let value = map[region][attribute];
if (value > max) {
max = value;
}
if (value < min) {
min = value;
}
}
for (const region in map) {
if (map[region][attribute] !== undefined) {
map[region][attribute + normalizedSuffix] = (map[region][attribute] - min) / (max - min)
}
}
}
/**
* Filter out any region from the results that isn't in the list of allowed regions
* @param {Set} allowedRegions set of allowed regions
* @param {Array} sortedResults array of results, sorted by score
*/
function keepOnlyAllowedRegionsFromResults(allowedRegions, sortedResults) {
return sortedResults.filter(result => allowedRegions.has(result.region));
}
function rankRegions(regions, inputs) {
let results = [];
let missingData = [];
let latencyData;
normalizeAttributes(regions, cfeAttr);
normalizeAttributes(regions, carbonIntensityAttr);
normalizeAttributes(regions, priceAttr);
// If latency is a criteria and some locations have been specified,
// score each region based on proximity to locations.
if (inputs.weights.latency > 0 && inputs.locations.length > 0) {
latencyData = {};
for (const region in regions) {
let d = 0;
for (const location of inputs.locations) {
d += distance(location, regions[region]);
}
latencyData[region] = { distance: d };
}
normalizeAttributes(latencyData, distanceAttr);
}
for (const region in regions) {
let score = 0;
// price: lower is better
if(inputs.weights.price > 0) {
score += (1 - regions[region]?.[priceAttr + normalizedSuffix]) * inputs.weights.price;
}
// carbon
if(inputs.weights.carbon > 0) {
if (regions[region]?.[cfeAttr] !== undefined) {
// CFE: higher is better
score += regions[region]?.[cfeAttr + normalizedSuffix] * inputs.weights.carbon;
} else {
// Carbon Intensity: Lower is better
score += (1 - regions[region]?.[carbonIntensityAttr + normalizedSuffix]) * inputs.weights.carbon;
}
}
if (inputs.weights.latency > 0 && inputs.locations.length > 0) {
// latency: lower is better
score += (1 - latencyData?.[region]?.[distanceAttr + normalizedSuffix]) * inputs.weights.latency;
}
if (!isNaN(score)) {
results.push({
region: region,
properties: regions[region],
score: score,
});
} else {
missingData.push({
region: region,
})
}
}
const resultSorted = results.sort(function (a, b) {
return b.score - a.score;
});
const resultFilteredSorted = keepOnlyAllowedRegionsFromResults(inputs.allowedRegions, resultSorted);
return {
sorted: resultFilteredSorted,
missingData
};
}
/*
@param regions: {
region: {}
}
@param inputs: {
weights: {
latency: 0.5,
price: 0.5
carbon: 0.5
},
locations: [
{latitude, longitude}
],
allowedRegions: ['us-central1', 'us-east1']
}
@return [{
region: 'us-central1',
name: ''
score: 0.2
}]
*/
async function regionOptimizer(regions, inputs) {
console.log('Optimizing with:', 'Regions:', regions, 'Inputs:', inputs)
return rankRegions(regions, inputs);
}
export { regionOptimizer }