Geovisualization Developer's Guide

Query Statistics

The query statistics request body is a JSON object sent in a Geovisualization REST API request to retrieve statistics about the data resulting from a query.

Table 1. Query Statistics Request Elements
Element Data Type Mandatory Description
stats Object Yes A nested list containing two objects:
  • column_stats – a property with column names, each of which is a facet or metric defined in the query, followed by a list of statistics to be run on them: <column_name>: <list of stats functions>
    The statistics functions available are the following:
    • $min
    • $max
    • $count_distinct
    • $count
    • $average
    • $sum
  • dynamic – a property with dynamic parameters, each of which is defined in the query, or $drop to remove that parameter from the filters: <dynamic_parameter>: <value>

The code example below illustrates a typical column_stats object.

"column_stats": {
      "metric_a": ["$max", "$min", "$average"],
      "facet_a": ["$count_distinct"]
}

The code example below illustrates a typical dynamic object. The following would remove both the tx and ty dynamic parameters from the query and set the zoom to 10:

"dynamic": {
        "tx": "$drop",
        "ty": "$drop",
        "zoom": 10
}

The following example shows the request body to get the average depth of earthquakes contained in the earthquake dataset in the Geovisualization starter pack (with dataset ID 90af668484394fa782cc103409cafe39), when using the following query:

{
  "origin": "user",
  "description": "Earthquake data from 2012 - Absolute value of depths",
  "tags": [
    "depths",
    "earthquakes"
  ],
  "updated_at": "2016-01-15T09:34:09Z",
  "visibility": "private",
  "dataset": "90af668484394fa782cc103409cafe39",
  "query": {
    "metrics": [
      "absolute_value"
    ],
    "version": 0.3,
    "namespace": {
      "absolute_value": {
        "source": [
          "Depth"
        ],
        "apply": [
          {
            "type": "transform",
            "fn": "abs"
          }
        ]
      }
    },
    "dataset": "90af668484394fa782cc103409cafe39"
  },
  "id": "f943d8f5f4e247df8b53e318ee4ee60b",
  "kind": "datalens#query",
  "created_at": "2016-01-15T09:34:09Z",
  "estimated_rows": 1076
}

The request body would be:

  {
  "stats": [
    {
      "column_stats": {
      "absolute_value": ["$average"]
      },
    }
  ]
}

The response body would be:

{
  "query_id": "f943d8f5f4e247df8b53e318ee4ee60b",
  "stats": [
    {
      "column_stats": {
        "absolute_value": {
          "$average": 29.479739777
        }
      }
    }
  ]
}

If you were to change the value $average to $max, then the stats response contains the maximum depth of the earthquakes in the dataset.