csc dashboard recent calls cover last 24h Change-Id: I19704643203795949c3e46b44fd136a59f83ac2bchanges/18/5018/6
parent
9c9f33340a
commit
4438756017
|
After Width: | Height: | Size: 771 B |
@ -0,0 +1,301 @@
|
||||
;(function(root) {
|
||||
'use strict';
|
||||
|
||||
var $ = root.jQuery || root.Zepto || root.$;
|
||||
|
||||
if (typeof $ === 'undefined') throw 'jquery.ajaxq requires jQuery or jQuery-compatible library (e.g. Zepto.js)';
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
*/
|
||||
var slice = Array.prototype.slice;
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
*/
|
||||
var noop = function() {};
|
||||
|
||||
/**
|
||||
* Copy of jQuery function
|
||||
* @type {Function}
|
||||
*/
|
||||
var isNumeric = function(obj) {
|
||||
return !$.isArray( obj ) && (obj - parseFloat( obj ) + 1) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Function}
|
||||
*/
|
||||
var isObject = function(obj) {
|
||||
return "[object Object]" === Object.prototype.toString.call(obj);
|
||||
}
|
||||
|
||||
|
||||
var Request = (function (argument) {
|
||||
|
||||
function Request(url, settings) {
|
||||
this._aborted = false;
|
||||
this._jqXHR = null;
|
||||
this._calls = {};
|
||||
this._args = [url, settings];
|
||||
this._deferred = $.Deferred();
|
||||
|
||||
this._deferred.pipe = this._deferred.then;
|
||||
|
||||
this.readyState = 1;
|
||||
}
|
||||
|
||||
var proto = Request.prototype;
|
||||
|
||||
$.extend(proto, {
|
||||
|
||||
// start jqXHR by calling $.ajax
|
||||
run: function() {
|
||||
var
|
||||
deferred = this._deferred,
|
||||
methodName, argsStack, i;
|
||||
|
||||
if (this._jqXHR !== null) {
|
||||
return this._jqXHR;
|
||||
}
|
||||
// clreate new jqXHR object
|
||||
var
|
||||
url = this._args[0],
|
||||
settings = this._args[1];
|
||||
|
||||
if (isObject(url)) {
|
||||
settings = url;
|
||||
} else {
|
||||
settings = $.extend(true, settings || {}, {
|
||||
url: url
|
||||
});
|
||||
}
|
||||
|
||||
this._jqXHR = $.ajax.call($, settings);
|
||||
|
||||
this._jqXHR.done(function() {
|
||||
deferred.resolve.apply(deferred, arguments);
|
||||
});
|
||||
|
||||
this._jqXHR.fail(function() {
|
||||
deferred.reject.apply(deferred, arguments);
|
||||
});
|
||||
|
||||
if (this._aborted) {
|
||||
this._jqXHR.abort(this.statusText);
|
||||
}
|
||||
|
||||
// apply buffered calls
|
||||
for (methodName in this._calls) {
|
||||
argsStack = this._calls[methodName];
|
||||
for (var i in argsStack) {
|
||||
this._jqXHR[methodName].apply(this._jqXHR, argsStack[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return this._jqXHR;
|
||||
},
|
||||
|
||||
// returns original jqXHR object if it exists
|
||||
// or writes to callected method to _calls and returns itself
|
||||
_call: function(methodName, args) {
|
||||
if (this._jqXHR !== null) {
|
||||
if (typeof this._jqXHR[methodName] === 'undefined') {
|
||||
return this._jqXHR;
|
||||
}
|
||||
return this._jqXHR[methodName].apply(this._jqXHR, args);
|
||||
}
|
||||
|
||||
this._calls[methodName] = this._calls[methodName] || [];
|
||||
this._calls[methodName].push(args);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
// returns original jqXHR object if it exists
|
||||
// or writes to callected method to _calls and returns itself
|
||||
abort: function(statusText) {
|
||||
if (this._jqXHR !== null) {
|
||||
var
|
||||
self = this,
|
||||
_copyProperties = ['readyState', 'status', 'statusText'],
|
||||
_return = this._jqXHR.abort.apply(this._jqXHR, arguments) || this._jqXHR;
|
||||
|
||||
if (_return) {
|
||||
$.each(_copyProperties, function(i, prop) {
|
||||
self[prop] = _return[prop];
|
||||
});
|
||||
}
|
||||
|
||||
return _return;
|
||||
}
|
||||
|
||||
this.statusText = statusText || 'abort';
|
||||
this.status = 0;
|
||||
this.readyState = 0;
|
||||
this._aborted = true;
|
||||
|
||||
return this;
|
||||
},
|
||||
state: function() {
|
||||
if (this._jqXHR !== null) {
|
||||
return this._jqXHR.state.apply(this._jqXHR, arguments);
|
||||
}
|
||||
return 'pending';
|
||||
}
|
||||
});
|
||||
|
||||
// each method returns self object
|
||||
var _chainMethods = ['setRequestHeader', 'overrideMimeType', 'statusCode',
|
||||
'done', 'fail', 'progress', 'complete', 'success', 'error', 'always' ];
|
||||
|
||||
$.each(_chainMethods, function(i, methodName) {
|
||||
proto[methodName] = function() {
|
||||
return this._call(methodName, slice.call(arguments)) || this._jqXHR;
|
||||
}
|
||||
});
|
||||
|
||||
var _nullMethods = ['getResponseHeader', 'getAllResponseHeaders'];
|
||||
|
||||
$.each(_nullMethods, function(i, methodName) {
|
||||
proto[methodName] = function() {
|
||||
// apply original method if _jqXHR exists
|
||||
if (this._jqXHR !== null) {
|
||||
return this._jqXHR[methodName].apply(this, arguments);
|
||||
}
|
||||
|
||||
// return null if origina method does not exists
|
||||
return null;
|
||||
};
|
||||
});
|
||||
|
||||
var _promiseMethods = ['pipe', 'then', 'promise'];
|
||||
|
||||
$.each(_promiseMethods, function(i, methodName) {
|
||||
proto[methodName] = function() {
|
||||
return this._deferred[methodName].apply(this._deferred, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
return Request;
|
||||
})()
|
||||
var Queue = (function() {
|
||||
|
||||
var _params = {}, _queueCounter = 0;
|
||||
|
||||
function _runNext(queue, request) {
|
||||
var
|
||||
removeIndex = _getStarted(queue).indexOf(request),
|
||||
nextRequest = _getPending(queue).shift();
|
||||
|
||||
if (removeIndex !== -1) {
|
||||
_getStarted(queue).splice(removeIndex, 1);
|
||||
}
|
||||
|
||||
if (typeof nextRequest !== 'undefined') {
|
||||
nextRequest
|
||||
.always($.proxy(_runNext, null, queue, nextRequest))
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
function _ajax(queue, request) {
|
||||
if (_getStarted(queue).length < _getBandwidth(queue)) {
|
||||
_getStarted(queue).push(request);
|
||||
request.always($.proxy(_runNext, null, queue, request));
|
||||
request.run();
|
||||
} else {
|
||||
_getPending(queue).push(request)
|
||||
}
|
||||
}
|
||||
|
||||
function _getParams(queue) {
|
||||
return _params[queue.id] || (_params[queue.id] = {});
|
||||
}
|
||||
|
||||
function _getParam(queue, name) {
|
||||
return _getParams(queue)[name];
|
||||
}
|
||||
|
||||
function _getStarted(queue) {
|
||||
return _getParams(queue).started || (_getParams(queue).started = []);
|
||||
}
|
||||
|
||||
function _getPending(queue) {
|
||||
return _getParams(queue).pending || (_getParams(queue).pending = []);
|
||||
}
|
||||
|
||||
function _setBandwidth(queue, bandwidth) {
|
||||
if ((bandwidth = parseInt(bandwidth || 1, 10)) < 1) throw "Bandwidth can\'t be less then 1";
|
||||
_getParams(queue).bandwidth = bandwidth;
|
||||
}
|
||||
|
||||
function _getBandwidth(queue, bandwidth) {
|
||||
return _getParams(queue).bandwidth;
|
||||
}
|
||||
|
||||
function Queue(bandwidth) {
|
||||
if (typeof bandwidth !== 'undefined' && !isNumeric(bandwidth)) throw "number expected";
|
||||
this.id = ++_queueCounter;
|
||||
_setBandwidth(this, bandwidth);
|
||||
};
|
||||
|
||||
$.extend(Queue.prototype, {
|
||||
ajax: function(url, settings) {
|
||||
var request = new Request(url, settings);
|
||||
_ajax(this, request);
|
||||
return request;
|
||||
},
|
||||
getJSON: function ( url, data, callback ) {
|
||||
return this.get( url, data, callback, "json" );
|
||||
},
|
||||
getBandwidth: function() {
|
||||
return _getBandwidth(this);
|
||||
}
|
||||
});
|
||||
|
||||
$.each(['get', 'post'], function(i, method) {
|
||||
Queue.prototype[method] = function( url, data, callback, type ) {
|
||||
// shift arguments if data argument was omitted
|
||||
if ( $.isFunction( data ) ) {
|
||||
type = type || callback;
|
||||
callback = data;
|
||||
data = undefined;
|
||||
}
|
||||
|
||||
return this.ajax({
|
||||
url: url,
|
||||
type: method,
|
||||
dataType: type,
|
||||
data: data,
|
||||
success: callback
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return Queue;
|
||||
})();
|
||||
|
||||
if (typeof $.ajaxq !== 'undefined') throw "Namespace $.ajaxq is Alread y busy.";
|
||||
|
||||
var _queue = new Queue();
|
||||
|
||||
$.ajaxq = function(url, settions) {
|
||||
return _queue.ajax.apply(_queue, arguments);
|
||||
};
|
||||
|
||||
$.each(['get', 'post', 'getJSON'], function(i, methodName) {
|
||||
$.ajaxq[methodName] = function() {
|
||||
return _queue[methodName].apply(_queue, arguments);
|
||||
}
|
||||
});
|
||||
|
||||
$.ajaxq.Queue = function(bandwidth) {
|
||||
return new Queue(bandwidth);
|
||||
};
|
||||
|
||||
$.ajaxq.Request = function(url, settings) {
|
||||
return new Request(url, settings);
|
||||
}
|
||||
|
||||
})(this);
|
||||
@ -0,0 +1,134 @@
|
||||
/*! sprintf.js | Copyright (c) 2007-2013 Alexandru Marasteanu <hello at alexei dot ro> | 3 clause BSD license */
|
||||
|
||||
(function(ctx) {
|
||||
var sprintf = function() {
|
||||
if (!sprintf.cache.hasOwnProperty(arguments[0])) {
|
||||
sprintf.cache[arguments[0]] = sprintf.parse(arguments[0]);
|
||||
}
|
||||
return sprintf.format.call(null, sprintf.cache[arguments[0]], arguments);
|
||||
};
|
||||
|
||||
sprintf.format = function(parse_tree, argv) {
|
||||
var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;
|
||||
for (i = 0; i < tree_length; i++) {
|
||||
node_type = get_type(parse_tree[i]);
|
||||
if (node_type === 'string') {
|
||||
output.push(parse_tree[i]);
|
||||
}
|
||||
else if (node_type === 'array') {
|
||||
match = parse_tree[i]; // convenience purposes only
|
||||
if (match[2]) { // keyword argument
|
||||
arg = argv[cursor];
|
||||
for (k = 0; k < match[2].length; k++) {
|
||||
if (!arg.hasOwnProperty(match[2][k])) {
|
||||
throw(sprintf('[sprintf] property "%s" does not exist', match[2][k]));
|
||||
}
|
||||
arg = arg[match[2][k]];
|
||||
}
|
||||
}
|
||||
else if (match[1]) { // positional argument (explicit)
|
||||
arg = argv[match[1]];
|
||||
}
|
||||
else { // positional argument (implicit)
|
||||
arg = argv[cursor++];
|
||||
}
|
||||
|
||||
if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) {
|
||||
throw(sprintf('[sprintf] expecting number but found %s', get_type(arg)));
|
||||
}
|
||||
switch (match[8]) {
|
||||
case 'b': arg = arg.toString(2); break;
|
||||
case 'c': arg = String.fromCharCode(arg); break;
|
||||
case 'd': arg = parseInt(arg, 10); break;
|
||||
case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;
|
||||
case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;
|
||||
case 'o': arg = arg.toString(8); break;
|
||||
case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;
|
||||
case 'u': arg = arg >>> 0; break;
|
||||
case 'x': arg = arg.toString(16); break;
|
||||
case 'X': arg = arg.toString(16).toUpperCase(); break;
|
||||
}
|
||||
arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg);
|
||||
pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' ';
|
||||
pad_length = match[6] - String(arg).length;
|
||||
pad = match[6] ? str_repeat(pad_character, pad_length) : '';
|
||||
output.push(match[5] ? arg + pad : pad + arg);
|
||||
}
|
||||
}
|
||||
return output.join('');
|
||||
};
|
||||
|
||||
sprintf.cache = {};
|
||||
|
||||
sprintf.parse = function(fmt) {
|
||||
var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;
|
||||
while (_fmt) {
|
||||
if ((match = /^[^\x25]+/.exec(_fmt)) !== null) {
|
||||
parse_tree.push(match[0]);
|
||||
}
|
||||
else if ((match = /^\x25{2}/.exec(_fmt)) !== null) {
|
||||
parse_tree.push('%');
|
||||
}
|
||||
else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {
|
||||
if (match[2]) {
|
||||
arg_names |= 1;
|
||||
var field_list = [], replacement_field = match[2], field_match = [];
|
||||
if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
|
||||
field_list.push(field_match[1]);
|
||||
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
|
||||
if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
|
||||
field_list.push(field_match[1]);
|
||||
}
|
||||
else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) {
|
||||
field_list.push(field_match[1]);
|
||||
}
|
||||
else {
|
||||
throw('[sprintf] huh?');
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw('[sprintf] huh?');
|
||||
}
|
||||
match[2] = field_list;
|
||||
}
|
||||
else {
|
||||
arg_names |= 2;
|
||||
}
|
||||
if (arg_names === 3) {
|
||||
throw('[sprintf] mixing positional and named placeholders is not (yet) supported');
|
||||
}
|
||||
parse_tree.push(match);
|
||||
}
|
||||
else {
|
||||
throw('[sprintf] huh?');
|
||||
}
|
||||
_fmt = _fmt.substring(match[0].length);
|
||||
}
|
||||
return parse_tree;
|
||||
};
|
||||
|
||||
var vsprintf = function(fmt, argv, _argv) {
|
||||
_argv = argv.slice(0);
|
||||
_argv.splice(0, 0, fmt);
|
||||
return sprintf.apply(null, _argv);
|
||||
};
|
||||
|
||||
/**
|
||||
* helpers
|
||||
*/
|
||||
function get_type(variable) {
|
||||
return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
|
||||
}
|
||||
|
||||
function str_repeat(input, multiplier) {
|
||||
for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */}
|
||||
return output.join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* export to either browser or node.js
|
||||
*/
|
||||
ctx.sprintf = sprintf;
|
||||
ctx.vsprintf = vsprintf;
|
||||
})(typeof exports != "undefined" ? exports : window);
|
||||
Loading…
Reference in new issue