Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

newsdev/elex-loader

Repository files navigation

AP ELECTION LOADER

Relies on Elex, a command-line tool to get results from the AP Election API 2.0. Demonstrates a method putting those results into a Postgres database using the COPY method and the loader's CSV output.

Assumptions

The following things are assumed to be true in this documentation.

  • You are running OSX.
  • You are using Python 2.7. (Probably the version that came OSX.)
  • You have pip, virtualenv and virtualenvwrapper installed and working.

See "Chapter 2: Install Virtualenv" of NPR's development environment blog post for details.

Having trouble on OS X El Capitan? See: Can't install virtualenvwrapper on OSX 10.11 El Capitan.

Getting started

mkvirtualenv elex-loader
git clone git@github.com:newsdev/elex-loader.git && cd elex-loader
pip install -r requirements.txt
./scripts/$ENV/bootstrap.sh

The bootstrap.sh script will create databases and the user necessary for local development. Note: This does not exist for non-development environments. Please use commands in elex-dotfiles instead.

Environments

The New York Times defines a handful of different environments; principally, dev, stg and prd.

  • dev: Hits test URLs by default. Assumes a local Postgres database where the local user is a superuser.
  • stg: Hits test URLs by default. Requires a Postgres user / host / password to be defined in the environment. We use a .pgpass file and export the rest in /etc/environment. Check out elex-dotfiles for more.
  • prd: Hits live URLs by default. Requires a Postgres user / host / password to be defined in the environment.

Use cases

Load initial data

./scripts/$ENV/reload.sh

The AP will make "live zeros" available in the morning of an election day. You can run reload.sh to get an entire new set of data, including races, reporting units, candidates and zeroed-out results.

Load results on election night

./scripts/$ENV/daemon.sh

The daemon will run 100,000 times (seriously) unless it is stopped. We control ours with a custom Supervisord instance and a modified /etc/supervisord.conf. This configuration file is available in elex-dotfiles along with other secrets.

Set a wait interval

You might want to control how long the daemon waits between cycles. This is hardcoded to a default -- 15s in production, 30s elsewhere. You can create the file /tmp/elex_loader_timeout.sh and export an ELEX_LOADER_TIMEOUT variable like this:

export ELEX_LOADER_TIMEOUT=60

The daemon checks for this file and sources it if it exists in every loop, which means you can dynamically control the wait time. For example, we do this in our admin.

Load results once

./scripts/$ENV/update.sh

Sometimes you just need to load a single update, e.g., to grab final results after we've turned off the loader. This command will get new results without baking or reloading any other parts of the database.

Load delegate data

./scripts/$ENV/delegates.sh

Often, the AP will update delegate information after our daemon has stopped running. To update just delegates for a given racedate, run this command.