Skip to content

Commit

Permalink
Merge pull request #2200 from fedspendingtransparency/staging
Browse files Browse the repository at this point in the history
Sprint 97 Production Deploy
  • Loading branch information
tony-sappe authored Jan 6, 2020
2 parents a8b1b18 + df0fe33 commit 108fc09
Show file tree
Hide file tree
Showing 36 changed files with 805 additions and 213 deletions.
2 changes: 2 additions & 0 deletions usaspending_api/accounts/v2/filters/account_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def generate_treasury_account_query(queryset, account_type, tas_id):
Value("-"),
"{}__federal_account__main_account_code".format(tas_id),
),
"submission_period": FiscalYearAndQuarter("reporting_period_end"),
}

# Derive recipient_parent_name
Expand All @@ -188,6 +189,7 @@ def generate_federal_account_query(queryset, account_type, tas_id):
"{}__federal_account__main_account_code".format(tas_id),
),
"agency_name": get_agency_name_annotation(tas_id, "agency_id"),
"submission_period": FiscalYearAndQuarter("reporting_period_end"),
}

# Derive recipient_parent_name for award_financial downloads
Expand Down
16 changes: 11 additions & 5 deletions usaspending_api/awards/v2/lookups/elasticsearch_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@


TRANSACTIONS_LOOKUP = {
"Recipient Name": "recipient_name",
"Recipient Name": "recipient_name.keyword",
"Action Date": "action_date",
"Transaction Amount": "transaction_amount",
"Award Type": "type_description",
"Awarding Agency": "awarding_toptier_agency_name",
"Awarding Sub Agency": "awarding_subtier_agency_name",
"Award Type": "type_description.keyword",
"Awarding Agency": "awarding_toptier_agency_name.keyword",
"Awarding Sub Agency": "awarding_subtier_agency_name.keyword",
"Funding Agency": "funding_toptier_agency_name",
"Funding Sub Agency": "funding_subtier_agency_name",
"Issued Date": "period_of_performance_start_date",
Expand All @@ -25,9 +25,15 @@
"Last Date to Order": "ordering_period_end_date",
}

TRANSACTIONS_SOURCE_LOOKUP = {key: value.replace(".keyword", "") for key, value in TRANSACTIONS_LOOKUP.items()}

INDEX_ALIASES_TO_AWARD_TYPES = deepcopy(all_award_types_mappings)
INDEX_ALIASES_TO_AWARD_TYPES["directpayments"] = INDEX_ALIASES_TO_AWARD_TYPES.pop("direct_payments")
INDEX_ALIASES_TO_AWARD_TYPES["other"] = INDEX_ALIASES_TO_AWARD_TYPES.pop("other_financial_assistance")

