-
Notifications
You must be signed in to change notification settings - Fork 8
/
js-hypercube.min.js
1 lines (1 loc) · 6.9 KB
/
js-hypercube.min.js
1
var ps=Object.create?Object.create(null):{};ps.addSingletonGetter=function(a){a.getInstance=function(){return a.instance_||a.setInstance.apply(this,arguments)};a.hasInstance=function(){return !!a.instance_};a.setInstance=function(){return a.instance_=new a(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6],arguments[7])};a.deleteInstance=function(){a.instance_=null}};ps.addCloning=function(a){a.prototype.clone=function(){var c=$.extend(true,new a(),this);var b=function(e){var d;for(name in e){d=e[name];if(!d instanceof Object){continue}else{if(d===e){continue}else{if(d.clone){e[name]=d.clone();continue}}}b(d)}};b(c);return c}};ps.inherits=function(b,a){function c(){}c.prototype=a.prototype;b.superClass_=a.prototype;b.prototype=new c();b.prototype.constructor=b};ps.time=function(){return Math.floor(ps.microTime()/1000)};ps.microTime=Date.now||(function(){return +new Date()});ps.timezone=function(){return Math.floor((new Date()).getTimezoneOffset()/60)*-1};ps.obj=function(){return Object.create?Object.create(null):{}};ps.isDef=function(a){return a!==undefined};ps.isNull=function(a){return a===null};ps.isDefAndNotNull=function(a){return a!=null};ps.isString=function(a){return typeof a=="string"};ps.isBoolean=function(a){return typeof a=="boolean"};ps.isNumber=function(a){return(typeof a=="number")&&!isNaN(a)&&isFinite(a)};ps.isFunction=function(a){return $.isFunction(a)};ps.isObject=function(a){return typeof a==="object"};ps.isPlainObject=function(a){return $.isPlainObject(a)};ps.isArray=function(a){return $.isArray(a)};ps.isElement=function(a){return ps.isObject(a)&&a.nodeType===1};ps.uid=function(){return ps._uidCounter++};ps._uidCounter=1000;ps.uniqueElementId=function(){return"_idgen_"+ps.uid()};ps.redirect=function(a){if(a){window.document.location.replace(a)}else{window.document.location.reload(true)}};ps.navigateTo=function(a){try{window.document.location.href=a}catch(b){}};ps.array={contains:function(d,c){for(var b=0,a=d.length;b<a;++b){if(d[b]===c){return true}}return false},unique:function(d){var c=[];for(var b=0,a=d.length;b<a;++b){if(!ps.array.contains(c,d[b])){c.push(d[b])}}return c},diff:function(f,e){var d=[],g,b=ps.array.pivot(e);for(var c=0,a=f.length;c<a;++c){g=f[c];if(!ps.isDef(b[g])){d.push(g)}}return d},intersect:function(e,d){var c=[];for(var b=0,a=e.length;b<a;++b){if(ps.array.contains(d,e[b])){c.push(e[b])}}return c},union:function(b,a){a=Array.prototype.slice.apply(arguments).slice(1);return Array.prototype.concat.apply(b,a)},clone:function(a){return ps.array.union([],a)},keys:function(b){var a=[];$.each(b,function(d,c){a.push(d)});return a},vals:function(b){var a=[];$.each(b,function(d,c){a.push(c)});return a},combine:function(d,a){var c=(d.length<=a.length?d.length:a.length),e=ps.obj();for(var b=0;b<c;++b){e[d[b]]=a[b]}return e},pivot:function(b){var a=ps.obj();$.each(b,function(d,c){a[c]=d});return a},randSort:function(a){a.sort(function(d,c){return 0.5-Math.random()});return a}};ps.object={contains:ps.array.contains,keys:ps.array.keys,vals:ps.array.vals,combine:ps.array.combine,fill:function(c,d){var e=ps.obj();for(var b=0,a=c.length;b<a;++b){e[c[b]]=d}return e}};ps.FactIndex=function(){this._slabs=ps.obj();this._factValues=[]};ps.FactIndex.prototype.getFactValues=function(){if(!this._factValues.length){this._factValues=ps.array.keys(this._slabs);this._factValues.sort()}return this._factValues};ps.FactIndex.prototype.get=function(a){return this._slabs[a]};ps.FactIndex.prototype.insert=function(c,b){this._factValues=[];var a=this._slabs[c];if(!ps.isDef(a)){a=[];this._slabs[c]=a}a.push(b)};ps.Cell=function(c,b,a){this.time=a?a:null;this.facts=c;this.measures=b};ps.addCloning(ps.Cell);ps.Cell.prototype.value=function(a){return ps.isNumber(this.measures[a])?this.measures[a]:0};ps.Cell.aggregate=function(b,d){var e=ps.obj();if(!b||!b.length){return e}for(var c=0,a=b.length;c<a;++c){$.each(b[c].measures,function(f,g){e[f]=d(e[f],g)})}return e};ps.Cell.getComparisonFn=function(a,b){return function(d,c){var e=d.value(a)-c.value(a);return b?e*-1:e}};ps.Cube=function(a){this._indecies=ps.obj();this._cells=[];this._factNames=[];this._measureNames=ps.isArray(a)?a:null};ps.Cube.prototype.count=function(){return this._cells.length};ps.Cube.prototype.getFactNames=function(){if(!this._factNames.length){this._factNames=ps.array.keys(this._indecies);this._factNames.sort()}return this._factNames};ps.Cube.prototype.getValues=function(d){var b=[];for(var c=0,a=this._cells.length;c<a;++c){value=this._cells[c].facts[d];if(value){b.push(value)}}return ps.array.unique(b)};ps.Cube.prototype.insert=function(b){this._factNames=[];var a=this._cells.length,c;$.each(b.facts,$.proxy(function(e,d){c=this._indecies[e];if(!ps.isDef(c)){c=new ps.FactIndex();this._indecies[e]=c}c.insert(d,a)},this));this._cells.push(b)};ps.Cube.prototype._getPos=function(d){var a,c,b;$.each(d,$.proxy(function(f,e){b=this._indecies[f];if(!ps.isDef(b)){a=[];return false}c=b.get(e);if(!ps.isDef(c)){a=[];return false}if(a){a=ps.array.intersect(a,c)}else{a=c}if(!a.length){a=[];return false}},this));return a};ps.Cube.prototype.slice=function(d){var e=new ps.Cube(this._measureNames),a=this._getPos(d);for(var c=0,b=a.length;c<b;++c){e.insert(this._cells[a[c]])}return e};ps.Cube.prototype.sliceDates=function(g,e){var a=new ps.Cube(this._measureNames),c=this._cells,f;for(var d=0,b=c.length;d<b;++d){if(c[d].time>=g&&c[d].time<e){a.insert(c[d])}}return a};ps.Cube.prototype.dice=function(e){var c=new ps.Cube(this._measureNames),a=this._getPos(e);a=ps.array.diff(ps.array.keys(this._cells),a);for(var d=0,b=a.length;d<b;++d){c.insert(this._cells[a[d]])}return c};ps.Cube.prototype.merge=function(a){for(var c=0,b=a._cells.length;c<b;++c){this.insert(a._cells[c])}};ps.Cube.prototype.sum=function(a){if(!this._cells.length){if(this._measureNames){return ps.object.fill(this._measureNames,0)}return ps.obj()}return ps.Cell.aggregate(this._cells,function(b,c){if(ps.isNumber(a)){c=parseFloat(c.toPrecision(a),10)}if(ps.isDef(b)){b+=c}else{b=c}return b})};ps.Cube.prototype.avg=function(d,a){var c=this.sum(a),e=ps.obj(),b=false;$.each(c,function(f,g){b=true;g=g?g/d:g;if(ps.isNumber(a)){g=parseFloat(g.toPrecision(a),10)}e[f]=g});if(!b&&this._measureNames){return ps.object.fill(this._measureNames,0)}return e};ps.Cube.prototype.serialize=function(){var d=[],e,a;for(var c=0,b=this._cells.length;c<b;++c){a=this._cells[c];e=ps.obj();e.facts=a.facts;e.measures=a.measures;d.push(e)}return d};ps.Cube.deserialize=function(f,e){var a=new ps.Cube(e),c;for(var d=0,b=f.length;d<b;++d){c=f[d];a.insert(new ps.Cell(c.facts,c.measures,c.time*1000))}return a};ps.Cube.transforms=ps.obj();ps.Cube.transforms.dateLocal=function(b){var a=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];$.each(b,function(e,c){if(!c.time||!c.facts){console.error("record is missing necessary properties. halting.",c);return false}var d=new Date(c.time*1000);c.facts.year=d.getFullYear();c.facts.month=d.getMonth()+1;c.facts.day=d.getDate();c.facts.hour=d.getHours();c.facts.day_of_week=a[d.getDay()]})};