Skip to content

Commit

Permalink
Add support for Acres Operated.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Oct 9, 2023
1 parent c4b0ba9 commit a863f7b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 12 deletions.
38 changes: 38 additions & 0 deletions src/constants/codapMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,34 @@ export const attrToCODAPColumnName: IAttrToCodapColumn = {
"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 = [
Expand All @@ -214,3 +242,13 @@ export const economicClassAttirbutes = [
"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
71 changes: 61 additions & 10 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, economicClassAttirbutes } 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 @@ -114,6 +120,11 @@ export const getAllAttrs = (selectedOptions: IStateOptions) => {
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;
Expand Down Expand Up @@ -230,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 @@ -244,15 +254,56 @@ const getAttrData = async (params: IGetAttrDataParams) => {
const values: any = {};
if (res) {
const {data} = res;
data.map((dataItem: any) => {
let codapColumnName;
if (attribute === "Economic Class") {
codapColumnName = attrToCODAPColumnName[dataItem.domaincat_desc].attributeNameInCodapTable;
} else {
codapColumnName = attrToCODAPColumnName[dataItem.short_desc].attributeNameInCodapTable;
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;
}
return values[codapColumnName] = dataItem.Value;
});
} 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

0 comments on commit a863f7b

Please sign in to comment.