testing and clients are 'online' and working know to get the tests running againts an actual ES node

This commit is contained in:
Spencer Alger
2013-09-27 12:46:45 -07:00
parent 0ac81e1989
commit e21fb21ee3
228 changed files with 51083 additions and 921 deletions

28
scripts/.jshintrc Normal file
View File

@ -0,0 +1,28 @@
{
"node": true,
"white": true,
"bitwise": false,
"curly": true,
"eqnull": true,
"eqeqeq": true,
"forin": true,
"immed": true,
"expr": false,
"indent": 2,
"latedef": "nofunc",
"newcap": true,
"noarg": true,
"noempty": true,
"undef": true,
"quotmark": "single",
"plusplus": false,
"boss": true,
"trailing": true,
"laxbreak": true,
"laxcomma": true,
"validthis": true,
"sub": true,
"maxlen": 140
}

View File

@ -0,0 +1,60 @@
module.exports = {
'cluster.nodeHotThreads': [
'/_cluster/nodes/hotthreads',
'/_cluster/nodes/hot_threads',
'/_nodes/hot_threads',
'/_cluster/nodes/{node_id}/hotthreads',
'/_cluster/nodes/{node_id}/hot_threads',
'/_nodes/{node_id}/hot_threads'
],
'cluster.nodeInfo': [
'/_cluster/nodes',
'/_nodes/settings',
'/_nodes/os',
'/_nodes/process',
'/_nodes/jvm',
'/_nodes/thread_pool',
'/_nodes/network',
'/_nodes/transport',
'/_nodes/http',
'/_nodes/plugin',
'/_cluster/nodes/{node_id}',
'/_nodes/{node_id}/settings',
'/_nodes/{node_id}/os',
'/_nodes/{node_id}/process',
'/_nodes/{node_id}/jvm',
'/_nodes/{node_id}/thread_pool',
'/_nodes/{node_id}/network',
'/_nodes/{node_id}/transport',
'/_nodes/{node_id}/http',
'/_nodes/{node_id}/plugin'
],
'cluster.nodeShutdown': [
'/_cluster/nodes/_shutdown'
],
'cluster.nodeStats': [
'/_cluster/nodes/stats',
'/_nodes/stats/{metric_family}',
'/_nodes/stats/indices/{metric}/{fields}',
'/_cluster/nodes/{node_id}/stats',
'/_nodes/{node_id}/stats/{metric_family}',
'/_nodes/{node_id}/stats/indices/{metric}/{fields}'
],
'get': [
'/{index}/{type}/{id}/_source'
],
'indices.deleteMapping': [
'/{index}/{type}/_mapping'
],
'indices.stats': [
'_stats/{metric_family}',
'/_stats/indexing',
'/_stats/indexing/{indexing_types}',
'/_stats/search/{search_groups}',
'/_stats/fielddata/{fields}',
'/{index}/_stats/{metric_family}',
'/{index}/_stats/indexing',
'/{index}/_stats/search/{search_groups}',
'/{index}/_stats/fielddata/{fields}'
]
};

View File

