Skip to content

Commit

Permalink
Add README to data warehouse export zip files
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpalmer committed May 1, 2020
1 parent 39366c0 commit fd941e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
41 changes: 41 additions & 0 deletions classes/DataWarehouse/Export/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CCR\Loggable;
use DataWarehouse\Data\BatchDataset;
use DataWarehouse\Data\RawStatisticsConfiguration;
use DataWarehouse\Export\FileWriter\FileWriterFactory;
use Exception;
use Log;
Expand Down Expand Up @@ -34,6 +35,12 @@ class FileManager extends Loggable
*/
private $fileWriterFactory;

/**
* Raw statistics configuration.
* @var \DataWarehouse\Data\RawStatisticsConfiguration;
*/
private $rawStatsConfig;

/**
* Construct a new file manager.
*
Expand All @@ -46,6 +53,8 @@ public function __construct(Log $logger = null)
$this->fileWriterFactory = new FileWriterFactory($logger);
parent::__construct($logger);

$this->rawStatsConfig = RawStatisticsConfiguration::factory();

try {
$this->exportDir = xd_utilities\getConfiguration(
'data_warehouse_export',
Expand Down Expand Up @@ -243,6 +252,15 @@ public function createZipFile($dataFile, array $request)
));
}

$readmeText = $this->getReadmeText($request['realm']);
if (!$zip->addFromString('README.txt', $readmeText)) {
throw new Exception(sprintf(
'Failed to add "README.txt" to zip file "%s"',
$dataFile,
$zipFile
));
}

if (!$zip->close()) {
throw new Exception(sprintf(
'Failed to close zip file "%s"',
Expand Down Expand Up @@ -309,4 +327,27 @@ public function removeDeletedRequests(array $deletedRequestIds)
}
}
}

/**
* Get the contents for README.txt for a realm.
*
* @param string $realm The name of a realm.
* @return string
*/
private function getReadmeText($realm)
{
$fields = $this->rawStatsConfig->getBatchExportFieldDefinitions($realm);

$text = "{$realm} Realm Fields\n\n";

foreach ($fields as $field) {
$text .= sprintf(
"%s: %s\n",
$field['name'],
$field['documentation']
);
}

return $text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"id": 1,
"user_id": 1,
"realm": "jobs",
"realm": "Jobs",
"start_date": "2018-01-01",
"end_date": "2018-02-28",
"export_file_format": "CSV",
Expand All @@ -20,7 +20,7 @@
{
"id": 2,
"user_id": 1,
"realm": "jobs",
"realm": "Jobs",
"start_date": "2001-01-01",
"end_date": "2001-01-02",
"export_file_format": "JSON",
Expand Down
3 changes: 2 additions & 1 deletion tests/component/lib/Export/FileManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ public function testCreateZipFile(array $request)
$zip = new ZipArchive();
$openCode = $zip->open($zipFile, ZipArchive::CHECKCONS);
$this->assertTrue($openCode, 'Open zip file');
$this->assertEquals(1, $zip->numFiles, 'File count in zip file');
$this->assertEquals(2, $zip->numFiles, 'File count in zip file');
$dataFileName = self::$fileManager->getDataFileName($request);
$this->assertEquals($dataFileName, $zip->getNameIndex(0), 'Data file name');
$this->assertEquals('README.txt', $zip->getNameIndex(1), 'README file name');
$fileData = $zip->getFromName($dataFileName);
$this->assertEquals($testData, $fileData, 'Data file contents');
$zip->close();
Expand Down

0 comments on commit fd941e1

Please sign in to comment.