Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Economic Class + Acres Operated (PT-185986613) #15

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ function App() {
setStatusGraphic(<ProgressIndicator/>);
const res = await createTableFromSelections(selectedOptions);
if (res !== "success") {
setStatusMessage(strings.fetchSuccess);
setStatusMessage(strings.fetchError);
setStatusGraphic(<Error/>);
} else {
setStatusMessage(strings.fetchError);
setStatusMessage(strings.fetchSuccess);
setStatusGraphic(<Checkmark/>);
}
};
Expand Down
71 changes: 71 additions & 0 deletions src/constants/codapMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,76 @@ export const attrToCODAPColumnName: IAttrToCodapColumn = {
"WHEAT - ACRES HARVESTED": {
"attributeNameInCodapTable": "Wheat Area Harvested",
"unitInCodapTable": "Acres Harvested"
},
"ECONOMIC CLASS: (1,000 TO 9,999 $)": {
"attributeNameInCodapTable": "Small Farm ($1,000 - $9,999)",
"unitInCodapTable": ""
},
"ECONOMIC CLASS: (10,000 TO 99,999 $)": {
"attributeNameInCodapTable": "Small Medium Farm ($10,000 - $99,000)",
"unitInCodapTable": "# of Farms"
},
"ECONOMIC CLASS: (100,000 TO 249,999 $)": {
"attributeNameInCodapTable": "Medium Farm ($100,000 - $249,000)",
"unitInCodapTable": "# of Farms"
},
"ECONOMIC CLASS: (250,000 TO 499,999 $)": {
"attributeNameInCodapTable": "Medium Large Farm ($250,000 - $499,999)",
"unitInCodapTable": "# of Farms"
},
"ECONOMIC CLASS: (500,000 TO 999,999 $)": {
"attributeNameInCodapTable": "Large Farm ($500,000 - $999,999)",
"unitInCodapTable": "# of Farms"
},
"ECONOMIC CLASS: (1,000,000 OR MORE $)": {
"attributeNameInCodapTable": "Very Large Farm ($1 million or more)",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (1.0 TO 9.9 ACRES)": {
"attributeNameInCodapTable": "Less than 10 Acres",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (10.0 TO 49.9 ACRES)": {
"attributeNameInCodapTable": "10 - 50 Acres",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (50.0 TO 100 ACRES)": {
"attributeNameInCodapTable": "50 - 100 Acres",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (100 TO 500 ACRES)": {
"attributeNameInCodapTable": "100 - 500 Acres",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (500 TO 999 ACRES)": {
"attributeNameInCodapTable": "500 - 1,000 Acres",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (1,000 TO 5,000 ACRES)": {
"attributeNameInCodapTable": "1,000 - 5,000 Acres",
"unitInCodapTable": "# of Farms"
},
"AREA OPERATED: (5,000 OR MORE ACRES)": {
"attributeNameInCodapTable": "5,000 Acres or More",
"unitInCodapTable": "# of Farms"
}
};

export const economicClassAttirbutes = [
"ECONOMIC CLASS: (1,000 TO 9,999 $)",
"ECONOMIC CLASS: (10,000 TO 99,999 $)",
"ECONOMIC CLASS: (100,000 TO 249,999 $)",
"ECONOMIC CLASS: (250,000 TO 499,999 $)",
"ECONOMIC CLASS: (500,000 TO 999,999 $)",
"ECONOMIC CLASS: (1,000,000 OR MORE $)"
];