@ -0,0 +1,70 @@
var _ = require('../../src/lib/utils')
, asset = require('assert')
, path = require('path')
, fs = require('fs')
, mkdirp = require('mkdirp')
, docs = _.requireDir(module, '../../es_api_spec/api')
, outputDir = _.joinPath(__dirname, '../../src/api/')
, templates = require('./templates')
, notes = require('./notes')
, aliases = require('./aliases');
// completely delete the output directory
(function clean(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
clean(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
})(outputDir);
// itterate all of the found docs
_.each(docs, function (doc) {
// and each definition within each doc (should only be one)
_.each(doc, function (def, name) {
name = _.map(name.split('.'), function (name) {
return _.camelCase(name);
}).join('.');
var steps = name.split('.')
, fileName = steps.pop() + '.js'
, dirName = _.joinPath(outputDir, steps.join('/') || './');
var spec = {
name: name,
methods: def.methods,
docUrl: def.documentation,
urlParts: def.url.parts,
params: def.url.params,
urls: _.difference(def.url.paths, aliases[name]),
body: def.body || null,
path2lib: _.repeat('../', steps.length + 1) + 'lib/',
notes: notes[name]
};
spec.enumOptions = _.object(_.filter(_.map(_.pairs(_.extend({}, spec.params, spec.urlParts)), function (pair) {
// pair = [name, param];
if (pair[1].type === 'enum') {
return [_.camelCase(pair[0]), pair[1].options];
}
})));
// turn a url string into an object describing the url, then sort them in decending order by how many args they have
spec.urls = _.sortBy(spec.urls, function (url) {
var vars = url.match(templates.urlParamRE);
return vars ? vars.length * -1 : 0;
});
mkdirp.sync(dirName);
fs.writeFileSync(_.joinPath(dirName, fileName), templates.action(spec));
});
});

View File

@ -0,0 +1,2 @@
exports['cluster.node_info'] =
'//TODO: this enpoint ***needs*** work, many of the possible urls are not supported';

View File

@ -0,0 +1,66 @@
var _ = require('<%= path2lib %>utils');<%
if (_.keys(enumOptions).length) {
%>
<% _.each(enumOptions, function(options, name) {
%>
var <%= name %>Options = <%= stringify(options) %>;<%
});
}
%>
<%= notes ? notes + '\n' : ''
%>
/**
* Perform an elasticsearch [<%= name %>](<%= docUrl %>) request
*
* @for Client
* @method <%= name %>
* @param {Object} params - An object with parameters used to carry out this action<% _.each(params, function(param, paramName) { %>
* @param {<%= paramType(param.type) %>} <%= paramWithDefault('params.' + paramName, param.default) %><% if (param.description) { %> - <%= param.description %><% } %><%
})
%>
*/
function do<%= _.studlyCase(name) %>(params) {
var request = {}
, url = {}
, query = {};
params = params || {};<%
if (body) { %>
request.body = params.body || null;<%
}
%>
<%
if (methods.length > 1) { %>
if (params.method) {
if (<%= _.map(methods, function (method) { return 'params.method === ' + stringify(method) }).join(' || ') %>) {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of <%= methods.join(', ') %>');
}
} else {
request.method = <%= stringify(methods[0]) %>;
}<%
} else { %>
request.method = <%= stringify(methods[0]) %>;<%
}
%>
// find the url's params
<%= writeParams(2, urlParts, 'url.') %>
// build the url
<%= writeUrls(2, urls) %>
// build the query string
<%= writeParams(2, params, 'query.') %>
request.url = request.url + _.makeQueryString(query);
return this.client.request(request);
}
module.exports = do<%= _.studlyCase(name) %>;

View File

@ -0,0 +1 @@
<%= set %> = <%= get %>;

View File

@ -0,0 +1,7 @@
if (<%= get %>.toLowerCase && (<%= get %> = <%= get %>.toLowerCase())
&& (<%= get %> === 'no' || <%= get %> === 'off')
) {
<%= set %> = false;
} else {
<%= set %> = !!<%= get %>;
}

View File

@ -0,0 +1,5 @@
if (_.isInterval(<%= get %>)) {
<%= set %> = <%= get %>;
} else {
throw new TypeError('Invalid <%= name %>: ' + <%= get %> + ' should be in interval notation (an integer followed by one of Mwdhmsy).');
}

View File

@ -0,0 +1,8 @@
if (_.contains(<%= _.camelCase(name) %>Options, <%= get %>)) {
<%= set %> = <%= get %>;
} else {
throw new TypeError(
'Invalid <%= name %>: ' + <%= get %> +
' should be one of ' + <%= _.camelCase(name) %>Options.join(', ') + '.'
);
}

View File

@ -0,0 +1,177 @@
var _ = require('../../../src/lib/utils')
, fs = require('fs')
, path = require('path')
, urlParamRE = /\{(\w+)\}/g;
/**
* Simple manager to take care of indentation
* @param {number} i - Width of the indentation
* @return {function} - Call this to add a new line to the output
*/
function lines(i) {
function l(line) {
l.lines.push(_.repeat(' ', l.indent) + line);
return l;
}
l.lines = [];
l.indent = i || 0;
l.split = function (toSplit) {
_.each(_.filter(toSplit.split(/\r|\n/)), l);
return l;
};
l.in = function () {
l.indent += 2;
return l;
};
l.out = function () {
l.indent -= 2;
return l;
};
l.toString = function () {
return l.lines.join('\n');
};
return l;
}
function stringify(thing) {
return JSON.stringify(thing)
.replace('\'', '\\\'')
.replace(/\\?"/g, function (quote) {
// replace external (unescaped) double quotes with single quotes
return quote === '\\"' ? '"' : '\'';
})
// inject a space between array parts
.replace(/([^\\])','/g, '$1\', \'');
}
/**
* We'll collect the templates here
* @type {Object}
*/
var templates = {};
/**
* These keys will be available as local variables to each template
* @type {Object}
*/
var templateGlobals = {
writeParams: function (indent, params, namespace) {
var l = lines(indent);
_.each(params, function (param, name) {
if (!param.required) {
l('if (typeof params.' + name + ' !== \'undefined\') {').in();
}
l.split(templates[param.type || 'any']({
get: 'params.' + name,
set: namespace + name,
name: name
}));
if (!param.required) {
l.out();
l('}');
}
l('');
});
return l.toString();
},
writeUrls: function (indent, urls) {
var l = lines(indent);
_.each(urls, function (url, urlIndex) {
// collect the vars from the url and replace them to form the js that will build the url
var makeL = lines(), vars = [];
makeL('request.url = \'' + url.replace(urlParamRE, function (match, varName) {
vars.push(varName);
return '\' + url.' + varName + ' + \'';
}) + '\';');
if (vars.length || urlIndex) {
var condition = _.map(vars, function (v) { return 'url.hasOwnProperty(\'' + v + '\')'; }).join(' && ');
l((urlIndex > 0 ? 'else ' : '') + (condition ? 'if (' + condition + ') ' : ' ') + '{').in();
l.split(makeL.toString()).out();
l('}');
if (urlIndex === urls.length - 1 && vars.length) {
l('else {').in();
l('throw new TypeError(\'Unable to build a url with those params. Supply at least ' + vars.join(', ') + '\');').out();
l('}');
}
} else {
l.split(makeL.toString());
}
});
l('');
return l.toString();
},
/**
* we want strings in code to use single-quotes, so this will JSON encode vars, but then
* modify them to follow our code standards.
*
* @param {*} thing - Any thing
* @return {String} - our pretty string
*/
stringify: stringify,
_: _,
paramType: function (type) {
switch (type && type.toLowerCase ? type.toLowerCase() : 'any') {
case 'time':
return 'Date|Number';
case 'any':
return '*';
case 'enum':
return 'String';
default:
return type;
}
},
paramWithDefault: function (name, def) {
if (def) {
return '[' + name + '=' + def + ']';
} else {
return name;
}
}
};
fs.readdirSync(path.resolve(__dirname)).forEach(function (filename) {
var name = filename.replace(/\..+$/, '');
if (name !== 'index') {
templates[name] = _.template(
fs.readFileSync(path.resolve(__dirname, filename), {
encoding: 'utf8'
}),
null,
{
imports: templateGlobals
}
);
}
});
templates.text = templates.string;
module.exports = {
action: templates.action,
urlParamRE: urlParamRE
};

