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.
- Docker
- Docker Compose v2
- Build images and start Postgres:
docker compose up -d --build postgres - Run database migrations:
docker compose run --rm app alembic upgrade head - Seed data from USAspending:
docker compose run --rm app python src/awardsreport/setup/seed.py -s 2023-10-01 -e 2023-10-31 - Run derivations:
docker compose run --rm app python src/awardsreport/setup/transaction_derivations.py - Populate the transactions table:
docker compose run --rm app python src/awardsreport/setup/seed_transactions_table.py - Run the API:
docker compose up app
- API: http://localhost:8000
- OpenAPI docs: http://localhost:8000/docs
docker compose -p awards-test run --rm app pytest
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/servicesFormat API responsesrc/awardsreport/database.pySQLAlchemy base classes and boilerplatesrc/awardsreport/main.pyuvicorn run command to start server for APIsrc/awardsreport/models.pySQLAlchemy modelssrc/awardsreport/setup/scripts to seed database and perform derivations
/src/testsunit tests and pytest configurationsrc/tests/logictests for scripts insrc/awardsreport/logicsrc/tests/setuptests for scripts insrc/awardsreport/setupsrc/tests/servicestests for scripts insrc/awardsreport/services
/.env.examplesample database connection information. To be saved as/.envfor 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_resultsintest_get_raw_columns_assistanceand/ortest_get_raw_columns_procurementinsrc/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
docattribute 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
transactionstable. (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_valuesLiteral insrc/awardsreport/schemas/summary_tables_schemas.py.src/tests/logic/test_summary_tables.py test_create_group_by_col_list_eachwill test this element when added togb_values.
- Add item to
group_by_key_coldict insrc/awardsreport/logic/summary_tables.py. Use the same key from previous step. - Add attribute to
TableSchemaDatainsrc/awardsreport/schemas/summary_tables_schemasusing 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_opdict 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
FilterStatementSchemainsrc/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
MIT