commit/galaxy-central: carlfeberhard: QA, metrics-logger: finish tests for existing; casperjs: restore correct return value for loggedInAs
1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/9fdf721e1ba1/ Changeset: 9fdf721e1ba1 User: carlfeberhard Date: 2014-04-18 21:58:29 Summary: QA, metrics-logger: finish tests for existing; casperjs: restore correct return value for loggedInAs Affected #: 4 files diff -r e2dd42ca0708315efffd55479e68a81954b9c931 -r 9fdf721e1ba10ffafd5a3c37e535809e1682cbb9 static/scripts/packed/utils/metrics-logger.js --- a/static/scripts/packed/utils/metrics-logger.js +++ b/static/scripts/packed/utils/metrics-logger.js @@ -1,1 +1,1 @@ -define([],function(){function h(z){z=z||{};var y=this;y.consoleLogger=z.consoleLogger||null;y._init(z);return y}h.ALL=0;h.DEBUG=10;h.INFO=20;h.WARN=30;h.ERROR=40;h.METRIC=50;h.NONE=100;h.defaultOptions={logLevel:h.NONE,consoleLevel:h.NONE,defaultNamespace:"Galaxy",clientPrefix:"client.",maxCacheSize:3000,postSize:1000,addTime:true,postUrl:"/api/metrics",getPingData:undefined,onServerResponse:undefined};h.prototype._init=function i(A){var z=this;z.options={};for(var y in h.defaultOptions){if(h.defaultOptions.hasOwnProperty(y)){z.options[y]=(A.hasOwnProperty(y))?(A[y]):(h.defaultOptions[y])}}z.options.logLevel=z._parseLevel(z.options.logLevel);z.options.consoleLevel=z._parseLevel(z.options.consoleLevel);z._sending=false;z._postSize=z.options.postSize;z._initCache();return z};h.prototype._initCache=function a(){this.cache=new w({maxSize:this.options.maxCacheSize})};h.prototype._parseLevel=function j(A){var z=typeof A;if(z==="number"){return A}if(z==="string"){var y=A.toUpperCase();if(h.hasOwnProperty(y)){return h[y]}}throw new Error("Unknown log level: "+A)};h.prototype.emit=function m(B,A,z){var y=this;A=A||y.options.defaultNamespace;if(!B||!z){return y}B=y._parseLevel(B);if(B>=y.options.logLevel){y._addToCache(B,A,z)}if(y.consoleLogger&&B>=y.options.consoleLevel){y._emitToConsole(B,A,z)}return y};h.prototype._addToCache=function b(D,A,z){this._emitToConsole("debug","MetricsLogger",["_addToCache:",arguments,this.options.addTime,this.cache.length()]);var y=this;try{var C=y.cache.add(y._buildEntry(D,A,z));if(C>=y._postSize){y._postCache()}}catch(B){if(y.options.consoleLevel<=h.WARN){console.warn("Metrics logger could not stringify logArguments:",A,z);console.error(B)}}return y};h.prototype._buildEntry=function r(B,z,y){this._emitToConsole("debug","MetricsLogger",["_buildEntry:",arguments]);var A={level:B,namespace:this.options.clientPrefix+z,args:y};if(this.options.addTime){A.time=new Date().toISOString()}return A};h.prototype._postCache=function s(B){B=B||{};this._emitToConsole("info","MetricsLogger",["_postCache",B,this._postSize]);if(!this.options.postUrl||this._sending){return jQuery.when({})}var A=this,D=B.count||A._postSize,y=A.cache.get(D),C=y.length,z=(typeof A.options.getPingData==="function")?(A.options.getPingData()):({});z.metrics=A._preprocessCache(y);A._sending=true;return jQuery.post(A.options.postUrl,z).always(function(){A._sending=false}).fail(function(){A._postSize=A.options.maxCacheSize}).done(function(E){if(typeof A.options.onServerResponse==="function"){A.options.onServerResponse(E)}A.cache.remove(C);A._postSize=A.options.postSize})};h.prototype._preprocessCache=function f(y){return["[",(y.join(",\n")),"]"].join("\n")};h.prototype._emitToConsole=function c(C,B,A){var y=this;if(!y.consoleLogger){return y}var z=Array.prototype.slice.call(A,0);z.unshift(B);if(C>=h.METRIC&&typeof(y.consoleLogger.info)==="function"){return y.consoleLogger.info.apply(y.consoleLogger,z)}else{if(C>=h.ERROR&&typeof(y.consoleLogger.error)==="function"){return y.consoleLogger.error.apply(y.consoleLogger,z)}else{if(C>=h.WARN&&typeof(y.consoleLogger.warn)==="function"){y.consoleLogger.warn.apply(y.consoleLogger,z)}else{if(C>=h.INFO&&typeof(y.consoleLogger.info)==="function"){y.consoleLogger.info.apply(y.consoleLogger,z)}else{if(C>=h.DEBUG&&typeof(y.consoleLogger.debug)==="function"){y.consoleLogger.debug.apply(y.consoleLogger,z)}else{if(typeof(y.consoleLogger.log)==="function"){y.consoleLogger.log.apply(y.consoleLogger,z)}}}}}}return y};h.prototype.debug=function l(){this.emit(h.DEBUG,this.options.defaultNamespace,arguments)};h.prototype.log=function g(){this.emit(1,this.options.defaultNamespace,arguments)};h.prototype.info=function u(){this.emit(h.INFO,this.options.defaultNamespace,arguments)};h.prototype.warn=function t(){this.emit(h.WARN,this.options.defaultNamespace,arguments)};h.prototype.error=function p(){this.emit(h.ERROR,this.options.defaultNamespace,arguments)};h.prototype.metric=function n(){this.emit(h.METRIC,this.options.defaultNamespace,arguments)};function w(z){var y=this;y._cache=[];return y._init(z||{})}w.defaultOptions={maxSize:5000};w.prototype._init=function i(y){this.maxSize=y.maxSize||w.defaultOptions.maxSize;return this};w.prototype.add=function k(A){var z=this,y=(z.length()+1)-z.maxSize;if(y>0){z.remove(y)}z._cache.push(z._preprocessEntry(A));return z.length()};w.prototype._preprocessEntry=function q(y){return JSON.stringify(y)};w.prototype.length=function e(){return this._cache.length};w.prototype.get=function v(y){return this._cache.slice(0,y)};w.prototype.remove=function x(y){return this._cache.splice(0,y)};w.prototype.stringify=function o(y){return["[",(this.get(y).join(",\n")),"]"].join("\n")};w.prototype.print=function d(){this._cache.forEach(function(y){console.log(y)})};return{MetricsLogger:h,LoggingCache:w}}); \ No newline at end of file +define([],function(){function h(z){z=z||{};var y=this;y.userId=window.bootstrapped?window.bootstrapped.user.id:null;y.userId=y.userId||z.userId||null;y.consoleLogger=z.consoleLogger||null;y._init(z);return y}h.ALL=0;h.DEBUG=10;h.INFO=20;h.WARN=30;h.ERROR=40;h.METRIC=50;h.NONE=100;h.defaultOptions={logLevel:h.NONE,consoleLevel:h.NONE,defaultNamespace:"Galaxy",clientPrefix:"client.",maxCacheSize:3000,postSize:1000,addTime:true,postUrl:"/api/metrics",getPingData:undefined,onServerResponse:undefined};h.prototype._init=function i(A){var z=this;z.options={};for(var y in h.defaultOptions){if(h.defaultOptions.hasOwnProperty(y)){z.options[y]=(A.hasOwnProperty(y))?(A[y]):(h.defaultOptions[y])}}z.options.logLevel=z._parseLevel(z.options.logLevel);z.options.consoleLevel=z._parseLevel(z.options.consoleLevel);z._sending=false;z._postSize=z.options.postSize;z._initCache();return z};h.prototype._initCache=function a(){this.cache=new w({maxSize:this.options.maxCacheSize})};h.prototype._parseLevel=function j(A){var z=typeof A;if(z==="number"){return A}if(z==="string"){var y=A.toUpperCase();if(h.hasOwnProperty(y)){return h[y]}}throw new Error("Unknown log level: "+A)};h.prototype.emit=function m(B,A,z){var y=this;A=A||y.options.defaultNamespace;if(!B||!z){return y}B=y._parseLevel(B);if(B>=y.options.logLevel){y._addToCache(B,A,z)}if(y.consoleLogger&&B>=y.options.consoleLevel){y._emitToConsole(B,A,z)}return y};h.prototype._addToCache=function b(D,A,z){this._emitToConsole("debug","MetricsLogger",["_addToCache:",arguments,this.options.addTime,this.cache.length()]);var y=this;try{var C=y.cache.add(y._buildEntry(D,A,z));if(C>=y._postSize){y._postCache()}}catch(B){y._emitToConsole("warn","MetricsLogger",["Metrics logger could not stringify logArguments:",A,z]);y._emitToConsole("error","MetricsLogger",[B])}return y};h.prototype._buildEntry=function r(B,z,y){this._emitToConsole("debug","MetricsLogger",["_buildEntry:",arguments]);var A={level:B,namespace:this.options.clientPrefix+z,args:y};if(this.options.addTime){A.time=new Date().toISOString()}return A};h.prototype._postCache=function s(B){B=B||{};this._emitToConsole("info","MetricsLogger",["_postCache",B,this._postSize]);if(!this.options.postUrl||this._sending){return jQuery.when({})}var A=this,D=B.count||A._postSize,y=A.cache.get(D),C=y.length,z=(typeof A.options.getPingData==="function")?(A.options.getPingData()):({});z.metrics=A._preprocessCache(y);A._sending=true;return jQuery.post(A.options.postUrl,z).always(function(){A._sending=false}).fail(function(){A._postSize=A.options.maxCacheSize}).done(function(E){if(typeof A.options.onServerResponse==="function"){A.options.onServerResponse(E)}A.cache.remove(C);A._postSize=A.options.postSize})};h.prototype._preprocessCache=function f(y){return["[",(y.join(",\n")),"]"].join("\n")};h.prototype._emitToConsole=function c(C,B,A){var y=this;if(!y.consoleLogger){return y}var z=Array.prototype.slice.call(A,0);z.unshift(B);if(C>=h.METRIC&&typeof(y.consoleLogger.info)==="function"){return y.consoleLogger.info.apply(y.consoleLogger,z)}else{if(C>=h.ERROR&&typeof(y.consoleLogger.error)==="function"){return y.consoleLogger.error.apply(y.consoleLogger,z)}else{if(C>=h.WARN&&typeof(y.consoleLogger.warn)==="function"){y.consoleLogger.warn.apply(y.consoleLogger,z)}else{if(C>=h.INFO&&typeof(y.consoleLogger.info)==="function"){y.consoleLogger.info.apply(y.consoleLogger,z)}else{if(C>=h.DEBUG&&typeof(y.consoleLogger.debug)==="function"){y.consoleLogger.debug.apply(y.consoleLogger,z)}else{if(typeof(y.consoleLogger.log)==="function"){y.consoleLogger.log.apply(y.consoleLogger,z)}}}}}}return y};h.prototype.log=function g(){this.emit(1,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))};h.prototype.debug=function l(){this.emit(h.DEBUG,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))};h.prototype.info=function u(){this.emit(h.INFO,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))};h.prototype.warn=function t(){this.emit(h.WARN,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))};h.prototype.error=function p(){this.emit(h.ERROR,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))};h.prototype.metric=function n(){this.emit(h.METRIC,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))};function w(z){var y=this;y._cache=[];return y._init(z||{})}w.defaultOptions={maxSize:5000};w.prototype._init=function i(y){this.maxSize=y.maxSize||w.defaultOptions.maxSize;return this};w.prototype.add=function k(A){var z=this,y=(z.length()+1)-z.maxSize;if(y>0){z.remove(y)}z._cache.push(z._preprocessEntry(A));return z.length()};w.prototype._preprocessEntry=function q(y){return JSON.stringify(y)};w.prototype.length=function e(){return this._cache.length};w.prototype.get=function v(y){return this._cache.slice(0,y)};w.prototype.remove=function x(y){return this._cache.splice(0,y)};w.prototype.stringify=function o(y){return["[",(this.get(y).join(",\n")),"]"].join("\n")};w.prototype.print=function d(){this._cache.forEach(function(y){console.log(y)})};return{MetricsLogger:h,LoggingCache:w}}); \ No newline at end of file diff -r e2dd42ca0708315efffd55479e68a81954b9c931 -r 9fdf721e1ba10ffafd5a3c37e535809e1682cbb9 static/scripts/utils/metrics-logger.js --- a/static/scripts/utils/metrics-logger.js +++ b/static/scripts/utils/metrics-logger.js @@ -33,7 +33,8 @@ //TODO: this might be used if we store the logs in browser storage ///** */ - //self.userId = options.userId || null; + self.userId = window.bootstrapped? window.bootstrapped.user.id: null; + self.userId = self.userId || options.userId || null; /** the (optional) console to emit logs to */ self.consoleLogger = options.consoleLogger || null; @@ -161,10 +162,10 @@ } // discard entry if an error occurs, but warn if level set to do so } catch( err ){ - if( self.options.consoleLevel <= MetricsLogger.WARN ){ - console.warn( 'Metrics logger could not stringify logArguments:', namespace, logArguments ); - console.error( err ); - } + self._emitToConsole( 'warn', 'MetricsLogger', + [ 'Metrics logger could not stringify logArguments:', namespace, logArguments ] ); + self._emitToConsole( 'error', 'MetricsLogger', [ err ] ); + } return self; }; @@ -268,34 +269,40 @@ //----------------------------------------------------------------------------- shortcuts // generic functions when logging from non-namespaced object (e.g. templates) +/** log to default namespace */ +MetricsLogger.prototype.log = function log(){ + this.emit( 1, this.options.defaultNamespace, + Array.prototype.slice.call( arguments, 0 ) ); +}; + /** debug to default namespace */ MetricsLogger.prototype.debug = function debug(){ - this.emit( MetricsLogger.DEBUG, this.options.defaultNamespace, arguments ); -}; - -/** log to default namespace */ -MetricsLogger.prototype.log = function log(){ - this.emit( 1, this.options.defaultNamespace, arguments ); + this.emit( MetricsLogger.DEBUG, this.options.defaultNamespace, + Array.prototype.slice.call( arguments, 0 ) ); }; /** info to default namespace */ MetricsLogger.prototype.info = function info(){ - this.emit( MetricsLogger.INFO, this.options.defaultNamespace, arguments ); + this.emit( MetricsLogger.INFO, this.options.defaultNamespace, + Array.prototype.slice.call( arguments, 0 ) ); }; /** warn to default namespace */ MetricsLogger.prototype.warn = function warn(){ - this.emit( MetricsLogger.WARN, this.options.defaultNamespace, arguments ); + this.emit( MetricsLogger.WARN, this.options.defaultNamespace, + Array.prototype.slice.call( arguments, 0 ) ); }; /** error to default namespace */ MetricsLogger.prototype.error = function error(){ - this.emit( MetricsLogger.ERROR, this.options.defaultNamespace, arguments ); + this.emit( MetricsLogger.ERROR, this.options.defaultNamespace, + Array.prototype.slice.call( arguments, 0 ) ); }; /** metric to default namespace */ MetricsLogger.prototype.metric = function metric(){ - this.emit( MetricsLogger.METRIC, this.options.defaultNamespace, arguments ); + this.emit( MetricsLogger.METRIC, this.options.defaultNamespace, + Array.prototype.slice.call( arguments, 0 ) ); }; diff -r e2dd42ca0708315efffd55479e68a81954b9c931 -r 9fdf721e1ba10ffafd5a3c37e535809e1682cbb9 test/casperjs/modules/user.js --- a/test/casperjs/modules/user.js +++ b/test/casperjs/modules/user.js @@ -165,7 +165,7 @@ User.prototype.loggedInAs = function loggedInAs(){ var currUser = this.spaceghost.api.users.show( 'current' ); //this.spaceghost.debug( this.spaceghost.jsonStr( currUser ) ); - return currUser.email; + return currUser.email || ''; //TODO: due to late rendering of masthead this is no longer reliable - need a wait for in the main page //return this.spaceghost.jumpToTop( function(){ // var userEmail = ''; diff -r e2dd42ca0708315efffd55479e68a81954b9c931 -r 9fdf721e1ba10ffafd5a3c37e535809e1682cbb9 test/qunit/tests/metrics-logger.js --- a/test/qunit/tests/metrics-logger.js +++ b/test/qunit/tests/metrics-logger.js @@ -17,7 +17,9 @@ self.lastMessage = null; [ 'log', 'debug', 'info', 'warn', 'error' ].forEach( function( fnName ){ self[ fnName ] = function(){ - self.lastMessage = { level: fnName, args: Array.prototype.slice.call( arguments, 0 ) }; + var args = Array.prototype.slice.call( arguments, 0 ); + //console.debug( 'MockConsole:', fnName, JSON.stringify( args ) ); + self.lastMessage = { level: fnName, args: args }; }; }); }; @@ -171,6 +173,70 @@ server.restore(); }); + // ------------------------------------------------------------------------ Emit to console + test( "emit to console at level", function() { + var mockConsole = new MockConsole(), + logger = new metrics.MetricsLogger({ + consoleLevel : 'debug', + consoleLogger : mockConsole + }); + equal( logger.options.consoleLevel, metrics.MetricsLogger.DEBUG ); + equal( logger.consoleLogger.constructor, MockConsole ); + + logger.emit( 'debug', 'test', [ 1, 2, { three: 3 }] ); + equal( logger.cache.length(), 0 ); + //console.debug( JSON.stringify( mockConsole.lastMessage ) ); + equal( mockConsole.lastMessage.level, 'debug' ); + equal( mockConsole.lastMessage.args.length, 4 ); + equal( mockConsole.lastMessage.args[0], 'test' ); + equal( mockConsole.lastMessage.args[3].three, 3 ); + }); + + test( "emit to console below does not output", function() { + var mockConsole = new MockConsole(), + logger = new metrics.MetricsLogger({ + consoleLevel : 'error', + consoleLogger : mockConsole + }); + logger.emit( 'debug', 'test', [ 1, 2, { three: 3 }] ); + equal( mockConsole.lastMessage, null ); + }); + + // ------------------------------------------------------------------------ Shortcuts + test( "logger shortcuts emit to default namespace properly", function() { + var logger = new metrics.MetricsLogger({ + logLevel : 'all' + }); + equal( logger.options.logLevel, metrics.MetricsLogger.ALL ); + logger.log( 0 ); + logger.debug( 1 ); + logger.info( 2 ); + logger.warn( 3 ); + logger.error( 4 ); + logger.metric( 5 ); + + equal( logger.cache.length(), 6 ); + var cached = logger.cache.remove( 6 ).map( JSON.parse ), + entry; + + cached.forEach( function( entry ){ + ok( entry.namespace === logger.options.clientPrefix + logger.options.defaultNamespace ); + ok( jQuery.type( entry.args ) === 'array' ); + ok( typeof entry.time === 'string' ); + }); + + // log is different + entry = cached[0]; + ok( entry.level === 1 ); + ok( entry.args[0] === 0 ); + + [ 'debug', 'info', 'warn', 'error', 'metric' ].forEach( function( level, i ){ + entry = cached[( i + 1 )]; + ok( entry.level === logger._parseLevel( level ) ); + ok( entry.args[0] === ( i + 1 ) ); + }); + }); + // ======================================================================== LoggingCache test( "cache construction/initializiation defaults", function() { Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.
participants (1)
-
commits-noreply@bitbucket.org