Skip to content

Developer instructions

Greg Linch edited this page Nov 13, 2017 · 2 revisions

Hello and welcome to the developer wiki for mccelections!

The key elements and features to use the system as a developer involve the following.

General options

  • Do you want connect the output of the main scripts to a Slack channel? if so configure the slackbot.py module with the token, channel, username, and icon_url. The default setup involves sending messages to different channels based on a variable in settings.py indicating if it's the local, test/dev or prod server.
  • If you don't use the Slack output, comment out everything in the slackbot function except for the final line -- print text -- or else you won't see any output in your terminal.

Initial data loading

  • Downloading elections from the AP with ./manage.py download_elections management command.
  • Hand-keying AP tests under the Election model.
  • Having users enter any manual results in the admin.

Automation

Basics

  • Execute ./manage.py election_auto management command to run on election or test election days. You could also set this up on a cron or edit and use the automator module.
  • If you want to fake the today variable for an automation module, run ./manage.py election_auto -d YYYY-MM-DD.

How the module works

  • The script looks for any election or election tests for the current day when it runs.
  • If there are any, it will start looping through them to see any votes exists as an indicator for whether the election is live. This is done by loading the data to the ResultCheck model and getting the aggregate sum of the votecount fields. If it's greater than zero, then it's a live election.
  • If an election is live, the script exits the check loop and enters a loop to import for ResultStage, which feeds the /resultlive endpoint.
  • On first import, it will set the Election to live and set the start time, plus set the number of result objects.
  • Whether it's a test or real election, the data will import to ResultStage until the aggregate average for precinctsreportingpcg is 100 percent, indicating all results are in, or the number of result objects changes, indicating AP has cleared data from their API in anticipation of a future election. This usually happens when state primaries are on different days for each major party.
  • If it's a test election, there's also a timer that will expire the ResultStage import loop after an hour, which is the typical test length.
  • As that loop is running, it will also output three percentage averages. The "percent average total" is the same number used by the script to see if the election is finished. There's a "percent average state-level" number to show how many statewide or below race results remain. Finally, there's a "percent average state-level for president" to show how presidential races going. This is mostly useful during AP tests when presidential race results are simulated to 100 percent, but statewide or below races usually aren't. If presidential average is 100 for a test, then the test is probably finished.
  • After the results finish, it will switch the Election to no longer being live, set the end time and exit that loop.
  • The script then loads the archive data to the Result model. If it's a test, ResultStage is immediately cleared. If it's not, the script it slept for 15 minutes, which is the current cache time set for the Result archive endpoint, to avoid having an empty ResultStage model being caught by the cache.
  • If there was more than one test, it will then restart the vote check and look just at the remaining elections.

Manual input

If users will be hand-keying results data in a Google spreadsheet, then they will need to

Update the code on the test and prod servers

Set up an alias in ~/.bash_profile (Mac) or ~/.bashrc (Ubuntu) to make updating the codebase easier. Update the file paths as needed:

export "alias mccupdate='git pull origin master && cp ~/electionsproject/electionsproject/settings_prod.py ~/electionsproject/electionsproject/settings.py'" >> ~/.bash_profile

Enable the virtualenv and change into the main directory:

mccelections

Get the latest code from the GitHub repo and update the settings.py file based on the environment

mccupdate

Then run the following Django commands (make sure you're still in the main project directory, such as by executing cd ~/mccelections/electionsproject/results)

Make a new migration

./manage.py makemigrations

Migrate that

./manage.py migrate

If you changed the admin, you'll need to restart uwsgi:

sudo service uwsgi restart

If you still don't see changes after that, you might need to reboot the server:

sudo reboot

Then confirm everything updated and works.