Skip to content

Instantly share code, notes, and snippets.

@davehughes
Last active March 29, 2017 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davehughes/8bebfa9c589fae00ca90fdc26addd79c to your computer and use it in GitHub Desktop.
Save davehughes/8bebfa9c589fae00ca90fdc26addd79c to your computer and use it in GitHub Desktop.
Sketch of basic metric rollup table and queries

This is a description of a scheme I've used a few times for representing time series data on a fairly small scale. In a nutshell, it uses a single table to house multiple data series on multiple scales and is designed to make it easy to display those series.

Sample Data:

series timescale timestamp value
active-users weekly 2016-03-05 487
active-users daily 2016-03-05 75
active-users daily 2016-03-06 43
active-users daily 2016-03-07 51
active-users daily 2016-03-08 64
active-users daily 2016-03-09 77
active-users daily 2016-03-10 103
active-users daily 2016-03-11 74
... ... ... ...

Or in SQL (I'm using Postgres):

CREATE TABLE series_rollups (
    series VARCHAR(256),
    timescale VARCHAR(20),
    timestamp TIMESTAMP,
    value INTEGER
);

INSERT INTO series_rollups (series, timescale, timestamp, value) VALUES
('active-users', 'weekly', '2016-03-05', 487),
('active-users', 'daily', '2016-03-05', 75),  
('active-users', 'daily', '2016-03-06', 43),  
('active-users', 'daily', '2016-03-07', 51),  
('active-users', 'daily', '2016-03-08', 64),  
('active-users', 'daily', '2016-03-09', 77),  
('active-users', 'daily', '2016-03-10', 103), 
('active-users', 'daily', '2016-03-11', 74);

The basic query form is:

SELECT timestamp, value
FROM series_rollups
WHERE 1=1
   AND series = 'active-users'
   AND timescale = 'daily'
   AND timestamp >= '2016-03-05'
   AND timestamp < '2016-03-12'
ORDER BY timestamp ASC

which can be modified to select data points from any series/timescale/date range. For all new-users by week in 2016, we can change the query to:

SELECT timestamp, value
FROM series_rollups
WHERE 1=1
   AND series = 'new-users'
   AND timescale = 'weekly'
   AND timestamp >= '2016-01-01'
   AND timestamp < '2017-01-01'
ORDER BY timestamp ASC

Considerations and Complexities

  • Timestamp representation - TODO
  • Time period selection for display - TODO
  • Tagging and subsets - TODO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment