glutenfree
Version:
A profiler/loganalyzer for nginx/Cetrea Aw.
236 lines (221 loc) • 8.49 kB
JavaScript
// Generated by CoffeeScript 1.6.3
var ArgumentGenerator, Q, ag, argv, fs, http, id, info, lookupConf, promises, stats, statsFile, url, util, winston, xml2js, _, _fn, _ref;
Q = require("q");
http = require("http");
url = require("url");
fs = require("fs");
xml2js = require("xml2js");
_ = require("underscore");
winston = require("winston");
util = require("util");
ArgumentGenerator = (function() {
function ArgumentGenerator(server, lookupConfiguration, cache, premapped, mappers, targeting) {
var file, _i, _j, _len, _len1, _ref, _ref1,
_this = this;
this.server = server;
this.lookupConfiguration = lookupConfiguration;
this.cache = cache != null ? cache : {};
this.premapped = premapped != null ? premapped : {};
this.mappers = mappers;
this.targeting = targeting;
this.version = "0.2.1";
this.parser = new xml2js.Parser({
ignoreAttrs: true
});
winston.info("loading mappers");
if (this.mappers == null) {
this.mappers = {};
_ref = fs.readdirSync("ArgumentMappers");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
if (file.match(/.*\.js/)) {
(function(file) {
var m;
m = require("./ArgumentMappers/" + file).mapper;
_this.mappers[m.identifier] = m;
return winston.info("installed mapper for '" + m.identifier + "'");
})(file);
}
}
}
winston.info("loading targeting");
if (this.targeting == null) {
this.targeting = {};
_ref1 = fs.readdirSync("ProfilerTargeting");
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
file = _ref1[_j];
if (file.match(/.*\.js/)) {
(function(file) {
var t;
t = require("./ProfilerTargeting/" + file).targeting;
_this.targeting[t.name] = t;
return winston.info("installed targeting '" + t.name + "'");
})(file);
}
}
}
}
ArgumentGenerator.prototype.map = function(method, action, component, componentVersion, endpoint, fun, args) {
var cached, mapper, mapperId, promises, schema,
_this = this;
cached = this.premapped["" + method + "." + component + "." + componentVersion + "." + endpoint + "." + fun + "." + args];
if (cached != null) {
return Q.fcall(function() {
return cached;
});
}
mapperId = [component, componentVersion, endpoint].filter(function(c) {
return c != null;
}).join("/");
mapper = this.mappers[mapperId];
if (mapper == null) {
winston.warn("No mapper for " + mapperId + " found. Original args returned.");
return Q.fcall(function() {
return args;
});
}
schema = mapper.schema(method, endpoint, action, fun, decodeURI(args));
if (schema == null) {
winston.warn("No schema returned for '" + method + " " + action + " (" + component + "/" + componentVersion + ") " + endpoint + "/" + fun + "/" + args + "'. Original args returned.");
return Q.fcall(function() {
return args;
});
}
promises = _.map(_.flatten(schema), function(arg) {
var currentvalue, deferred, newvalue, options, value, _fn, _i, _len, _ref;
switch (arg.origin) {
case "mapi":
cached = _this.cache["" + arg.type + "." + arg.hql + "." + arg.depth];
value = cached != null ? cached : void 0;
if (value == null) {
deferred = Q.defer();
options = {
host: _this.server,
port: 8080,
path: ("/" + arg.type + "?depth=" + arg.depth + "&limit=200&hql=active=true") + (arg.hql != null ? " AND " + arg.hql : ""),
auth: "spider:spider",
headers: {
"User-Agent": "GLUTENFREE (AG-" + _this.version + ")"
}
};
http.get(options, function(res) {
var chunks;
chunks = "";
res.on("data", function(chunk) {
return chunks += chunk;
});
return res.on("end", function() {
console.log("get", options);
return _this.parser.parseString(chunks, function(err, data) {
var values;
values = _.flatten(_.values(data.Collection));
_this.cache["" + arg.type + "." + arg.hql + "." + arg.depth] = values;
arg.newvalue = values;
return deferred.resolve(arg);
});
});
}).on("error", function(e) {
winston.error(e);
return deferred.reject("error when getting " + arg.type + "." + arg.hql + "." + arg.depth);
});
return deferred.promise;
} else {
arg.newvalue = value;
return arg;
}
break;
case "configuration":
case "conf":
if (_this.lookupConfiguration != null) {
newvalue = [];
_ref = arg.currentvalue;
_fn = function(currentvalue) {
var v;
v = _this.lookupConfiguration[arg.type][currentvalue];
if (v == null) {
v = _.first(_.shuffle(_.values(_this.lookupConfiguration[arg.type])));
}
return newvalue.push(v);
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
currentvalue = _ref[_i];
_fn(currentvalue);
}
arg.newvalue = newvalue;
} else {
arg.newvalue = arg.currentvalue;
}
return arg;
case "fixed":
arg.newvalue = arg.currentvalue;
return arg;
}
});
return Q.allSettled(promises).then(function() {
var schematic, _fn, _i, _len, _ref;
_ref = _.flatten(schema);
_fn = function(schematic) {
var candidates;
if (schematic.origin === "mapi") {
candidates = schematic.filter != null ? _.filter(schematic.newvalue, function(t) {
return schematic.filter(t, schema);
}) : schematic.newvalue;
return schematic.newvalue = _.first(_.shuffle(_.flatten(_.pluck(candidates, schematic.property))), schematic.currentvalue.length);
}
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
schematic = _ref[_i];
_fn(schematic);
}
url = mapper.applySchema(method, action, endpoint, fun, schema);
_this.premapped["" + method + "." + component + "." + componentVersion + "." + endpoint + "." + fun + "." + args] = url;
console.log("url (pre-encode)", url);
return encodeURI(url);
});
};
return ArgumentGenerator;
})();
exports.AG = ArgumentGenerator;
if (require.main === module) {
argv = require('optimist').usage("Usage: $0 -i [inputfile] -o [outputfile] -s [server] -t [targeting] -c [configuration]").alias("s", "server").demand(["i", "o", "s"]).alias("c", "configiration").argv;
statsFile = argv.i[0] === "/" ? argv.i : "./" + argv.i;
stats = require(statsFile);
lookupConf = argv.c != null ? argv.c[0] === "/" ? require(argv.c) : require("./" + argv.c) : {};
winston.info("creating ag");
ag = new ArgumentGenerator(argv.s, lookupConf);
winston.info("looping stats");
promises = [];
_ref = stats.uniques;
_fn = function(id, info) {
info.newArgs = [];
return promises.push(ag.map(info.method, info.action, info.component, info.componentVersion, info.endpoint, info.fun, info.args).then((function(newarg) {
return info.newArgs.push(newarg);
})));
};
for (id in _ref) {
info = _ref[id];
_fn(id, info);
}
Q.all(promises).then(function() {
var targeting, targets;
targeting = ag.targeting[argv.t];
if (targeting == null) {
return fs.writeFile(argv.o, JSON.stringify(stats), function(err) {
if (err == null) {
return winston.info("raw output written to " + argv.o);
} else {
return winston.warn("error writing to " + argv.o);
}
});
} else {
targets = targeting.generate(stats);
return fs.writeFile(argv.o, JSON.stringify(targets), function(err) {
if (err == null) {
return winston.info("targeting (" + targeting.name + ") output written to " + argv.o);
} else {
return winston.warn("error writing to " + argv.o);
}
});
}
});
}