Skip to content

Instantly share code, notes, and snippets.

@zehome
Last active December 20, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zehome/6122038 to your computer and use it in GitHub Desktop.
Save zehome/6122038 to your computer and use it in GitHub Desktop.
use simple hash function to avoid re-sending all targets as alias
diff --git a/jquery.tswidget.js b/jquery.tswidget.js
index b30e77a..f300745 100755
--- a/jquery.tswidget.js
+++ b/jquery.tswidget.js
@@ -4,6 +4,9 @@ function strip_ending_slash(str) {
}
return str;
}
+function hash_target(target) {
+ return target.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a;},0);
+}
function build_graphite_options(options, raw) {
raw = raw || false;
@@ -36,7 +39,7 @@ function build_graphite_options(options, raw) {
}
if (key === "targets") {
$.each(value, function (index, value) {
- if (raw) {
+ if (raw) {
// it's normally pointless to use alias() in raw mode, because we apply an alias (name) ourself
// in the client rendering step. we just need graphite to return the target.
// but graphite sometimes alters the name of the target in the returned data
@@ -44,10 +47,10 @@ function build_graphite_options(options, raw) {
// so we need a good string identifier and set it using alias() (which graphite will honor)
// so that we recognize the returned output. simplest is just to include the target spec again
// though this duplicates a lot of info in the url.
- clean_options.push("target=alias(" + encodeURIComponent(value.target) + ",'" + encodeURIComponent(value.target) +"')");
- } else {
- clean_options.push("target=alias(color(" +encodeURIComponent(value.target + ",'" + value.color) +"'),'" + encodeURIComponent(value.name) +"')");
- }
+ clean_options.push("target=alias(" + encodeURIComponent(value.target) + ",'" + hash_target(value.target) +"')");
+ } else {
+ clean_options.push("target=alias(color(" +encodeURIComponent(value.target + ",'" + value.color) +"'),'" + encodeURIComponent(value.name) +"')");
+ }
});
} else if (value !== null) {
clean_options.push(key + "=" + encodeURIComponent(value));
@@ -83,7 +86,7 @@ function find_definition (target_graphite, options) {
var matching_i = undefined;
for (var cfg_i = 0; cfg_i < options.targets.length && matching_i == undefined; cfg_i++) {
// string match (no globbing)
- if(options.targets[cfg_i].target == target_graphite.target) {
+ if(hash_target(options.targets[cfg_i].target) == target_graphite.target) {
matching_i = cfg_i;
}
// glob match?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment