Skip to content

Commit

Permalink
write data as csv
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Apr 2, 2011
1 parent 14d27e9 commit 7056626
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/CreepyCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
print(count($dataManager->getData()));

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteHourOfDayData.php';
$dataManager->writeData(new WriteHourOfDayData(array()));
$dataManager->writeData(new WriteHourOfDayData(array('file'=>'hourOfDay.csv')));

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteDayOfWeekData.php';
$dataManager->writeData(new WriteDayOfWeekData(array()));
$dataManager->writeData(new WriteDayOfWeekData(array('file'=>'dayOfWeek.csv')));

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteICalData.php';
$dataManager->writeData(new WriteICalData(array('file'=>'out.ical')));
Expand Down
32 changes: 29 additions & 3 deletions src/WriteDayOfWeekData.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,39 @@ class WriteDayOfWeekData extends BaseWriteClass {
public function write() {

$data = array();
$data['Sun'] = $data['Mon'] = $data['Tue'] = $data['Wed'] = $data['Thu'] = $data['Fri'] = $data['Sat'] = 0;

// these are set in the order we want them to appear in the output.
$data['Mon'] = 0;
$data['Tue'] = 0;
$data['Wed'] = 0;
$data['Thu'] = 0;
$data['Fri'] = 0;
$data['Sat'] = 0;
$data['Sun'] = 0;

foreach($this->dataManager->getData() as $item) {
$data[$item->getDateTimeAs('D')]++;
}

print_r($data);
if (isset($this->configData['file']) && $this->configData['file']) {

if (!$handle = fopen($this->configData['file'], 'w')) {
throw new Exception("Cannot open file");
}

$this->fwrite($handle,"Day,Events\r\n");

foreach($data as $day=>$commits) {
$this->fwrite($handle,$day.",".$commits."\r\n");
}

fclose($handle);
}
}

private function fwrite($handle,$data) {
if (fwrite($handle, $data) === FALSE) {
throw new Exception("Cannot write to file");
}
}

}
Expand Down
24 changes: 23 additions & 1 deletion src/WriteHourOfDayData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,30 @@ public function write() {
$data[intval($item->getDateTimeAs('G'))]++;
}

print_r($data);
if (isset($this->configData['file']) && $this->configData['file']) {

if (!$handle = fopen($this->configData['file'], 'w')) {
throw new Exception("Cannot open file");
}

$this->fwrite($handle,"Hour,Events\r\n");

foreach($data as $hour=>$commits) {
$this->fwrite($handle,$hour.",".$commits."\r\n");
}

fclose($handle);
}


}

private function fwrite($handle,$data) {
if (fwrite($handle, $data) === FALSE) {
throw new Exception("Cannot write to file");
}
}


}

0 comments on commit 7056626

Please sign in to comment.