diff --git a/sampleConfig.xml b/sampleConfig.xml index 73dcd93..a34823c 100644 --- a/sampleConfig.xml +++ b/sampleConfig.xml @@ -8,6 +8,7 @@ + diff --git a/src/CreepyCoder.php b/src/CreepyCoder.php index 8a6a777..aadba0e 100644 --- a/src/CreepyCoder.php +++ b/src/CreepyCoder.php @@ -109,3 +109,12 @@ } } +$configList = $xmlDoc->getElementsByTagName('WriteRangeByMonth'); +$configListLength = $configList->length; +if ($configListLength > 0) { + require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteRangeByMonth.php'; + for($pos=0; $pos<$configListLength; $pos++) { + $dataManager->writeData(new WriteRangeByMonth($configList->item($pos))); + } +} + diff --git a/src/WriteRangeByMonth.php b/src/WriteRangeByMonth.php new file mode 100644 index 0000000..1bb42c4 --- /dev/null +++ b/src/WriteRangeByMonth.php @@ -0,0 +1,110 @@ +. +**/ + + +class WriteRangeByMonth 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); + } + + public function write() { + + $data = array(); + + foreach($this->dataManager->getData() as $item) { + $v = $this->commitToDateIdx($item); + if (!isset($data[$v])) $data[$v] = 0; + $data[$v]++; + } + + $minDate = min(array_keys($data)); + $maxDate = max(array_keys($data)); + + + if ($this->graph) { + $maxCommits = max($data); + $dataArray = $labelArray = array(); + for($i = $minDate; $i <= $maxDate; $i++) { + $dataArray[$i] = 0; + $labelArray[$i] = ''; + } + foreach(array($minDate, $maxDate, intval($minDate + ($maxDate - $minDate)*.25),intval($minDate + ($maxDate - $minDate)*.50),intval($minDate + ($maxDate - $minDate)*.75)) as $pos) { + $labelArray[$pos] = $this->dateIdxToDateString($pos); + } + foreach($data as $date=>$commits) { + $dataArray[$date] = intval($commits / $maxCommits * 100); + } + $url = 'http://chart.apis.google.com/chart?cht=bvg&chtt=Range+By+Month'. + '&chbh='.(intVal(350/count($dataArray))-1).',1&chs=500x300'. + '&chxt=x&chxl=0:|'.implode('|',$labelArray).'&chd=t:'.implode(',',$dataArray); + + 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"); + } + + $this->fwrite($handle,"Day,Events\r\n"); + + foreach($data as $idx=>$commits) { + $this->fwrite($handle,$this->dateIdxToDateString($idx).",".$commits."\r\n"); + } + + fclose($handle); + } + } + + private function fwrite($handle,$data) { + if (fwrite($handle, $data) === FALSE) { + throw new Exception("Cannot write to file"); + } + } + + private function commitToDateIdx(CoderAction $item) { + return intval($item->getDateTimeAs('Y')*12) + intval($item->getDateTimeAs('n')) - 1; + } + + private function dateIdxToDateString($idx) { + $year = intval($idx / 12); + $month = ($idx%12)+1; + return $month."/".$year; + } + +} +