CartoDB / Windshaft-cartodb

Windshaft tailored for CARTO
BSD 3-Clause "New" or "Revised" License
72 stars 59 forks source link

Named map auto-view does not seem to work with usernames with hyphens #734

Open andy-esch opened 7 years ago

andy-esch commented 7 years ago

I have a hunch that the hyphens are the problem here, but I don't know how to look into it very easily so thought I'd open a ticket about my bug hunch. I've seen that on all carto accounts with a hyphen in the username, the cartoframes static map output is zoomed out to the same level, but the un-hyphenated ones behave as expected from the docs:

A Named Maps static image will get its constraints from the view argument of the Create Named Map function. If view is not defined, it will estimate the extent based on the involved tables, otherwise it fallbacks to "zoom": 1, "lng": 0 and "lat": 0.

What seems weird to me, though, is that the default bounding box view is not being used as far as I can tell.

Compare this: https://lwolf-carto.carto.com/api/v1/map/static/named/cartoframes_ver20170406_layers1_time0_baseid1_labels0_zoom0/800/400.png?config=%7B%22sql_0%22%3A+%22SELECT+%2A+FROM+nat%22%7D

to this:

https://ljw.carto.com/api/v1/map/static/named/cartoframes_ver20170406_layers1_time0_baseid1_labels0_zoom0/800/400.png?config=%7B%22sql_0%22%3A+%22SELECT+%2A+FROM+nat%22%7D

It uses the same dataset and same named map template:

{
  "template": {
    "auth": {
      "method": "open"
    },
    "version": "0.0.1",
    "name": "cartoframes_ver20170406_layers1_time0_baseid1_labels0_zoom0",
    "placeholders": {
      "cartocss_0": {
        "type": "sql_ident",
        "default": "#layer { marker-fill: red; marker-width: 5; marker-allow-overlap: true; marker-line-color: #000; }"
      },
      "sql_0": {
        "type": "sql_ident",
        "default": "SELECT ST_PointFromText('POINT(0 0)', 4326) as the_geom, 1 as cartodb_id, ST_PointFromText('Point(0 0)', 3857) as the_geom_webmercator"
      },
      "west": {
        "type": "number",
        "default": -45
      },
      "south": {
        "type": "number",
        "default": -45
      },
      "east": {
        "type": "number",
        "default": 45
      },
      "north": {
        "type": "number",
        "default": 45
      }
    },
    "layergroup": {
      "version": "1.0.1",
      "layers": [
        {
          "type": "http",
          "options": {
            "urlTemplate": "https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png",
            "subdomains": "abcd"
          }
        },
        {
          "type": "mapnik",
          "options": {
            "cartocss_version": "2.1.1",
            "cartocss": "<%= cartocss_0 %>",
            "sql": "<%= sql_0 %>"
          }
        }
      ]
    },
    "view": {
      "bounds": {
        "west": "<%= west %>",
        "south": "<%= south %>",
        "east": "<%= east %>",
        "north": "<%= north %>"
      }
    }
  }
}

cc @rochoa

rochoa commented 7 years ago

@andy-esch, nice one you caught here! 💯

First of all, the view attribute doesn't support variables, so your placeholders won't get substituted with your default values. That means your bounds attribute will be ignored and will fall back to the estimation.

Second, and most important: "it will estimate the extent based on the involved table", the key is the estimate. There are several reasons why the estimation might fail: no tables associated with the query, not stats for the tables, wrong stats on the tables. But none of those is the scenario in your case. Let me explain.

The process is as follows: we compute the tables associated with the layer query, with those tables we calculate the maximum extent using the ST_EstimatedExtent of each table. The problem is, as you pointed, with usernames with hyphens, well not that simple, but with usernames with hyphens for users that belong to multi user accounts (aka organizations). Normal users have their tables in the public schema. However, organization users have their tables in their own schema, that has the same name as the user.

When we compute the tables associated with the query of the layer, we return the schema name as well to avoid issues with table names collisions and to use the ST_EstimatedExtent(text schema_name, text table_name, text geocolumn_name); signature. But we return the schema name already quoted if required, so in a non-org user with hyphens, the schema name will be public, but in an organization user with hyphens, the schema name will be something like "user-name", which used in combination with ST_EstimatedExtent won't work.

I've written all this down so we can investigate further where we should make the proper changes/fixes, as I'm not sure if we should fix it at our application/queries, or ST_EstimatedExtent should handle quoted schema (table) names.

dgaubert commented 6 years ago

Hey @rochoa

First of all, thanks for the detailed explanation, super useful!!. The second, now that Core Team take care of this kind of issues, do you think it'd be better to open this issue in postgis' repository?