Skip to content

Commit

Permalink
Fixing Rollup header to work with multiple datasets.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed Nov 3, 2013
1 parent d417474 commit 071653d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
78 changes: 51 additions & 27 deletions classes/headers/RollupHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,87 @@ class RollupHeader extends HeaderBase {
'required'=>true,
'type'=>'object',
'default'=>array()
),
'dataset'=>array(
'required'=>false,
'default'=>0
)
);

public static function init($params, &$report) {
//make sure at least 1 column is defined
if(empty($params['columns'])) throw new Exception("Rollup header needs at least 1 column defined");

//store all the rollup rows in the report
if(!isset($report->options['Rollup'])) $report->options['Rollup'] = array();
$report->options['Rollup'][] = $params;

// If more than one dataset is defined, add the rollup header multiple times
if(is_array($params['dataset'])) {
$new_params = $params;
foreach($params['dataset'] as $dataset) {
$new_params['dataset'] = $dataset;
$report->options['Rollup'][] = $new_params;
}
}
// Otherwise, just add one rollup header
else {
$report->options['Rollup'][] = $params;
}
}

public static function beforeRender(&$report) {
//cache for Twig parameters for each column
public static function beforeRender(&$report) {
//cache for Twig parameters for each dataset/column
$twig_params = array();
foreach($report->options['Rows'] as $row) {
foreach($row['values'] as $value) {
if(!isset($twig_params[$value->key])) $twig_params[$value->key] = array('values'=>array());
$twig_params[$value->key]['values'][] = $value->getValue();

// First get all the values
foreach($report->options['Rollup'] as $rollup) {
// If we already got twig parameters for this dataset, skip it
if(isset($twig_params[$rollup['dataset']])) continue;
$twig_params[$rollup['dataset']] = array();
foreach($report->options['DataSets'][$rollup['dataset']]['rows'] as $row) {
foreach($row['values'] as $value) {
if(!isset($twig_params[$rollup['dataset']][$value->key])) $twig_params[$rollup['dataset']][$value->key] = array('values'=>array());
$twig_params[$rollup['dataset']][$value->key]['values'][] = $value->getValue();
}
}
}
foreach($twig_params as $column=>&$params) {
//get non-null values and sort them
$real_values = array_filter($params['values'],function($a) {if($a === null || $a==='') return false; return true; });
sort($real_values);

$params['sum'] = array_sum($real_values);
$params['count'] = count($real_values);
$params['mean'] = $params['average'] = $params['sum'] / $params['count'];
$params['median'] = ($params['count']%2)? ($real_values[$params['count']/2] + $real_values[$params['count']/2+1])/2 : $real_values[ceil($params['count']/2)];
$params['min'] = $real_values[0];
$params['max'] = $real_values[$params['count']-1];

$devs = array();
foreach($real_values as $v) $devs[] = pow($v - $params['mean'], 2);
$params['stdev'] = sqrt(array_sum($devs) / (count($devs) - 1));
// Then, calculate other statistical properties
foreach($twig_params as $dataset=>&$tp) {
foreach($tp as $column=>&$params) {
//get non-null values and sort them
$real_values = array_filter($params['values'],function($a) {if($a === null || $a==='') return false; return true; });
sort($real_values);

$params['sum'] = array_sum($real_values);
$params['count'] = count($real_values);
$params['mean'] = $params['average'] = $params['sum'] / $params['count'];
$params['median'] = ($params['count']%2)? ($real_values[$params['count']/2] + $real_values[$params['count']/2+1])/2 : $real_values[ceil($params['count']/2)];
$params['min'] = $real_values[0];
$params['max'] = $real_values[$params['count']-1];

$devs = array();
foreach($real_values as $v) $devs[] = pow($v - $params['mean'], 2);
$params['stdev'] = sqrt(array_sum($devs) / (count($devs) - 1));
}
}

if(!isset($report->options['FooterRows'])) $report->options['FooterRows'] = array();

//render each rollup row
foreach($report->options['Rollup'] as $rollup) {
if(!isset($report->options['DataSets'][$rollup['dataset']]['footer'])) $report->options['DataSets'][$rollup['dataset']]['footer'] = array();
$columns = $rollup['columns'];
$row = array(
'values'=>array(),
'rollup'=>true
);

foreach($twig_params as $column=>$p) {
foreach($twig_params[$rollup['dataset']] as $column=>$p) {
if(isset($columns[$column])) {
$row['values'][] = new ReportValue(-1,$column,PhpReports::renderString($columns[$column],$p));
}
else {
$row['values'][] = new ReportValue(-1,$column,null);
}
}
$report->options['FooterRows'][] = $row;
$report->options['DataSets'][$rollup['dataset']]['footer'][] = $row;
}
}
}
8 changes: 1 addition & 7 deletions sample_reports/php/timezones_multiple.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
// Get All Timezones By Region
// INCLUDE: {report: "functions.php"}
// VARIABLE: {
// name: "regions",
// multiple: true,
Expand All @@ -9,6 +8,7 @@
// options: ["cdt","acst","pdt"],
// default: "acst"
// }
// ROLLUP: {columns: {Timezone: "AVERAGE", Offset: "{{mean}}"}, dataset: [0,1]}

$timezone_abbreviations = DateTimeZone::listAbbreviations();

Expand All @@ -30,12 +30,6 @@
);
}

$stats = array_stats($dataset['rows'], 'Offset');
$dataset['rows'][] = array(
'Timezone'=>'AVERAGE',
'Offset'=>$stats['mean']
);

$datasets[] = $dataset;
}

Expand Down

0 comments on commit 071653d

Please sign in to comment.