View File

@ -0,0 +1,7 @@
if (typeof <%= get %> === 'string') {
<%= set %> = <%= get %>;
} else if (_.isArray(<%= get %>)) {
<%= set %> = <%= get %>.join(',');
} else {
throw new TypeError('Invalid <%= name %>: ' + <%= get %> + ' should be a comma seperated list or array.');
}

View File

@ -0,0 +1,5 @@
if (_.isNumeric(<%= get %>)) {
<%= set %> = <%= get %> * 1;
} else {
throw new TypeError('Invalid <%= name %>: ' + <%= get %> + ' should be a number.');
}

View File

@ -0,0 +1,5 @@
if (typeof <%= get %> !== 'object' && typeof <%= get %> !== 'undefined') {
<%= set %> = '' + <%= get %>;
} else {
throw new TypeError('Invalid <%= name %>: ' + <%= get %> + ' should be a string.');
}

View File

@ -0,0 +1,7 @@
if (<%= get %> instanceof Date) {
<%= set %> = <%= get %>.getTime();
} else if (_.isNumeric(<%= get %>)) {
<%= set %> = <%= get %>;
} else {
throw new TypeError('Invalid <%= name %>: ' + <%= get %> + ' should be be some sort of time.');
}

10
scripts/scratch.js Normal file
View File

@ -0,0 +1,10 @@
var elasticsearch = require('../src/elasticsearch')
, EventEmitter = require('events').EventEmitter;
var client = new elasticsearch.Client({
logger: 'trace'
});
console.log(EventEmitter.listenerCount(client.log, 'trace'));
client.cluster.node_stats();