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

3 lines
6.3 KiB
JavaScript

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