torque/dist/torque.js

27 lines
20 KiB
JavaScript
Raw Normal View History

2013-07-30 16:45:35 +08:00
// =================
// profiler
// =================
//
// Counters
// pendingJobs.inc();
// pendingJobs.dec();
//
// Meters
// A meter measures the rate of events over time
// requests.mark();
//
// Histograms
// responseSizes.update(response.getContent().length);
//
// Timers
// private final Timer responses = metrics.timer(name(RequestHandler.class, "responses"));
//
// final Timer.Context context = responses.time();
//try {
//return "OK";
//} finally {
//context.stop();
//}
// Health Checks
//
function Profiler(){}Profiler.times={},Profiler.new_time=function(e,t){var n=Profiler.times[e]=Profiler.times[e]||{max:0,min:1e7,avg:0,total:0,count:0};n.max=Math.max(n.max,t),n.total+=t,n.min=Math.min(n.min,t),++n.count,n.avg=n.total/n.count},Profiler.print_stats=function(){for(k in Profiler.times){var e=Profiler.times[k];console.log(" === "+k+" === "),console.log(" max: "+e.max),console.log(" min: "+e.min),console.log(" avg: "+e.avg),console.log(" total: "+e.total)}},Profiler.get=function(e){return{t0:null,start:function(){this.t0=(new Date).getTime()},end:function(){this.t0!==null&&(Profiler.new_time(e,this.time=(new Date).getTime()-this.t0),this.t0=null)}}},function(e){function n(e){for(var t=1;t<arguments.length;++t){var n=arguments[t];for(var r in n)e=e.replace(RegExp("\\{"+r+"\\}","g"),n[r])}return e}e.torque=e.torque||{};var t=e.torque.providers=e.torque.providers||{},r=function(e){this._ready=!1,this._tileQueue=[],this.options=e;if(e.resolution===undefined)throw new Error("resolution should be provided");if(e.steps===undefined)throw new Error("steps should be provided");e.start===undefined?this.getKeySpan():this._ready=!0};r.prototype={proccessTile:function(e,t,n){var r=new Uint8Array(e.length),i=new Uint8Array(e.length),s=0,o=0;for(var u=0;u<e.length;++u){var a=e[u];s+=a.dates__uint16.length,o=Math.max(o,a.dates__uint16.length)}var f=new Int32Array(o),l=new Int32Array(o),c=new Uint8Array(s),h=new Uint32Array(s),p=[];for(var u=0;u<e.length;++u){var a=e[u];r[u]=a.x__uint8,i[u]=a.y__uint8;var s=e[u].dates__uint16,d=e[u].vals__uint8;for(var v=0,m=s.length;v<m;++v){var g=p[s[v]]||(p[s[v]]=[]);g.push([u,d[v]])}}var y=0,b=0;for(var w=0;w<o;++w){var E=0,S=p[w];if(S)for(var u=0;u<S.length;++u){var g=S[u];++E,h[y]=g[0],c[y]=g[1],++y}f[w]=b,l[w]=E,b+=E}return{x:r,y:i,coord:{x:t.x,y:t.y,z:n},timeCount:l,timeIndex:f,renderDataPos:h,renderData:c}},url:function(){return this.options.url||"http://"+this.options.user+".cartodb.com/api/v2/sql"},sql:function(e,t,n){n=n||{},torque.net.get(this.url()+"?q="+encodeURIComponent(e),function(e){n.parseJSON&&(e=JSON.parse(e.responseText)),t(e)})},getTileData:function(e,t,n){this._ready?this._getTileData(e,t,n):this._tileQueue.push([e,t,n])},_setReady:function(e){this._ready=!0,this._processQueue()},_processQueue:function(){var e;while(e=this._tileQueue.pop())this._getTileData.apply(this,e)},_getTileData:function(e,t,r){this.table=this.options.table;var i=1<<t,s=this.options.column;this.options.is_time&&(s=n("date_part('epoch', {column})",this.options));var o="WITH par AS ( SELECT CDB_XYZ_Resolution({zoom})*{resolution} as res, CDB_XYZ_Extent({x}, {y}, {zoom}) as ext ),cte AS ( SELECT ST_SnapToGrid(i.the_geom_webmercator, p.res) g, {countby} c, floor(({column_conv} - {start})/{step}) d FROM {table} i, par p WHERE i.the_geom_webmercator && p.ext GROUP BY g, d) SELECT least((st_x(g)-st_xmin(p.ext))/p.res, 255) x__uint8, least((st_y(g)-st_ymin(p.ext))/p.res, 255) y__uint8, array_agg(c) vals__uint8, array_agg(d) dates__uint16 FROM cte, par p GROUP BY x__uint8, y__uint8",u=n(o,this.options,{zoom:t,x:e.x,y:e.y,column_conv:s}),a=this;this.sql(u,function(n){var i=JSON.parse(n.responseText).rows;r(a.proccessTile(i,e,t))})},getKeySpan:function(){var e,t,r,i;this.options.is_time?(r="date_part('epoch', max({column}))",i="date_part('epoch', min({column}))"):(r="max({0})",i="min({0})"),e=n(r,{column:this.options.column}),t=n(i,{column:this.options.column});var s=n("SELECT st_xmax(st_envelope(st_collect(the_geom))) xmax,st_ymax(st_envelope(st_collect(the_geom))) ymax, st_xmin(st_envelope(st_collect(the_geom))) xmin, st_ymin(st_envelope(st_collect(the_geom))) ymin, {max_col} max, {min_col} min FROM {table}",{max_col:e,min_col:t,table:this.options.table}),o=this;this.sql(s,function(e){e=e.rows[0],o.options.start=e.min,o.options.step=(e.max-e.min)/o.options.steps,o._setReady(!0)},{parseJSON:!0})}},torque.providers.json=r}(typeof exports=="undefined"?this:exports),function(e){function n(e,t){for(var n=1;n<arguments.length;++n){var t=arguments[n];for(var r in t)e=e.replace(RegExp("\\{"+r+"\\}