Last active
December 19, 2015 11:29
-
-
Save logical-and/5947974 to your computer and use it in GitHub Desktop.
2nd task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8"> | |
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
console.log((function (servers, neededServers, neededClusters, performanceDegradation) { | |
var defaultServerStringSeparator = "\n", | |
defaultPerformanceDegradation = performanceDegradation || .3; | |
function fetchServersString(servers, separator) { | |
if (! separator) separator = defaultServerStringSeparator; | |
return servers | |
.split(separator) | |
.filter(function (v) { return ! ! v; }) | |
.map(function (v) { | |
var split = v.split(';'); | |
return { | |
address : split[0], | |
distance : parseFloat(split[1]), | |
performance: parseFloat(split[2]) | |
}; | |
}); | |
} | |
function serversPerformance(servers, degradation) { | |
if (undefined === degradation) degradation = defaultPerformanceDegradation; | |
var performance = 0, distance = 0; | |
servers.each(function (server, i) { | |
performance += server.performance; | |
if (i) distance += server.distance; // don't calculate first element | |
}); | |
// Fix a performance with performance degradation | |
performance -= distance * degradation; | |
return performance; | |
} | |
// Workhorse | |
function getClusters(serversInClusters, clusterCount) { | |
var clusters = [], | |
actualServers = Array.clone(servers); // to prevent modify of original servers array | |
for (var c = 0; c < clusterCount; c ++) { | |
var cluster = []; | |
for (var s = 0; s < serversInClusters; s ++) { | |
if (! actualServers.length) throw new Error('No servers left, needed servers (in each cluster): ' + | |
serversInClusters + ', clusters: ' + clusterCount + ', builded clusters: ', clusters); | |
cluster.push(actualServers[0]); | |
actualServers.shift(); // remove first [0] element | |
} | |
clusters.push(cluster); | |
} | |
return clusters; | |
} | |
function sortClustersByPerformance(clusters) { | |
if (clusters.length < 2) return clusters; // we don't need any type of sorting on this | |
// Sort it as required | |
return clusters.sort(function (cluster1, cluster2) { | |
return serversPerformance(cluster2) - serversPerformance(cluster1); | |
}); | |
} | |
function clustersToString(clusters) { | |
var str = []; | |
clusters.each(function (cluster) { | |
var clstr = []; | |
cluster.each(function (server) { | |
clstr.push([server.address].join(';')); | |
}); | |
str.push(clstr.join(';') + ' (мощность кластера: ' + serversPerformance(cluster) + ')'); | |
}); | |
return str.join(defaultServerStringSeparator); | |
} | |
servers = fetchServersString(servers); | |
return clustersToString(sortClustersByPerformance(getClusters(neededServers, neededClusters))) | |
})( | |
"192.168.0.1;0;3\n" + | |
"192.168.0.2;0.5;5\n" + | |
"192.168.0.3;10;5\n" + | |
"192.168.0.4;10;3\n", | |
2, | |
2 | |
)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment