The USAspending Monthly Awards Report uses federal prime award transaction data from USAspending.gov to provide information on the top categories receiving spending by various elements for each month.
- python 3.10.11
- PostgreSQL 15
- install requirements:
pip install -r requirements.txt
andpip install .
- create a psql database
- set database information:
mv .env.example .env
, update values in.env
- run alembic migrations:
alembic upgrade head
- seed the database with raw data from USAs:
python src/awardsreport/setup/seed.py
. Seepython src/awardsreport/setup/seed.py -h
- run derivations:
python src/awardsreport/setup/transaction_derivations.py
. - insert records to
transansactions
table:python src/awardsreport/setup/seed_transactions_table.py
- run server on localhost:
python src/awardsreport/main.py
Using the summary_tables
GET endpoint to populate a pandas DataFrame:
import json
import pandas as pd
import requests
r = requests.get("http://localhost:8000/summary_tables/?gb=naics&gb=ppopct&gb=awag&limit=500")
df = pd.read_json(json.dumps(r.json()), orient='table')
Study the response metadata using the JSON Table Schema:
r.json()['schema']
/src/awardsreport/
main functionality of the API, including: business logic, routers, setup scripts, models./src/awardsreport/logic/
business logicsrc/awardsreport/routers/
FastAPI routers to implement logicsrc/awardsreport/schemas/
Pydantic models to support validation and documentationsrc/awwardsreport/services
Format API responsesrc/awardsreport/database.py
SQLAlchemy base classes and boilerplatesrc/awardsreport/main.py
uvicorn run command to start server for APIsrc/awardsreport/models.py
SQLAlchemy modelssrc/awardsreport/setup/
scripts to seed database and perform derivations
/src/tests
unit tests and pytest configurationsrc/tests/logic
tests for scripts insrc/awardsreport/logic
src/tests/setup
tests for scripts insrc/awardsreport/setup
src/tests/services
tests for scripts insrc/awardsreport/services
/.env.example
sample database connection information. To be saved as/.env
for python dotenv package.
- black formatting, available through
/.vscode/settings.json
- Pylance with Type Checking Mode = basic
This section describes how to add new columns to the project from a raw USAs download file. This section does not cover adding new derived columns. Adding new columns is necessary to support grouping or filtering by elements available in USAs downloads, but not awardsreport.
- Update
expected_results
intest_get_raw_columns_assistance
and/ortest_get_raw_columns_procurement
insrc/tests/setup/test_seed_helpers.py
. Add raw column name from USAs download to list in alphabetical order.- If the column appears in Assistance download files, add to
test_get_raw_columns_assistance
. - If the column appears in Contract download files, add to
test_get_raw_columns_procurement
.
- If the column appears in Assistance download files, add to
- Add new column header as it appears in the download to the appropriate model
in
src/awardsreport/models.py
.- If the column only appears in Assistance download files, add to
AssistanceTransactionsMixin
- If the column only appears in Contract download files, add to
ProcurementTransactionsMixin
- If the column appears in both Assistance and Contract download files, add
to
TransactionsMixin
- Provide a
doc
attribute to descibe the element. Take langauge from USAspending data dictionary.
- If the column only appears in Assistance download files, add to
- Generate new alembic revision using updated model:
alembic revision --autogenerate -m "brief description of change"
- Run alembic migrations, seed the database, run derivations, populate
transactions
table. (SeeSetup and Installation
) - run tests:
pytest
This section describes how to allow the path GET /summary_tables/
to accept
new gb
parameter key values.
- Add any necessary unit tests to
src/tests/logic/test_summary_tables.py
. - Add brief key to
gb_values
Literal insrc/awardsreport/schemas/summary_tables_schemas.py
.src/tests/logic/test_summary_tables.py test_create_group_by_col_list_each
will test this element when added togb_values
.
- Add item to
group_by_key_col
dict insrc/awardsreport/logic/summary_tables.py
. Use the same key from previous step. - Add attribute to
TableSchemaData
insrc/awardsreport/schemas/summary_tables_schemas
using same name from step 2. - run tests:
pytest
This section describes how to allow the path GET /summary_tables/
to accept
new filter parameters.
- Add any necessary unit tests to
src/tests/logic/test_summary_tables.py
.- test function names should take the form:
test_create_filter_statement_
- test function names should take the form:
- Add item to
filter_key_op
dict insrc/awardsreport/logic/summary_tables.py
. If the filter merely checks for equality with an element that can be grouped by, use the same key fromgroup_by_key_col
. - Add attribute to
FilterStatementSchema
insrc/awardsreport/schemas/summary_tables_schemas.py
. The attribute name should match the key of the dict item from the previous set. If the filter merely checks for equality, the Query description should call the SQLAlchemy model element doc. - run tests:
pytest
MIT