From 7d4caf69740f174f43f89637cfa97f9b4055c8b6 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 6 May 2016 16:37:52 +0200 Subject: [PATCH 1/2] Analysis layers can have a sql_wrap option to wrap node queries --- .../models/analysis-mapconfig-adapter.js | 7 ++- test/acceptance/analysis/analysis-layers.js | 40 ++++++++++++++++++ .../adm0cap-source-id-mapnik-layer.png | Bin 0 -> 10627 bytes 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/analysis/adm0cap-source-id-mapnik-layer.png diff --git a/lib/cartodb/models/analysis-mapconfig-adapter.js b/lib/cartodb/models/analysis-mapconfig-adapter.js index 08050287..c7e788f9 100644 --- a/lib/cartodb/models/analysis-mapconfig-adapter.js +++ b/lib/cartodb/models/analysis-mapconfig-adapter.js @@ -139,7 +139,12 @@ AnalysisMapConfigAdapter.prototype.getMapConfig = function(analysisConfiguration var layerSourceId = getLayerSourceId(layer); var layerNode = sourceId2Node[layerSourceId]; if (layerNode) { - layer.options.sql = layerQuery(layerNode.getQuery(), layerNode.getColumns()); + var analysisSql = layerQuery(layerNode.getQuery(), layerNode.getColumns()); + var sqlQueryWrap = layer.options.sql_wrap; + if (sqlQueryWrap) { + analysisSql = sqlQueryWrap.replace(/<%=\s*__analysis_query\s*%>/g, analysisSql); + } + layer.options.sql = analysisSql; var layerDataviews = getLayerDataviews(layer, dataviews); layer.options.columns = layerDataviews.reduce(function(columns, dataview) { return columns.concat(getDataviewColumns(dataview)); diff --git a/test/acceptance/analysis/analysis-layers.js b/test/acceptance/analysis/analysis-layers.js index 826c6604..560c938b 100644 --- a/test/acceptance/analysis/analysis-layers.js +++ b/test/acceptance/analysis/analysis-layers.js @@ -327,4 +327,44 @@ describe('analysis-layers', function() { testClient.drain(done); }); }); + + it('should wrap queries from analyses', function(done) { + var testClient = new TestClient(mapConfig( + [ + { + "type": "cartodb", + "options": { + "source": { + "id": "2570e105-7b37-40d2-bdf4-1af889598745" + }, + "sql_wrap": "SELECT * FROM (<%= __analysis_query %>) __wrapped WHERE adm0cap = 1", + "cartocss": DEFAULT_MULTITYPE_STYLE, + "cartocss_version": "2.3.0" + } + } + ], + {}, + [ + { + "id": "2570e105-7b37-40d2-bdf4-1af889598745", + "type": "source", + "params": { + "query": "select * from populated_places_simple_reduced" + } + } + ] + ), 1234); + + var tile = TILE_ANALYSIS_TABLES; + testClient.getTile(tile.z, tile.x, tile.y, function(err, res, image) { + assert.ok(!err, err); + + var fixturePath = './test/fixtures/analysis/adm0cap-source-id-mapnik-layer.png'; + assert.imageIsSimilarToFile(image, fixturePath, IMAGE_TOLERANCE_PER_MIL, function(err) { + assert.ok(!err, err); + + testClient.drain(done); + }); + }); + }); }); diff --git a/test/fixtures/analysis/adm0cap-source-id-mapnik-layer.png b/test/fixtures/analysis/adm0cap-source-id-mapnik-layer.png new file mode 100644 index 0000000000000000000000000000000000000000..59d237c3a3b419796a3647593513d80477fa984f GIT binary patch literal 10627 zcmd6NRaYBKxOITwUaUBj0xjE>~D;}f}++B*h6?b?cKp=em z&Tlvu=VoTsT+PgS)}Fnez2mesmGNZ&($7F}8fZeq(xq2}xa4A;i~p6(v1*NKEFedq!f28ric;Xe z)4dw<{9Q2NrN0X)Z~u7+cP!_c(xby?|8Rj*i=YcEgYvm=_A}qfI+C8@&MAJPVofj2Iwg%V*Ou0TAnS*{s<=y3v4)dRY8B20LLF8gz z89Qa9r9pFk2760*;l`H^jMN5K@Ohpj`5>c<=(%f1Cy0?r*?$smh~?Coe!#$7EMK+P z=3~xb;*>tG+Ytdi()1P`*V?dJMJ!&~rPdQK+`ODk-l>nF|?)d)xU^ck!y_P+BYr6teg}QiZ447e3$TYD6#ja9i6B zEr&3a-{2S02JX7DFg!{HWIN-ud)Pg%;E=`BBxu`QC{I;BOO`R>Nq78b?~L~ER$q=h zBcg^7v1f~ZcT*C5jyxdbY_9zrxE1YP%16dog2MjUk)l?wLN6Tok*F0$c+#+zuX#bcLh;se*&$<3!u_#V~MOzasTs{_r zh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(5}FT9Odi)|;g=^1P(A|uIq{{C}|tEJHyo^y;Rse_sT zp?1qqiW@{N&WkSUKXFC8z^mN_$Dl{l7p z5NORmww+%J-l~p%0?(4FPOZZnHGX(Hxw8cB*k&A^X0j{H#@$ZXe#edB1%N{FCQG)$ z7l-!`lG9w)Ctw9%QXpScuHvv;jv3UwA2YB4xonO1VJN*?XPwdR%)U#!S7iwWMKyz? z+)Nz@&jc5GA@v!~R@Rzzg~_2(xc}LlEO1Zz?+wNK?OUdw!j-d}Pg}Y`Vk80uU~7E; z^VSnVf3#x7ZDqF7>;&gRYc!1Zxk;{4BBpsN@ehNCV$w3seA8=Pb)^Lh9jsNPn$ZjO~vUZM(~9ft`sS`&Zp)yzO@+aV0qIN+qI1Ev(7_x9p?74Ola99T)HX z*1F1rqWx9xP0=dGlY$}j!X*+8Dy7CQp@3-;V*ovUjUa z!U_I>&1izB!~V6E<5l;!vR^f)1)S+MQAFOAw3%-H{CfFwS&IL6BYyuo>BC}@hY$pM X4b^D9o9n-DB|ueCQ=vxAD*XQeE9-v= literal 0 HcmV?d00001 From 3e94e3288fa6a75cbb78ebacc06ac7f0355c4e07 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 6 May 2016 17:09:41 +0200 Subject: [PATCH 2/2] Use sql as replacement variable --- lib/cartodb/models/analysis-mapconfig-adapter.js | 2 +- test/acceptance/analysis/analysis-layers.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cartodb/models/analysis-mapconfig-adapter.js b/lib/cartodb/models/analysis-mapconfig-adapter.js index c7e788f9..2897114b 100644 --- a/lib/cartodb/models/analysis-mapconfig-adapter.js +++ b/lib/cartodb/models/analysis-mapconfig-adapter.js @@ -142,7 +142,7 @@ AnalysisMapConfigAdapter.prototype.getMapConfig = function(analysisConfiguration var analysisSql = layerQuery(layerNode.getQuery(), layerNode.getColumns()); var sqlQueryWrap = layer.options.sql_wrap; if (sqlQueryWrap) { - analysisSql = sqlQueryWrap.replace(/<%=\s*__analysis_query\s*%>/g, analysisSql); + analysisSql = sqlQueryWrap.replace(/<%=\s*sql\s*%>/g, analysisSql); } layer.options.sql = analysisSql; var layerDataviews = getLayerDataviews(layer, dataviews); diff --git a/test/acceptance/analysis/analysis-layers.js b/test/acceptance/analysis/analysis-layers.js index 560c938b..a19128b0 100644 --- a/test/acceptance/analysis/analysis-layers.js +++ b/test/acceptance/analysis/analysis-layers.js @@ -337,7 +337,7 @@ describe('analysis-layers', function() { "source": { "id": "2570e105-7b37-40d2-bdf4-1af889598745" }, - "sql_wrap": "SELECT * FROM (<%= __analysis_query %>) __wrapped WHERE adm0cap = 1", + "sql_wrap": "SELECT * FROM (<%= sql %>) __wrapped WHERE adm0cap = 1", "cartocss": DEFAULT_MULTITYPE_STYLE, "cartocss_version": "2.3.0" }