Skip to content

Instantly share code, notes, and snippets.

@rochoa
rochoa / tile.sql
Created November 8, 2017 15:22 — forked from javisantana/tile.sql
CREATE OR REPLACE FUNCTION tile (z integer, x integer, y integer, query text) RETURNS TABLE(id int8, geom geometry)
AS $$
DECLARE
sql TEXT;
BEGIN
sql := 'with _conf as (
select
CDB_XYZ_resolution(' || z || ') as res,
1.0/CDB_XYZ_resolution(' || z || ') as invres,
st_xmin(CDB_XYZ_Extent(' || x || ',' || y || ',' || z ||')) as tile_x,

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@rochoa
rochoa / dot_density.sql
Last active February 14, 2017 16:58 — forked from stuartlynn/dot_density.sql
dot_density.sql
-- Source code from https://github.com/CartoDB/crankshaft/blob/develop/src/pg/sql/30_dot_density.sql
CREATE OR REPLACE FUNCTION cdb_dot_density(geom geometry , no_points Integer, max_iter_per_point Integer DEFAULT 1000)
RETURNS GEOMETRY AS $$
DECLARE
extent GEOMETRY;
test_point Geometry;
width NUMERIC;
height NUMERIC;
x0 NUMERIC;
y0 NUMERIC;
@rochoa
rochoa / cdb_delayed_invalidate.sql
Created January 22, 2016 14:21 — forked from pramsey/cdb_delayed_invalidate.sql
Delayed Invalidation Trigger
CREATE OR REPLACE FUNCTION public.CDB_Delayed_TableMetadata_Trigger()
RETURNS trigger AS $$
DECLARE upd timestamp;
BEGIN
IF TG_RELID = 'cartodb.CDB_TableMetadata'::regclass::oid THEN
RETURN NULL;
END IF;
SELECT updated_at INTO upd
FROM cartodb.CDB_TableMetadata WHERE tabname = TG_RELID::regclass;
IF upd IS NOT NULL AND (now() - upd) > '20m'::interval THEN
@rochoa
rochoa / README.md
Last active September 10, 2015 13:35 — forked from tyrasd/README.md
Map & Globe

modified version of http://bl.ocks.org/curran/01aa2685f083b6c1b9fb with the map's bounding box on the globe

original readme follows:

--

Pan and zoom in the map on the left to rotate the globe.

Click and drag the globe to pan on the map.

@rochoa
rochoa / speed.sql
Last active August 29, 2015 14:27 — forked from javisantana/speed.sql
speed from a track table
WITH deltas as (
SELECT
st_distance(the_geom::geography, lag(the_geom::geography, 1) over(order by timestamp)) as ddist,
timestamp - lag(timestamp, 1) over(order by timestamp) as dt
from out_2 offset 1000
)
select avg(ddist/dt) as speed from deltas
@rochoa
rochoa / index.html
Last active March 16, 2017 13:00 — forked from rochoa/index.html
[CARTO] CartoDB.js + Leaflet interactivity example
<!DOCTYPE html>
<html>
<head>
<title>CartoDB + Leaflet interactivity example | CartoDB.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;