newsdev/ap-deja-vu

Name: ap-deja-vu

Owner: NYT Newsroom Developers

Description: A small web service that will replay captured JSON from an AP election test.

Created: 2015-12-10 20:43:07.0

Updated: 2018-05-22 22:45:47.0

Pushed: 2018-02-23 16:10:14.0

Homepage: null

Size: 40

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

AP DEJA-VU

That strange feeling we sometimes get that we've lived through something before.

ap-deja-vu is a small web service that will replay JSON from an AP election test. ap-deja-vu does not require a database for persistence and does not write to the filesystem.

Important Notes
User interface

screen shot 2016-01-29 at 1 04 07 pm

Getting Started
Install requirements

ap-deja-vu requires a running redis instance on localhost. Install with homebrew (development on a Mac) or via apt-get (production use on an Ubuntu-based server).

Mac

Requires Homebrew.

 install redis
 services start redis
Ubuntu Linux
 apt-get install redis-server
Install this app
rtualenv ap-deja-vu && git clone git@github.com:newsdev/ap-deja-vu.git && cd ap-deja-vu
install -r requirements.txt
virtualenv ap_deja_vu
Run The Server
on -m ap_deja_vu.app
How It Works
Environment Variables

Export an AP_DEJAVU_DATA_DIRECTORY environment variable.

rt AP_DEJAVU_DATA_DIRECTORY=/tmp/ap-elex-data/
AP Elections Files

Your data directory should contain a series of folders corresponding to AP election dates. Within each of those folders, ap-deja-vu expects a series of files, named as UNIX timestamps. Really, any incrementing number or letter combination will work as long as the default sort produces the correct order. Each of these files should contain the output of the AP election API v2. Use this layout as a guide:

/
/ap-elex-data/
    /2016-02-01/
            /local/
                001.json
                002.json
                003.json
            /national/
                001.json
                002.json
                003.json
    /2016-02-09/
            /local/
                001.json
                002.json
                003.json
            /national/
                001.json
                002.json
                003.json

We recommend The New York Times's Elex-loader for capturing and parsing AP election data. Pay particular attention to the get_national_results and get_local_results functions in this script.

Playback

The route /<election_date> will replay the election files found in the folder /<DATA_DIR>/<election_date>/. The files should be named such that the first file will be sorted first in a list by glob.glob(), e.g., a higher letter (a) or lower number (0). Incrementing UNIX timestamps (such as those captured by Elex) would be ideal.

This route takes two optional control parameters. Once these have been passed to set up an election test, the raw URL will obey the instructions below until the last file in the hopper has been reached.

When the last file in the hopper has been reached, it will be returned until the Flask app is restarted OR a new pair of control parameters are passed.

Example: Let's say you would like to test an election at 10x speed. You have 109 files in your /<DATA_DIR>/<election_date>/ folder named 001.json through 109.json

Requesting /?position=0&playback=1&national=true will reset to the default position and playback speeds, respectively.

Status

The route //status will return the status of a given election date test, including the current position in the hopper, the playback speed, and the path of the file that will be served at the current position.


position: 22,
playback: 1,
file: "/tmp/ap-elex-data/2016-02-09/ap_elections_loader_recording-1449676507.json"


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.