Skip to content

Commit

Permalink
adding various stubs for segmentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
padams committed Mar 17, 2011
1 parent fa58700 commit e174ad8
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 79 deletions.
207 changes: 181 additions & 26 deletions modules/base/classes/resultSetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ class owa_resultSetManager extends owa_base {
var $query_params = array();
var $baseEntity;
var $metricObjectsByEntityMap = array();
var $metricObjectsCache = array();
var $errors = array();
var $formatters = array();
var $segment;

function __construct($db = '') {

Expand Down Expand Up @@ -184,34 +186,137 @@ function applyConstraints() {

foreach ($this->getConstraints() as $k => $constraint) {

$dim = $this->lookupDimension($constraint['name'], $this->baseEntity);
// refactor to support metrics in constraints

//$dimEntity = owa_coreAPI::entityFactory($dim['entity']);
if ( $this->isDimension( $constraint['name'] ) ) {

$dim = $this->lookupDimension($constraint['name'], $this->baseEntity);

//$dimEntity = owa_coreAPI::entityFactory($dim['entity']);


$col = $dim['column'];
$constraint['name'] = $col;
$nconstraints[$col] = $constraint;

}

if ( $this->isMetric( $constraint['name'] ) ) {

// get metric object
//$m = $this->metricObjectsByEntityMap[$this->baseEntity->getName()][$metric_name];
// if not calculated
if ( ! $m->isCalculated() ) {
$col = $m->getSelectWithNoAlias();
$constraint['name'] = $col;
$nconstraints[$col] = $constraint;
}
}

$col = $dim['column'];
$constraint['name'] = $col;
$nconstraints[$col] = $constraint;
$this->db->multiWhere($nconstraints);
//print_r($nconstraints);

}

$this->db->multiWhere($nconstraints);

}

function setSegment($segment) {

if ( ! strpos( $segment, 'dynamic' ) ) {
// look up segment from db
$segment = 'something from db';
}

$parsed = $this->constraintsStringToArray( $segment );
$metrics = array();
$dimensions = array();

foreach ( $parsed as $item ) {

if ( $this->isMetric( $item['name'] ) ) {
$metrics[$item['name']] = $item;

// add to all metrics or dimensions array - needed to determin base entity
if ( ! in_array($item['name'], $this->allMetrics) ) {
$this->allMetrics[] = $item['name'];
}

if ( ! in_array($item['name'], $this->allDimensions) ) {
$this->allDimensions[] = $item['name'];
}

} elseif ($this->isDimension( $item['name'] ) ) {
$dimensions[$item['name']] = $item;
}
}

$this->segment = array('metrics' => $metrics, 'dimensions' => $dimensions);
}

function getMetricNamesFromSegment() {

if ( isset($this->segment['metrics'] ) ) {

return array_keys($this->segment['metrics']);
} else {

return array();
}
}

function getDimensionNamesFromSegment() {

if ( isset( $this->segment['dimensions'] ) ) {

return array_keys( $this->segment['dimensions'] );
} else {
return array();
}
}

function generateSegmentTable( $base_entity ) {

$rsm = new owa_resultSetManager;

$rsm->setBaseEntity($base_entity);
//set metrics
$rsm->metrics = $this->getMetricNamesFromSegment();
//set dimensions
$rsm->setDimensions( $this->getDimensionNamesFromSegment() );

if ( $rsm->dimensions ) {
$rsm->applyJoins();
$rsm->applyDimensions();
}

//get all columns. need to optimize to just the columns needed
$rsm->db->select( '*' );
$rsm->db->from( $base_entity->getTableName(), $base_entity->getTableAlias() );
return $rsm->db->generateSelectQuerySql();
}

function chooseBaseEntity() {

$metric_imps = array();

// load metric implementations
foreach ($this->metrics as $metric_name) {
$all_metrics = $this->metrics;

// add in metrics from segment if present
if ( isset($this->segment['metrics'] ) ) {

$metric_imps = array_merge($this->getMetricEntities($metric_name), $metric_imps);
$all_metrics = array_unique( array_merge( $this->metrics, $this->getMetricNamesFromSegment() ) );

}

$all_metrics = array_unique( array_merge( $this->metrics, $this->getMetricNamesFromConstraints() ) );

foreach ($all_metrics as $metric_name) {

$metric_imps = array_merge($this->getMetricEntities($metric_name), $metric_imps);
}
//print_r($metric_imps);

owa_coreAPI::debug('pre-reduce set of entities to choose from: '.print_r($metric_imps, true));

$entities = array();
Expand Down Expand Up @@ -251,7 +356,12 @@ function chooseBaseEntity() {
$error = false;

//cycle through each dimension frm dim list and those found in constraints.
$dims = array_unique(array_merge($this->dimensions, $this->getDimensionsFromConstraints()));
$dims = array_unique( array_merge( $this->dimensions, $this->getDimensionsFromConstraints() ) );

if ( isset( $this->segment['dimensions'] ) ) {

$dims = array_unique( array_merge( $this->dimensions, $this->getDimensionNamesFromSegment() ) );
}

owa_coreAPI::debug(sprintf('Dimensions: %s',print_r($this->dimensions, true)));

Expand Down Expand Up @@ -302,6 +412,11 @@ function getDimensionsFromConstraints() {

return $dims;
}

function getMetricNamesFromConstraints() {

return array();
}

function isDimensionRelated($dimension_name, $entity_name) {

Expand All @@ -328,6 +443,7 @@ function isDimensionRelated($dimension_name, $entity_name) {
}

function getMetricEntities($metric_name) {
owa_coreAPI::debug("getting metric entities for $metric_name");

//get the class implementations
$s = owa_coreAPI::serviceSingleton();
Expand All @@ -340,17 +456,17 @@ function getMetricEntities($metric_name) {
$m = owa_coreAPI::metricFactory($map['class'], $map['params']);

// check to see if this is a calculated metric
if ($m->isCalculated()) {

foreach ($m->getChildMetrics() as $cmetric_name) {
$this->addCalculatedMetric($m);
$entities = array_merge($this->getMetricEntities($cmetric_name), $entities);
}

} else {
$this->metricObjectsByEntityMap[$m->getEntityName()][$metric_name] = $m;
$entities[$metric_name][] = $m->getEntityName();
if ($m->isCalculated()) {

foreach ($m->getChildMetrics() as $cmetric_name) {
$this->addCalculatedMetric($m);
$entities = array_merge($this->getMetricEntities($cmetric_name), $entities);
}

} else {
$this->metricObjectsByEntityMap[$m->getEntityName()][$metric_name] = $m;
$entities[$metric_name][] = $m->getEntityName();
}

}

Expand Down Expand Up @@ -391,6 +507,19 @@ function getDimensionForeignKey($dimension, $entity) {
return $fk;
}
}

function isDimension( $name ) {

$dims = owa_coreAPI::getAllDimensions();
//print_r($dims);
return in_array( $name, array_keys( $dims ) );
}

function isMetric( $name ) {

$metrics = owa_coreAPI::getAllMetrics();
return in_array( $name, array_keys( $metrics ) );
}

function lookupDimension($name, $entity) {

Expand Down Expand Up @@ -1035,7 +1164,13 @@ function getResults() {
$this->applyConstraints();
$this->applySelects();

$this->db->selectFrom($bm->getTableName(), $bm->getTableAlias());
// set from table
if ( $this->segment ) {
$this->db->selectFrom( $this->generateSegmentTable( $bm ), $bm->getTableAlias() );
} else {
$this->db->selectFrom($bm->getTableName(), $bm->getTableAlias());
}

// generate aggregate results
$results = $this->db->getOneRow();
// merge into result set
Expand All @@ -1045,13 +1180,19 @@ function getResults() {

// setup dimensional query
if (!empty($this->dimensions)) {

$this->applyJoins();
// apply dimensional SQL
$this->applyDimensions();

$this->applySelects();

$this->db->selectFrom($bm->getTableName(), $bm->getTableAlias());

// set from table
if ( $this->segment ) {
$this->db->selectFrom( $this->generateSegmentTable( $bm ), $bm->getTableAlias() );
} else {
$this->db->selectFrom($bm->getTableName(), $bm->getTableAlias());
}

// pass limit to db object if one exists
if (!empty($this->limit)) {
Expand Down Expand Up @@ -1212,13 +1353,27 @@ function evalFormula($formula) {
return $value;
}

/**
* Return the approraite metric implementation for the baseEntity
* Must be called after the base entity has been determined
*
* @param string $name the name of the metric
*
*/
function getMetric($name) {

if (in_array($name, $this->metrics)) {
return $this->metricObjectsByEntityMap[$this->baseEntity->getName()][$name];
}
// check to see if the entity object map is loaded
if ( ! in_array( $name, $this->metrics ) ) {
// if not load it forthat metric
$this->getMetricEntities($name);
}

return $this->metricObjectsByEntityMap[$this->baseEntity->getName()][$name];

}



function setQueryStringParam($name, $string) {

$this->query_params[$name] = $string;
Expand Down
2 changes: 1 addition & 1 deletion modules/base/js/owa.report.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ OWA.report.prototype = {
// ...

// bind event handlers
that = this;
var that = this;
jQuery('#owa_reportSiteFilterSelect').change( function() { that.reload(); } );
jQuery("#owa_reportPeriodFilterSubmit").click( function() { that.reload(); } );
},
Expand Down
30 changes: 23 additions & 7 deletions modules/base/js/owa.resultSetExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ OWA.resultSetExplorer.prototype = {
},

refreshGrid : function() {

var that = this;

// custom formattter functions.
Expand All @@ -201,15 +201,25 @@ OWA.resultSetExplorer.prototype = {
//var name = 'actionName';
//alert(that.columnLinks[name].template);
OWA.debug(options.rowId-1+' '+name);
//var new_url = that.columnLinks[name].template.replace('%s', escape(sub_value));
var new_url = that.resultSet.resultsRows[options.rowId-1][name].link;
var link = '<a href="' + new_url + '">' + cellvalue + '</a>';
return link;
//var new_url = that.columnLinks[name].template.replace('%s', escape(sub_value));
//alert(JSON.stringify(that.resultSet.resultsRows[options.rowId-1]));
//alert(options.rowId-1);
//alert(name);
//alert(JSON.stringify(cellvalue));
//alert(JSON.stringify(options.colModel.link_template));
//alert(JSON.stringify(rowdata));
if ( rowdata[name].link.length > 0 ) {
var new_url = rowdata[name].link;
//var new_url = that.resultSet.resultsRows[options.rowId-1][name].link;
var link = '<a href="' + new_url + '">' + cellvalue + '</a>';
return link;
}
},

useServerFormatter : function(cellvalue, options, rowdata) {
var name = options.colModel.realColName;
return that.resultSet.resultsRows[options.rowId-1][name].formatted_value;
return rowdata[name].formatted_value;
//return that.resultSet.resultsRows[options.rowId-1][name].formatted_value;
}

});
Expand Down Expand Up @@ -404,6 +414,7 @@ OWA.resultSetExplorer.prototype = {
var _resizable = true;
var _fixed = false;
var _datefmt = '';
var _link_template = '';

if (column.result_type === 'dimension') {
_align = 'left';
Expand Down Expand Up @@ -432,6 +443,10 @@ OWA.resultSetExplorer.prototype = {
if (this.options.grid.columnFormatters.hasOwnProperty(column.name)) {
_format = this.options.grid.columnFormatters[column.name];
}

if ( this.columnLinks.hasOwnProperty( column.name ) ) {
_link_template = this.columnLinks[column.name].template;
}

var columnDef = {
name: column.name +'.value',
Expand All @@ -445,7 +460,8 @@ OWA.resultSetExplorer.prototype = {
resizable: _resizable,
fixed: _fixed,
realColName: column.name,
datefmt: _datefmt
datefmt: _datefmt,
link_template: _link_template
};

return columnDef;
Expand Down
Loading

0 comments on commit e174ad8

Please sign in to comment.