Uses the CBER Data Grabber in a shell script to pull US federal data from various APIs and update the County Profiles website, produced by Ball State University's Center for Business and Economic Research.
When installed on the same server as County Profiles and config/app.php is set up with the correct
database connection settings, this app interfaces with the County Profiles database.
To view the menu and select an import:
cd C:\path\to\app
bin\cake import
To skip the menu and run a specific import:
cd C:\path\to\app
bin\cake import {importName}
The selected import proceeds thusly:
- Data is pulled from the API
- This data is checked for errors and it's determined whether this is data that it needs to insert into the database, data that needs to update existing records, or data that is already present in the database and can be ignored
- Assuming there's data to import, the script asks for confirmation to proceed and for permission to overwrite existing records if appropriate.
- MAGIC
To set up a means to update Foo data through this Shell, create the file src/Shell/Imports/FooShell.php, changing ACSUpdater to a different class if needed.
<?php
namespace App\Shell\Imports;
use App\Location\Location;
use App\Shell\ImportShell;
use CBERDataGrabber\ACSUpdater;
class FooShell extends ImportShell
{
public function run()
{
$defaultYear = 2014;
$this->year = $this->in('What year do you want to import data for?', null, $defaultYear);
$this->stateId = '18'; // Indiana
$this->locationTypeId = 2; // County
$this->surveyDate = $this->year.'0000';
$this->sourceId = 60; // 'American Community Survey (ACS) (https://www.census.gov/programs-surveys/acs/)'
$this->categoryIds = [
'First data category name' => 123,
'Another data category name' => 456
];
$this->out('Retrieving data from Census API...');
ACSUpdater::setAPIKey($this->apiKey);
$this->makeApiCall(function () {
return ACSUpdater::getCountyData($this->year, $this->stateId, ACSUpdater::$FOO, false);
});
$this->import();
}
}
The method run() must
- Set the object properties
locationTypeId,surveyDate,sourceId, andcategoryIds(the County Profiles class/Model/SegmentData.phpand the Data Categories Manager will help you determine the right category IDs to use) - Output
'Retrieving data from {data source}...' - Set any API key necessary
- Call
$this->apiCallResults($callable)with a function that returns the result of a a call to a CBER Data Grabber method - Call
$this->import();
After creating this class, Foo will appear in the list of available imports.
- After an import completes, update the relevant method in
/Model/SegmentData.phpin County Profiles with the appropriate new year. - Load
http://profiles.cberdata.org/data_center/pages/clear_cacheto clear old cached charts/tables. - If the relevant segment description (set in the
segmentsdatabase table) references data from the old year, update it to reflect the new year.
- The import process takes a large amount of memory, and currently exceeds the limit of CBER's shared hosting plan. If this process cannot run on the production server, it will be necessary to run it on a workstation, then replace the
statisticsdatabase table on the production server with the updated table. - When running this locally, it may be necessary to be running a 64-bit version of PHP.