|
1 | 1 | """ |
2 | | -Look ups for elasticsearch fields to be displayed for the front end |
| 2 | +Lookups for elasticsearch fields to be displayed for the front end |
3 | 3 | """ |
4 | 4 |
|
5 | 5 | from copy import deepcopy |
| 6 | +from dataclasses import dataclass |
| 7 | +from enum import Enum |
6 | 8 |
|
7 | 9 | from usaspending_api.awards.v2.lookups.lookups import all_award_types_mappings |
8 | 10 |
|
9 | | -TRANSACTIONS_LOOKUP = { |
10 | | - "Recipient Name": "recipient_name.keyword", |
11 | | - "Action Date": "action_date", |
12 | | - "Transaction Amount": "federal_action_obligation", |
13 | | - "Award Type": "type_description.keyword", |
14 | | - "Awarding Agency": "awarding_toptier_agency_name.keyword", |
15 | | - "Awarding Sub Agency": "awarding_subtier_agency_name.keyword", |
16 | | - "Funding Agency": "funding_toptier_agency_name", |
17 | | - "Funding Sub Agency": "funding_subtier_agency_name", |
18 | | - "Issued Date": "period_of_performance_start_date", |
19 | | - "Loan Value": "face_value_loan_guarantee", |
20 | | - "Subsidy Cost": "original_loan_subsidy_cost", |
21 | | - "Mod": "modification_number.keyword", |
22 | | - "Award ID": "display_award_id", |
23 | | - "awarding_agency_id": "awarding_agency_id", |
24 | | - "internal_id": "award_id", |
25 | | - "generated_internal_id": "generated_unique_award_id", |
26 | | - "Last Date to Order": "ordering_period_end_date", |
27 | | - "def_codes": "disaster_emergency_fund_codes", |
28 | | - "Transaction Description": "transaction_description.keyword", |
29 | | - "Action Type": "action_type", |
30 | | - "Recipient UEI": "recipient_uei.keyword", |
31 | | - "awarding_agency_slug": "awarding_toptier_agency_name.keyword", |
32 | | - "funding_agency_slug": "funding_toptier_agency_name.keyword", |
33 | | - "recipient_location_city_name": "recipient_location_city_name.keyword", |
34 | | - "recipient_location_state_code": "recipient_location_state_code", |
35 | | - "recipient_location_country_name": "recipient_location_country_name.keyword", |
36 | | - "recipient_location_address_line1": "recipient_location_address_line1.keyword", |
37 | | - "recipient_location_address_line2": "recipient_location_address_line2.keyword", |
38 | | - "recipient_location_address_line3": "recipient_location_address_line3.keyword", |
39 | | - "pop_city_name": "pop_city_name.keyword", |
40 | | - "pop_state_code": "pop_state_code", |
41 | | - "pop_country_name": "pop_country_name.keyword", |
42 | | - "naics_code": "naics_code.keyword", |
43 | | - "naics_description": "naics_description.keyword", |
44 | | - "product_or_service_code": "product_or_service_code.keyword", |
45 | | - "product_or_service_description": "product_or_service_description.keyword", |
46 | | - "cfda_number": "cfda_number.keyword", |
47 | | - "cfda_title": "cfda_title.keyword", |
48 | | -} |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class ElasticsearchField: |
| 14 | + """ |
| 15 | + Represents a field that is searchable by an API endpoint and pairs it with the corresponding elasticsearch field. |
| 16 | +
|
| 17 | + Args: |
| 18 | + field_name: The name of the field provided by the user when selecting fields and returned by the API |
| 19 | + full_path: A complete path that may include additional field types such as ".keyword" |
| 20 | + short_path: The full_path with any additional field types removed; may be 1:1 with full_path |
| 21 | + """ |
| 22 | + |
| 23 | + field_name: str |
| 24 | + full_path: str |
| 25 | + short_path: str |
| 26 | + |
| 27 | + |
| 28 | +class TransactionField(str, Enum): |
| 29 | + ACTION_DATE = ("Action Date", "action_date") |
| 30 | + ACTION_TYPE = ("Action Type", "action_type") |
| 31 | + AWARD_ID = ("Award ID", "display_award_id") |
| 32 | + AWARD_TYPE = ("Award Type", "type_description.keyword") |
| 33 | + AWARDING_AGENCY = ("Awarding Agency", "awarding_toptier_agency_name.keyword") |
| 34 | + AWARDING_AGENCY_ID = ("awarding_agency_id", "awarding_agency_id") |
| 35 | + AWARDING_AGENCY_SLUG = ("awarding_agency_slug", "awarding_toptier_agency_name.keyword") |
| 36 | + AWARDING_SUB_AGENCY = ("Awarding Sub Agency", "awarding_subtier_agency_name.keyword") |
| 37 | + CFDA_NUMBER = ("cfda_number", "cfda_number.keyword") |
| 38 | + CFDA_TITLE = ("cfda_title", "cfda_title.keyword") |
| 39 | + DEF_CODES = ("def_codes", "disaster_emergency_fund_codes") |
| 40 | + FUNDING_AGENCY = ("Funding Agency", "funding_toptier_agency_name.keyword") |
| 41 | + FUNDING_AGENCY_SLUG = ("funding_agency_slug", "funding_toptier_agency_name.keyword") |
| 42 | + FUNDING_SUB_AGENCY = ("Funding Sub Agency", "funding_subtier_agency_name.keyword") |
| 43 | + GENERATED_INTERNAL_ID = ("generated_internal_id", "generated_unique_award_id") |
| 44 | + INTERNAL_ID = ("internal_id", "award_id") |
| 45 | + ISSUED_DATE = ("Issued Date", "period_of_performance_start_date") |
| 46 | + LAST_DATE_TO_ORDER = ("Last Date to Order", "ordering_period_end_date") |
| 47 | + LOAN_VALUE = ("Loan Value", "face_value_loan_guarantee") |
| 48 | + MOD = ("Mod", "modification_number.keyword") |
| 49 | + NAICS_CODE = ("naics_code", "naics_code.keyword") |
| 50 | + NAICS_DESCRIPTION = ("naics_description", "naics_description.keyword") |
| 51 | + POP_CITY_NAME = ("pop_city_name", "pop_city_name.keyword") |
| 52 | + POP_COUNTRY_NAME = ("pop_country_name", "pop_country_name.keyword") |
| 53 | + POP_STATE_CODE = ("pop_state_code", "pop_state_code") |
| 54 | + PSC_CODE = ("product_or_service_code", "product_or_service_code.keyword") |
| 55 | + PSC_DESCRIPTION = ("product_or_service_description", "product_or_service_description.keyword") |
| 56 | + RECIPIENT_ID = ("recipient_id", "recipient_agg_key") |
| 57 | + RECIPIENT_LOCATION_ADDRESS_LINE_1 = ("recipient_location_address_line1", "recipient_location_address_line1.keyword") |
| 58 | + RECIPIENT_LOCATION_ADDRESS_LINE_2 = ("recipient_location_address_line2", "recipient_location_address_line2.keyword") |
| 59 | + RECIPIENT_LOCATION_ADDRESS_LINE_3 = ("recipient_location_address_line3", "recipient_location_address_line3.keyword") |
| 60 | + RECIPIENT_LOCATION_CITY_NAME = ("recipient_location_city_name", "recipient_location_city_name.keyword") |
| 61 | + RECIPIENT_LOCATION_COUNTRY_NAME = ("recipient_location_country_name", "recipient_location_country_name.keyword") |
| 62 | + RECIPIENT_LOCATION_STATE_CODE = ("recipient_location_state_code", "recipient_location_state_code") |
| 63 | + RECIPIENT_NAME = ("Recipient Name", "recipient_name.keyword") |
| 64 | + RECIPIENT_UEI = ("Recipient UEI", "recipient_uei.keyword") |
| 65 | + SUBSIDY_COST = ("Subsidy Cost", "original_loan_subsidy_cost") |
| 66 | + TRANSACTION_AMOUNT = ("Transaction Amount", "federal_action_obligation") |
| 67 | + TRANSACTION_DESCRIPTION = ("Transaction Description", "transaction_description.keyword") |
| 68 | + |
| 69 | + def __new__(cls, field_name: str, full_path: str) -> "str": |
| 70 | + obj = str.__new__(cls, field_name) |
| 71 | + obj._value_ = field_name |
| 72 | + short_path = full_path.split(".")[0] |
| 73 | + obj._es_field = ElasticsearchField(field_name, full_path, short_path) |
| 74 | + return obj |
| 75 | + |
| 76 | + @property |
| 77 | + def field_name(self) -> str: |
| 78 | + return self._es_field.field_name |
| 79 | + |
| 80 | + @property |
| 81 | + def full_path(self) -> str: |
| 82 | + return self._es_field.full_path |
| 83 | + |
| 84 | + @property |
| 85 | + def short_path(self) -> str: |
| 86 | + return self._es_field.short_path |
| 87 | + |
49 | 88 |
|
50 | 89 | base_mapping = { |
51 | 90 | "Award ID": "display_award_id", |
|
145 | 184 | }, |
146 | 185 | } |
147 | 186 |
|
148 | | -TRANSACTIONS_SOURCE_LOOKUP = {key: value.replace(".keyword", "") for key, value in TRANSACTIONS_LOOKUP.items()} |
149 | | - |
150 | 187 | CONTRACT_SOURCE_LOOKUP = {key: value.replace(".keyword", "") for key, value in contracts_mapping.items()} |
151 | 188 | IDV_SOURCE_LOOKUP = {key: value.replace(".keyword", "") for key, value in idv_mapping.items()} |
152 | 189 | NON_LOAN_ASST_SOURCE_LOOKUP = {key: value.replace(".keyword", "") for key, value in non_loan_assist_mapping.items()} |
|
0 commit comments