Files
galaxy/static/scripts/utils/metrics-logger.js
T

1 line
6.4 KiB
JavaScript

define("utils/metrics-logger",["exports"],function(e){"use strict";function t(e){e=e||{};var t=this;return t.userId=window.bootstrapped&&window.bootstrapped.user?window.bootstrapped.user.id:null,t.userId=t.userId||e.userId||null,t.consoleLogger=e.consoleLogger||null,t._init(e),t}function o(e){var t=String(e);return"[object Object]"==t&&(t=JSON.stringify(e)),t}function n(e){return this._init(e||{})}Object.defineProperty(e,"__esModule",{value:!0});var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};t.ALL=0,t.LOG=0,t.DEBUG=10,t.INFO=20,t.WARN=30,t.ERROR=40,t.METRIC=50,t.NONE=100,t.defaultOptions={logLevel:t.NONE,consoleLevel:t.NONE,defaultNamespace:"Galaxy",consoleNamespaceWhitelist:null,consoleFlattenMessages:!1,clientPrefix:"client.",maxCacheSize:3e3,postSize:1e3,addTime:!0,cacheKeyPrefix:"logs-",postUrl:"/api/metrics",delayPostInMs:6e5,getPingData:void 0,onServerResponse:void 0},t.prototype._init=function(e){var o=this;o.options={};for(var n in t.defaultOptions)t.defaultOptions.hasOwnProperty(n)&&(o.options[n]=e.hasOwnProperty(n)?e[n]:t.defaultOptions[n]);return o.options.logLevel=o._parseLevel(o.options.logLevel),o.options.consoleLevel=o._parseLevel(o.options.consoleLevel),o._sending=!1,o._waiting=null,o._postSize=o.options.postSize,o._initCache(),o},t.prototype._initCache=function(){try{this.cache=new n({maxSize:this.options.maxCacheSize,key:this.options.cacheKeyPrefix+this.userId})}catch(e){this._emitToConsole("warn","MetricsLogger",["Could not intitialize logging cache:",e]),this.options.logLevel=t.NONE}},t.prototype._parseLevel=function(e){var o=void 0===e?"undefined":i(e);if("number"===o)return e;if("string"===o){var n=e.toUpperCase();if(t.hasOwnProperty(n))return t[n]}throw new Error("Unknown log level: "+e)},t.prototype.emit=function(e,t,o){var n=this;return t=t||n.options.defaultNamespace,e&&o?((e=n._parseLevel(e))>=n.options.logLevel&&n._addToCache(e,t,o),n.consoleLogger&&e>=n.options.consoleLevel&&n._emitToConsole(e,t,o),n):n},t.prototype._addToCache=function(e,t,o){this._emitToConsole("debug","MetricsLogger",["_addToCache:",arguments,this.options.addTime,this.cache.length()]);var n=this;try{n.cache.add(n._buildEntry(e,t,o))>=n._postSize&&n._postCache()}catch(e){n._emitToConsole("warn","MetricsLogger",["Metrics logger could not stringify logArguments:",t,o]),n._emitToConsole("error","MetricsLogger",[e])}return n},t.prototype._buildEntry=function(e,t,o){this._emitToConsole("debug","MetricsLogger",["_buildEntry:",arguments]);var n={level:e,namespace:this.options.clientPrefix+t,args:o};return this.options.addTime&&(n.time=(new Date).toISOString()),n},t.prototype._postCache=function(e){if(e=e||{},this._emitToConsole("info","MetricsLogger",["_postCache",e,this._postSize]),!this.options.postUrl||this._sending)return jQuery.when({});var t=this,o=e.count||t._postSize,n=t.cache.get(o),i=n.length,r="function"==typeof t.options.getPingData?t.options.getPingData():{};return r.metrics=JSON.stringify(n),t._sending=!0,jQuery.post(t.options.postUrl,r).always(function(){t._sending=!1}).fail(function(e,o,n){t._postSize=t.options.maxCacheSize,t.emit("error","MetricsLogger",["_postCache error:",e.readyState,e.status,e.responseJSON||e.responseText])}).done(function(e){"function"==typeof t.options.onServerResponse&&t.options.onServerResponse(e),t.cache.remove(i),t._postSize=t.options.postSize})},t.prototype._delayPost=function(){var e=this;e._waiting=setTimeout(function(){e._waiting=null},e.options.delayPostInMs)},t.prototype._emitToConsole=function(e,n,i){var r=this,s=r.options.consoleNamespaceWhitelist;if(!r.consoleLogger)return r;if(s&&-1===s.indexOf(n))return r;var a=Array.prototype.slice.call(i,0);return a.unshift(n),r.options.consoleFlattenMessages&&(a=[a.map(o).join(" ")]),e>=t.METRIC&&"function"==typeof r.consoleLogger.info?r.consoleLogger.info.apply(r.consoleLogger,a):e>=t.ERROR&&"function"==typeof r.consoleLogger.error?r.consoleLogger.error.apply(r.consoleLogger,a):(e>=t.WARN&&"function"==typeof r.consoleLogger.warn?r.consoleLogger.warn.apply(r.consoleLogger,a):e>=t.INFO&&"function"==typeof r.consoleLogger.info?r.consoleLogger.info.apply(r.consoleLogger,a):e>=t.DEBUG&&"function"==typeof r.consoleLogger.debug?r.consoleLogger.debug.apply(r.consoleLogger,a):"function"==typeof r.consoleLogger.log&&r.consoleLogger.log.apply(r.consoleLogger,a),r)},t.prototype.log=function(){this.emit(1,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))},t.prototype.debug=function(){this.emit(t.DEBUG,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))},t.prototype.info=function(){this.emit(t.INFO,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))},t.prototype.warn=function(){this.emit(t.WARN,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))},t.prototype.error=function(){this.emit(t.ERROR,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))},t.prototype.metric=function(){this.emit(t.METRIC,this.options.defaultNamespace,Array.prototype.slice.call(arguments,0))},n.defaultOptions={maxSize:5e3},n.prototype._init=function(e){if(!this._hasStorage())throw new Error("LoggingCache needs localStorage");if(!e.key)throw new Error("LoggingCache needs key for localStorage");return this.key=e.key,this._initStorage(),this.maxSize=e.maxSize||n.defaultOptions.maxSize,this},n.prototype._hasStorage=function(){try{return localStorage.setItem("test","test"),localStorage.removeItem("test"),!0}catch(e){return!1}},n.prototype._initStorage=function(){return null===localStorage.getItem(this.key)?this.empty():this},n.prototype.add=function(e){var t=this,o=t._fetchAndParse(),n=o.length+1-t.maxSize;return n>0&&o.splice(0,n),o.push(e),t._unparseAndStore(o),o.length},n.prototype._fetchAndParse=function(){var e=this;return JSON.parse(localStorage.getItem(e.key))},n.prototype._unparseAndStore=function(e){var t=this;return localStorage.setItem(t.key,JSON.stringify(e))},n.prototype.length=function(){return this._fetchAndParse().length},n.prototype.get=function(e){return this._fetchAndParse().slice(0,e)},n.prototype.remove=function(e){var t=this._fetchAndParse(),o=t.splice(0,e);return this._unparseAndStore(t),o},n.prototype.empty=function(){return localStorage.setItem(this.key,"[]"),this},n.prototype.stringify=function(e){return JSON.stringify(this.get(e))},n.prototype.print=function(){console.log(JSON.stringify(this._fetchAndParse(),null," "))},e.default={MetricsLogger:t,LoggingCache:n}});