KEYWORD_DATATYPE_FIELDS = ["recipient_name", "awarding_toptier_agency_name", "awarding_subtier_agency_name"]
KEYWORD_DATATYPE_FIELDS = [
"recipient_name.keyword",
"awarding_toptier_agency_name.keyword",
"awarding_subtier_agency_name.keyword",
"type_description.keyword",
]
5 changes: 1 addition & 4 deletions usaspending_api/common/matview_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
DEPENDENCY_FILEPATH = settings.APP_DIR / "database_scripts" / "matviews" / "functions_and_enums.sql"
JSON_DIR = settings.APP_DIR / "database_scripts" / "matview_sql_generator"
MATVIEW_GENERATOR_FILE = settings.APP_DIR / "database_scripts" / "matview_generator" / "matview_sql_generator.py"
OVERLAY_VIEWS = [
settings.APP_DIR / "database_scripts" / "matviews" / "vw_award_search.sql",
settings.APP_DIR / "database_scripts" / "etl" / "transaction_delta_view.sql",
]
OVERLAY_VIEWS = [settings.APP_DIR / "database_scripts" / "matviews" / "vw_award_search.sql"]
DROP_OLD_MATVIEWS = settings.APP_DIR / "database_scripts" / "matviews" / "drop_old_matviews.sql"
MATERIALIZED_VIEWS = OrderedDict(
[
Expand Down
11 changes: 8 additions & 3 deletions usaspending_api/conftest_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ def update_index(self):

def _add_contents(self):
"""
Get all of the transactions presented in the view and
stuff them into the Elasticsearch index.
Get all of the transactions presented in the view and stuff them into the Elasticsearch index.
The view is only needed to load the transactions into Elasticsearch so it is dropped after each use.
"""
transaction_delta_view_sql = open(
str(settings.APP_DIR / "database_scripts" / "etl" / "transaction_delta_view.sql"), "r"
).read()
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM {}".format(settings.ES_TRANSACTIONS_ETL_VIEW_NAME))
cursor.execute(transaction_delta_view_sql)
cursor.execute(f"SELECT * FROM {settings.ES_TRANSACTIONS_ETL_VIEW_NAME};")
transactions = ordered_dictionary_fetcher(cursor)
cursor.execute(f"DROP VIEW {settings.ES_TRANSACTIONS_ETL_VIEW_NAME};")

for transaction in transactions:
self.client.index(
Expand Down
76 changes: 72 additions & 4 deletions usaspending_api/database_scripts/etl/transaction_delta_view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ SELECT
UTM.award_category,
UTM.recipient_unique_id,
UTM.parent_recipient_unique_id,
CONCAT(UTM.recipient_hash, '-', case when UTM.parent_recipient_unique_id is null then 'R' else 'C' end) as recipient_hash,
UTM.recipient_name,

UTM.action_date,
DATE(UTM.action_date + interval '3 months') AS fiscal_action_date,
AWD.period_of_performance_start_date,
AWD.period_of_performance_current_end_date,
FPDS.ordering_period_end_date,
Expand All @@ -47,9 +49,14 @@ SELECT
UTM.federal_action_obligation AS transaction_amount,
UTM.face_value_loan_guarantee,
UTM.original_loan_subsidy_cost,
UTM.generated_pragmatic_obligation,

UTM.awarding_agency_id,
UTM.funding_agency_id,
AA.toptier_agency_id AS awarding_toptier_agency_id,
FA.toptier_agency_id AS funding_toptier_agency_id,
AA.subtier_agency_id AS awarding_subtier_agency_id,
FA.subtier_agency_id AS funding_subtier_agency_id,
UTM.awarding_toptier_agency_name,
UTM.funding_toptier_agency_name,
UTM.awarding_subtier_agency_name,
Expand All @@ -58,13 +65,18 @@ SELECT
UTM.funding_toptier_agency_abbreviation,
UTM.awarding_subtier_agency_abbreviation,
UTM.funding_subtier_agency_abbreviation,
TAA.toptier_code AS awarding_toptier_agency_code,
TFA.toptier_code AS funding_toptier_agency_code,
SAA.subtier_code AS awarding_subtier_agency_code,
SFA.subtier_code AS funding_subtier_agency_code,

CFDA.id AS cfda_id,
UTM.cfda_number,
UTM.cfda_title,
'' AS cfda_popular_name,
UTM.type_of_contract_pricing,
UTM.type_set_aside,
UTM.extent_competed,
UTM.pulled_from,
UTM.type,

UTM.pop_country_code,
Expand All @@ -83,10 +95,66 @@ SELECT
UTM.recipient_location_county_name,
UTM.recipient_location_zip5,
UTM.recipient_location_congressional_code,
UTM.recipient_location_city_name
UTM.recipient_location_city_name,

TREASURY_ACCT.treasury_accounts,
FEDERAL_ACCT.federal_accounts,
UTM.business_categories

FROM universal_transaction_matview UTM
JOIN transaction_normalized TM ON (UTM.transaction_id = TM.id)
INNER JOIN transaction_normalized TM ON (UTM.transaction_id = TM.id)
LEFT JOIN transaction_fpds FPDS ON (UTM.transaction_id = FPDS.transaction_id)
LEFT JOIN transaction_fabs FABS ON (UTM.transaction_id = FABS.transaction_id)
LEFT OUTER JOIN awards AWD ON (UTM.award_id = AWD.id);
LEFT JOIN awards AWD ON (UTM.award_id = AWD.id)
-- Similar joins are already performed on universal_transaction_matview, however, to avoid making the matview larger
-- than needed they have been placed here. Feel free to phase out if the columns gained from the following joins are
-- added to the universal_transaction_matview.
LEFT JOIN agency AA ON (TM.awarding_agency_id = AA.id)
LEFT JOIN agency FA ON (TM.funding_agency_id = FA.id)
LEFT JOIN toptier_agency TAA ON (AA.toptier_agency_id = TAA.toptier_agency_id)
LEFT JOIN subtier_agency SAA ON (AA.subtier_agency_id = SAA.subtier_agency_id)
LEFT JOIN toptier_agency TFA ON (FA.toptier_agency_id = TFA.toptier_agency_id)
LEFT JOIN subtier_agency SFA ON (FA.subtier_agency_id = SFA.subtier_agency_id)
LEFT JOIN references_cfda CFDA ON (FABS.cfda_number = CFDA.program_number)
LEFT JOIN (
SELECT
faba.award_id,
JSONB_AGG(
DISTINCT JSONB_BUILD_OBJECT(
'aid', taa.agency_id,
'ata', taa.allocation_transfer_agency_id,
'main', taa.main_account_code,
'sub', taa.sub_account_code,
'bpoa', taa.beginning_period_of_availability,
'epoa', taa.beginning_period_of_availability,
'a', taa.availability_type_code
)
) treasury_accounts
FROM
federal_account fa
INNER JOIN treasury_appropriation_account taa ON (fa.id = taa.federal_account_id)
INNER JOIN financial_accounts_by_awards faba ON (taa.treasury_account_identifier = faba.treasury_account_id)
WHERE
faba.award_id IS NOT NULL
GROUP BY
faba.award_id
) TREASURY_ACCT ON (TREASURY_ACCT.award_id = UTM.award_id)
LEFT JOIN (
SELECT
faba.award_id,
JSONB_AGG(
DISTINCT JSONB_BUILD_OBJECT(
'id', fa.id,
'account_title', fa.account_title,
'federal_account_code', fa.federal_account_code
)
) federal_accounts
FROM
federal_account fa
INNER JOIN treasury_appropriation_account taa ON fa.id = taa.federal_account_id
INNER JOIN financial_accounts_by_awards faba ON taa.treasury_account_identifier = faba.treasury_account_id
WHERE
faba.award_id IS NOT NULL
GROUP BY
faba.award_id
) FEDERAL_ACCT ON (FEDERAL_ACCT.award_id = UTM.award_id);
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@
" CASE WHEN COALESCE(transaction_fpds.legal_entity_country_code) = 'UNITED STATES' THEN 'USA' ELSE COALESCE(transaction_fpds.legal_entity_country_code,'USA') END AS recipient_location_country_code,",
" transaction_fpds.legal_entity_country_name AS recipient_location_country_name,",
" transaction_fpds.legal_entity_state_code AS recipient_location_state_code,",
" transaction_fpds.legal_entity_county_code AS recipient_location_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.legal_entity_county_code, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS recipient_location_county_code,",
" transaction_fpds.legal_entity_county_name AS recipient_location_county_name,",
" transaction_fpds.legal_entity_congressional AS recipient_location_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.legal_entity_congressional, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS recipient_location_congressional_code,",
" transaction_fpds.legal_entity_zip5 AS recipient_location_zip5,",
" TRIM(TRAILING FROM transaction_fpds.legal_entity_city_name) AS recipient_location_city_name,",
"",
" transaction_fpds.place_of_perform_country_n AS pop_country_name,",
" COALESCE(transaction_fpds.place_of_perform_country_c, 'USA') AS pop_country_code,",
" transaction_fpds.place_of_performance_state AS pop_state_code,",
" transaction_fpds.place_of_perform_county_co AS pop_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.place_of_perform_county_co, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS pop_county_code,",
" transaction_fpds.place_of_perform_county_na AS pop_county_name,",
" NULL::text AS pop_city_code,",
" transaction_fpds.place_of_performance_zip5 AS pop_zip5,",
" transaction_fpds.place_of_performance_congr AS pop_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.place_of_performance_congr, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS pop_congressional_code,",
" TRIM(TRAILING FROM transaction_fpds.place_of_perform_city_name) AS pop_city_name,",
"",
" NULL::text AS cfda_number,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@
" CASE WHEN transaction_fabs.legal_entity_country_code = 'UNITED STATES' THEN 'USA' ELSE COALESCE(transaction_fabs.legal_entity_country_code,'USA') END AS recipient_location_country_code,",
" transaction_fabs.legal_entity_country_name AS recipient_location_country_name,",
" transaction_fabs.legal_entity_state_code AS recipient_location_state_code,",
" transaction_fabs.legal_entity_county_code AS recipient_location_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.legal_entity_county_code, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS recipient_location_county_code,",
" transaction_fabs.legal_entity_county_name AS recipient_location_county_name,",
" transaction_fabs.legal_entity_congressional AS recipient_location_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.legal_entity_congressional, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS recipient_location_congressional_code,",
" transaction_fabs.legal_entity_zip5 AS recipient_location_zip5,",
" TRIM(TRAILING FROM transaction_fabs.legal_entity_city_name) AS recipient_location_city_name,",
"",
" transaction_fabs.place_of_perform_country_n AS pop_country_name,",
" COALESCE(transaction_fabs.place_of_perform_country_c,'USA') AS pop_country_code,",
" transaction_fabs.place_of_perfor_state_code AS pop_state_code,",
" transaction_fabs.place_of_perform_county_co AS pop_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.place_of_perform_county_co, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS pop_county_code,",
" transaction_fabs.place_of_perform_county_na AS pop_county_name,",
" transaction_fabs.place_of_performance_code AS pop_city_code,",
" transaction_fabs.place_of_performance_zip5 AS pop_zip5,",
" transaction_fabs.place_of_performance_congr AS pop_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.place_of_performance_congr, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS pop_congressional_code,",
" TRIM(TRAILING FROM transaction_fabs.place_of_performance_city) AS pop_city_name,",
"",
" transaction_fabs.cfda_number,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@
" CASE WHEN transaction_fabs.legal_entity_country_code = 'UNITED STATES' THEN 'USA' ELSE COALESCE(transaction_fabs.legal_entity_country_code,'USA') END AS recipient_location_country_code,",
" transaction_fabs.legal_entity_country_name AS recipient_location_country_name,",
" transaction_fabs.legal_entity_state_code AS recipient_location_state_code,",
" transaction_fabs.legal_entity_county_code AS recipient_location_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.legal_entity_county_code, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS recipient_location_county_code,",
" transaction_fabs.legal_entity_county_name AS recipient_location_county_name,",
" transaction_fabs.legal_entity_congressional AS recipient_location_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.legal_entity_congressional, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS recipient_location_congressional_code,",
" transaction_fabs.legal_entity_zip5 AS recipient_location_zip5,",
" TRIM(TRAILING FROM transaction_fabs.legal_entity_city_name) AS recipient_location_city_name,",
"",
" transaction_fabs.place_of_perform_country_n AS pop_country_name,",
" COALESCE(transaction_fabs.place_of_perform_country_c,'USA') AS pop_country_code,",
" transaction_fabs.place_of_perfor_state_code AS pop_state_code,",
" transaction_fabs.place_of_perform_county_co AS pop_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.place_of_perform_county_co, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS pop_county_code,",
" transaction_fabs.place_of_perform_county_na AS pop_county_name,",
" transaction_fabs.place_of_performance_code AS pop_city_code,",
" transaction_fabs.place_of_performance_zip5 AS pop_zip5,",
" transaction_fabs.place_of_performance_congr AS pop_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.place_of_performance_congr, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS pop_congressional_code,",
" TRIM(TRAILING FROM transaction_fabs.place_of_performance_city) AS pop_city_name,",
"",
" transaction_fabs.cfda_number,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@
" CASE WHEN COALESCE(transaction_fpds.legal_entity_country_code) = 'UNITED STATES' THEN 'USA' ELSE COALESCE(transaction_fpds.legal_entity_country_code,'USA') END AS recipient_location_country_code,",
" transaction_fpds.legal_entity_country_name AS recipient_location_country_name,",
" transaction_fpds.legal_entity_state_code AS recipient_location_state_code,",
" transaction_fpds.legal_entity_county_code AS recipient_location_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.legal_entity_county_code, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS recipient_location_county_code,",
" transaction_fpds.legal_entity_county_name AS recipient_location_county_name,",
" transaction_fpds.legal_entity_congressional AS recipient_location_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.legal_entity_congressional, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS recipient_location_congressional_code,",
" transaction_fpds.legal_entity_zip5 AS recipient_location_zip5,",
" TRIM(TRAILING FROM transaction_fpds.legal_entity_city_name) AS recipient_location_city_name,",
"",
" transaction_fpds.place_of_perform_country_n AS pop_country_name,",
" COALESCE(transaction_fpds.place_of_perform_country_c, 'USA') AS pop_country_code,",
" transaction_fpds.place_of_performance_state AS pop_state_code,",
" transaction_fpds.place_of_perform_county_co AS pop_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.place_of_perform_county_co, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS pop_county_code,",
" transaction_fpds.place_of_perform_county_na AS pop_county_name,",
" NULL::text AS pop_city_code,",
" transaction_fpds.place_of_performance_zip5 AS pop_zip5,",
" transaction_fpds.place_of_performance_congr AS pop_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fpds.place_of_performance_congr, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS pop_congressional_code,",
" TRIM(TRAILING FROM transaction_fpds.place_of_perform_city_name) AS pop_city_name,",
"",
" NULL::text AS cfda_number,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@
" CASE WHEN transaction_fabs.legal_entity_country_code = 'UNITED STATES' THEN 'USA' ELSE COALESCE(transaction_fabs.legal_entity_country_code,'USA') END AS recipient_location_country_code,",
" transaction_fabs.legal_entity_country_name AS recipient_location_country_name,",
" transaction_fabs.legal_entity_state_code AS recipient_location_state_code,",
" transaction_fabs.legal_entity_county_code AS recipient_location_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.legal_entity_county_code, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS recipient_location_county_code,",
" transaction_fabs.legal_entity_county_name AS recipient_location_county_name,",
" transaction_fabs.legal_entity_congressional AS recipient_location_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.legal_entity_congressional, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS recipient_location_congressional_code,",
" transaction_fabs.legal_entity_zip5 AS recipient_location_zip5,",
" TRIM(TRAILING FROM transaction_fabs.legal_entity_city_name) AS recipient_location_city_name,",
"",
" transaction_fabs.place_of_perform_country_n AS pop_country_name,",
" COALESCE(transaction_fabs.place_of_perform_country_c,'USA') AS pop_country_code,",
" transaction_fabs.place_of_perfor_state_code AS pop_state_code,",
" transaction_fabs.place_of_perform_county_co AS pop_county_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.place_of_perform_county_co, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 3, '0') AS pop_county_code,",
" transaction_fabs.place_of_perform_county_na AS pop_county_name,",
" transaction_fabs.place_of_performance_code AS pop_city_code,",
" transaction_fabs.place_of_performance_zip5 AS pop_zip5,",
" transaction_fabs.place_of_performance_congr AS pop_congressional_code,",
" LPAD(CAST(CAST((REGEXP_MATCH(transaction_fabs.place_of_performance_congr, '^[A-Z]*(\\d+)(?:\\.\\d+)?$'))[1] AS smallint) AS text), 2, '0') AS pop_congressional_code,",
" TRIM(TRAILING FROM transaction_fabs.place_of_performance_city) AS pop_city_name,",
"",
" transaction_fabs.cfda_number,",
Expand Down
Loading

0 comments on commit 108fc09

Please sign in to comment.