export const acresOperatedAttributes = [
"AREA OPERATED: (1.0 TO 9.9 ACRES)",
"AREA OPERATED: (10.0 TO 49.9 ACRES)",
"AREA OPERATED: (50.0 TO 100 ACRES)",
"AREA OPERATED: (100 TO 500 ACRES)",
"AREA OPERATED: (500 TO 999 ACRES)",
"AREA OPERATED: (1,000 TO 5,000 ACRES)",
"AREA OPERATED: (5,000 OR MORE ACRES)"
];
4 changes: 2 additions & 2 deletions src/constants/queryHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ export const queryData: Array<IQueryHeaders> = [
sect_desc: "Economics",
group_desc: "Farms & Land & Assets",
commodity_desc: "Farm Operations",
statisticcat_desc: "Area Operated",
short_desc: ["FARM OPERATIONS - ACRES OPERATED"],
statisticcat_desc: "Operations",
short_desc: ["FARM OPERATIONS - NUMBER OF OPERATIONS"],
domain_desc: "Area Operated",
geographicAreas: ["State", "County"],
years: {
Expand Down
75 changes: 68 additions & 7 deletions src/scripts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { connect } from "./connect";
import { cropOptions, fiftyStates } from "../constants/constants";
import { countyData } from "../constants/counties";
import { flatten, getQueryParams } from "./utils";
import { attrToCODAPColumnName } from "../constants/codapMetadata";
import { acresOperatedAttributes, attrToCODAPColumnName, economicClassAttirbutes } from "../constants/codapMetadata";

const baseURL = `https://quickstats.nass.usda.gov/api/api_GET/?key=9ED0BFB8-8DDD-3609-9940-A2341ED6A9E3`;

Expand All @@ -18,6 +18,12 @@ interface IGetAttrDataParams {
state?: string
}

interface IAcreTotals {
[key: string]: {
[key: string]: number | null
}
}

export const fetchDataWithRetry = async (req: string, maxRetries = 3) => {
let retries = 0;
while (retries < maxRetries) {
Expand Down Expand Up @@ -109,7 +115,17 @@ export const getAllAttrs = (selectedOptions: IStateOptions) => {
throw new Error("Invalid attribute");
}
const {short_desc} = queryParams;
if (Array.isArray(short_desc)) {
if (attribute === "Economic Class") {
for (const econAttr of economicClassAttirbutes) {
const codapColumnName = attrToCODAPColumnName[econAttr].attributeNameInCodapTable;
allAttrs.push(codapColumnName);
}
} else if (attribute === "Acres Operated") {
for (const acresAttr of acresOperatedAttributes) {
const codapColumnName = attrToCODAPColumnName[acresAttr].attributeNameInCodapTable;
allAttrs.push(codapColumnName);
}
} else if (Array.isArray(short_desc)) {
for (const desc of short_desc) {
const codapColumnName = attrToCODAPColumnName[desc].attributeNameInCodapTable;
allAttrs.push(codapColumnName);
Expand Down Expand Up @@ -225,7 +241,6 @@ const getDataForSingleYearAndState = async (selectedOptions: IStateOptions, coun
return item;
};


const getAttrData = async (params: IGetAttrDataParams) => {
const {attribute, geographicLevel, location, year, cropUnits, state} = params;
const reqParams: IGetAttrDataParams = {attribute, geographicLevel, location, year, state};
Expand All @@ -239,10 +254,56 @@ const getAttrData = async (params: IGetAttrDataParams) => {
const values: any = {};
if (res) {
const {data} = res;
data.map((dataItem: any) => {
const codapColumnName = attrToCODAPColumnName[dataItem.short_desc].attributeNameInCodapTable;
return values[codapColumnName] = dataItem.Value;
});
if (attribute === "Acres Operated") {
const acreTotals: IAcreTotals = {
"AREA OPERATED: (1.0 TO 9.9 ACRES)": {
"AREA OPERATED: (1.0 TO 9.9 ACRES)": 0
},
"AREA OPERATED: (10.0 TO 49.9 ACRES)": {
"AREA OPERATED: (10.0 TO 49.9 ACRES)": 0
},
"AREA OPERATED: (50.0 TO 100 ACRES)": {
"AREA OPERATED: (50.0 TO 69.9 ACRES)": 0,
"AREA OPERATED: (70.0 TO 99.9 ACRES)": 0
},
"AREA OPERATED: (100 TO 500 ACRES)": {
"AREA OPERATED: (100 TO 139 ACRES)": 0,
"AREA OPERATED: (140 TO 179 ACRES)": 0,
"AREA OPERATED: (180 TO 219 ACRES)": 0,
"AREA OPERATED: (220 TO 259 ACRES)": 0,
"AREA OPERATED: (260 TO 499 ACRES)": 0
},
"AREA OPERATED: (500 TO 999 ACRES)": {
"AREA OPERATED: (500 TO 999 ACRES)": 0
},
"AREA OPERATED: (1,000 TO 5,000 ACRES)": {
"AREA OPERATED: (1,000 TO 1,999 ACRES)": 0,
"AREA OPERATED: (2,000 TO 4,999 ACRES)": 0
},
"AREA OPERATED: (5,000 OR MORE ACRES)": {
"AREA OPERATED: (5,000 OR MORE ACRES)": 0
}
};
const totalKeys = Object.keys(acreTotals);
for (const total of totalKeys) {
const subTotalKeys = Object.keys(acreTotals[total]);
const dataItems = data.filter((dataItem: any) => subTotalKeys.includes(dataItem.domaincat_desc));
dataItems.forEach((dataItem: any) => {
acreTotals[total][dataItem.domaincat_desc] = dataItem.Value.replace(/,/g, "");
});
const codapColumnName = attrToCODAPColumnName[total].attributeNameInCodapTable;
// sum up all the values of acreTotals[total]
const onlyNumbers = subTotalKeys.map((key) => Number(acreTotals[total][key]));
const sum = onlyNumbers.reduce((acc, cur) => acc + cur);
values[codapColumnName] = sum;
}
} else {
data.forEach((dataItem: any) => {
const dataItemDesc = attribute === "Economic Class" ? dataItem.domaincat_desc : dataItem.short_desc;
const codapColumnName = attrToCODAPColumnName[dataItemDesc].attributeNameInCodapTable;
values[codapColumnName] = dataItem.Value;
});
}
} else {
// eslint-disable-next-line no-console
console.log(`Error: did not receive response for this request:`, req);
Expand Down
Loading