From 1e07fd7b793105e5714906a67ecf89ebed84bb27 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 11 Apr 2011 01:08:26 +0100 Subject: [PATCH] Print csv and graph of hour vs day --- sampleConfig.xml | 1 + src/CreepyCoder.php | 9 ++ src/WriteDayOfWeekAndHourOfDayData.php | 113 +++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/WriteDayOfWeekAndHourOfDayData.php diff --git a/sampleConfig.xml b/sampleConfig.xml index 073c0b9..3f5ea28 100644 --- a/sampleConfig.xml +++ b/sampleConfig.xml @@ -5,6 +5,7 @@ + diff --git a/src/CreepyCoder.php b/src/CreepyCoder.php index a65790b..eb1e8ec 100644 --- a/src/CreepyCoder.php +++ b/src/CreepyCoder.php @@ -89,3 +89,12 @@ } } +$configList = $xmlDoc->getElementsByTagName('WriteDayOfWeekAndHourOfDayData'); +$configListLength = $configList->length; +if ($configListLength > 0) { + require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteDayOfWeekAndHourOfDayData.php'; + for($pos=0; $pos<$configListLength; $pos++) { + $dataManager->writeData(new WriteDayOfWeekAndHourOfDayData($configList->item($pos))); + } +} + diff --git a/src/WriteDayOfWeekAndHourOfDayData.php b/src/WriteDayOfWeekAndHourOfDayData.php new file mode 100644 index 0000000..1902402 --- /dev/null +++ b/src/WriteDayOfWeekAndHourOfDayData.php @@ -0,0 +1,113 @@ +. +**/ + + +class WriteDayOfWeekAndHourOfDayData extends BaseWriteClass { + + + protected $graph; + protected $csv; + + public function __construct($configData = array()) { + if (get_class($configData) == 'DOMElement') { + $this->graph = $configData->getAttribute('GraphFile'); + $this->csv = $configData->getAttribute('CSVFile'); + } + parent::__construct($configData); + } + + private $days = array('1'=>'Mon','2'=>'Tue','3'=>'Wed','4'=>'Thu','5'=>'Fri','6'=>'Sat','7'=>'Sun',); + + + public function write() { + + $hourData = array(); + for ($i = 0; $i <= 23; $i++) $hourData[$i] = 0; + + $data = array(); + for ($i = 1; $i <= 7; $i++) $data[$i] = $hourData; + + foreach($this->dataManager->getData() as $item) { + $data[$item->getDateTimeAs('N')][$item->getDateTimeAs('G')]++; + } + + if ($this->graph) { + $maxCommits = 0; + foreach($data as $day=>$hourData) { + foreach($hourData as $hour=>$commits) { + $maxCommits = max($maxCommits, $commits); + } + } + + + $xAxis = $yAxis = $sizeAxis = array(); + foreach($data as $day=>$hourData) { + foreach($hourData as $hour=>$commits) { + if ($commits > 0) { + $xAxis[] = intval($hour/23*100); + $yAxis[] = intval($day/7*100); + $sizeAxis[] = intval($commits/$maxCommits*100); + } + } + } + + $url = 'http://chart.apis.google.com/chart?cht=s&chtt=Day+And+Hour&chxt=x,y&chs=800x300'. + '&chxr=0,0,23,1&chxl=1:||'.implode('|', array_values($this->days)). + '&chd=t:'.implode(",", $xAxis)."|".implode(",", $yAxis)."|".implode(",", $sizeAxis); + + print "URL: $url \n\n"; + + $ch = curl_init($url); + $fp = fopen($this->graph, "w"); + curl_setopt($ch, CURLOPT_FILE, $fp); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_exec($ch); + curl_close($ch); + fclose($fp); + + } + + if ($this->csv) { + + if (!$handle = fopen($this->csv, 'w')) { + throw new Exception("Cannot open file"); + } + + for ($i = 0; $i <= 23; $i++) $this->fwrite($handle,",".$i); + $this->fwrite($handle,"\n"); + + foreach($data as $day=>$hourData) { + $this->fwrite($handle,$this->days[$day].","); + foreach($hourData as $hour=>$commits) { + $this->fwrite($handle,$commits.","); + } + $this->fwrite($handle,"\r\n"); + } + + fclose($handle); + } + } + + private function fwrite($handle,$data) { + if (fwrite($handle, $data) === FALSE) { + throw new Exception("Cannot write to file"); + } + } + +} +