-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinfluxdb.min.js
1 lines (1 loc) · 2.72 KB
/
influxdb.min.js
1
"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),Influxdb=function(){function a(b,c){return _classCallCheck(this,a),this.sendPointsOnClose=c,this.host=b,this.points=[],this.beaconSent=!1,this.sendPointsOnClose&&this.registerUnloadEvent(),this}return _createClass(a,[{key:"registerUnloadEvent",value:function(){var b=this;window.addEventListener("unload",function(a){b.sendBeacon(b)},!1),window.onbeforeunload=function(a){b.sendBeacon(b)}}},{key:"sendBeacon",value:function(b){if(!b.beaconSent&&(!b.points||0!=b.points.length)&&(b.beaconSent=!0,navigator||navigator.sendBeacon)){var c=b.implodePoints();navigator.sendBeacon(b.host,c)}}},{key:"point",value:function(b,c,d,e){return this._addPoint(new Influxdb_Point(b,c,d,e))}},{key:"_addPoint",value:function(b){return b.isValid()&&this.points.push(b),this}},{key:"implodePoints",value:function(){if(0==this.points.length)return"";var b,c="";for(b=0;b<this.points.length;++b)this.points[b].isValid()?c+=this.points[b].getLine():this.points.slice(b,1);return c}},{key:"send",value:function(){if(0==this.points.length)return!1;var b=this.implodePoints();if(b){var c=new XMLHttpRequest;c.open("POST",this.host,!0),c.setRequestHeader("Content-Type","text/plain; charset=UTF-8"),c.send(b)}this.points=[]}}]),a}(),Influxdb_Point=function(){function a(b,c,d,e){_classCallCheck(this,a),this.key=b,"undefined"==typeof c||this.isEmpty(c)||(this.fields=c),"undefined"==typeof d||this.isEmpty(c)||(this.tags=d),"undefined"!=typeof e&&(this.time=e)}return _createClass(a,[{key:"isValid",value:function(){return!(!this.key||!this.fields)}},{key:"getLine",value:function(){if(this.key||this.fields)return this.line=this.key,this.tags&&(this.line=this.line+","+this.objToString(this.tags)),this.fields&&(this.line=this.line+" "+this.objToString(this.fields)),this.time&&(this.line=this.line+" "+this.time),this.line=this.line+"\n",this.line}},{key:"sortObjectByKey",value:function(b){if(!Array.prototype.forEach)return b;if(!Array.prototype.sort)return b;if(!Object.keys)return b;var c={};return Object.keys(b).sort().forEach(function(a){c[a]=b[a]}),c}},{key:"objToString",value:function(b){var e,f,c=0,d="";b=this.sortObjectByKey(b);for(var e in b)b.hasOwnProperty(e)&&(e=e.replace(/\s+/g,""),f=b[e].toString(),f=f.replace(/\s+/g,""),c++,c>1&&(d+=","),d+=e+"="+f);return d}},{key:"isEmpty",value:function(b){for(var c in b)if(b.hasOwnProperty(c))return!1;return!window.JSON||JSON.stringify(b)===JSON.stringify({})}}]),a}();