Permalink
Browse files

refactored metric class registration to remove the need for a concret…

…e mclass for each metric. Metric classes can nw be created dynamically via new registerMetricDefinition module class.

Added metric implementions for visits and uniqueVisitors for all fact tables.
fixes #611
  • Loading branch information...
padams
padams committed Mar 7, 2012
1 parent 325ae81 commit cdb4fa094cc8297f50da797926b79333d2f4f92e
@@ -1190,6 +1190,7 @@ function addSelect($select_array) {
$this->params['selects'][] = $select_array;
}
+ //depricated?
function getSelects() {
if (array_key_exists('selects', $this->params)) {
@@ -0,0 +1,58 @@
+<?php
+
+//
+// Open Web Analytics - An Open Source Web Analytics Framework
+//
+// Copyright 2006 Peter Adams. All rights reserved.
+//
+// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// $Id$
+//
+
+/**
+ * Configurable Metric
+ *
+ * This metric produces a count of goal completions for a specific goal number
+ * Goal number is passed into the object dynamicaly when the metric is created.
+ *
+ * @author Peter Adams <peter@openwebanalytics.com>
+ * @copyright Copyright &copy; 2012 Peter Adams http://www.openwebanalytics.com
+ * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
+ * @category owa
+ * @package owa
+ * @version $Revision$
+ * @since owa 1.5.3
+ */
+
+class owa_configurableMetric extends owa_metric {
+
+ function __construct( $params ) {
+
+ $this->setMetricType( $params['metric_type'] );
+ $this->setName( $params['name'] );
+ $this->setLabel( $params['label'] );
+ $this->setDataType( $params['data_type'] );
+
+ if ( $this->isCalculated() ) {
+ foreach ( $params['child_metrics'] as $child ) {
+ $this->setChildMetric( $child );
+ }
+
+ $this->setFormula( $params['formula']);
+ } else {
+ $this->setEntity( $params['entity'] );
+ $this->setColumn( $params['column'] );
+ }
+
+ return parent::__construct();
+ }
+}
+
+?>
View
@@ -282,6 +282,16 @@ function registerAdminPanels() {
*/
function registerMetrics() {
+ $fact_table_entities = array(
+ 'base.action_fact',
+ 'base.request',
+ 'base.session',
+ 'base.domstream',
+ 'base.click',
+ 'base.commerce_transaction_fact',
+ 'base.commerce_line_item_fact'
+ );
+
$this->registerMetric(
'pageViews',
array(
@@ -294,6 +304,8 @@ function registerMetrics() {
'Site Usage'
);
+ // unique visitors
+ /*
$this->registerMetric(
'uniqueVisitors',
'base.uniqueVisitors',
@@ -302,19 +314,52 @@ function registerMetrics() {
'The total number of unique visitors.',
'Site Usage'
);
+ */
+ foreach($fact_table_entities as $factEntity ) {
+
+ $this->registerMetricDefinition(array(
+ 'name' => 'uniqueVisitors',
+ 'label' => 'Unique Visitors',
+ 'description' => 'The total number of unique visitors.',
+ 'group' => 'Site Usage',
+ 'entity' => $factEntity,
+ 'metric_type' => 'distinct_count',
+ 'data_type' => 'integer',
+ 'column' => 'visitor_id'
+
+ ));
+ }
+ // visits
$this->registerMetric(
'visits',
- array(
- 'base.visits',
- 'base.visitsFromRequestFact',
- ),
+ 'base.visits',
'',
'Visits',
'The total number of visits/sessions.',
'Site Usage'
);
+ foreach($fact_table_entities as $factEntity ) {
+
+ // owa_session uses a different column name and has it's own metric registration above.
+ if ($factEntity === 'base.session') {
+ continue;
+ }
+
+ $this->registerMetricDefinition(array(
+ 'name' => 'visits',
+ 'label' => 'Visits',
+ 'description' => 'The total number of visits/sessions.',
+ 'group' => 'Site Usage',
+ 'entity' => $factEntity,
+ 'metric_type' => 'distinct_count', // 'count', 'distinct_count', 'sum', or 'calculated'
+ 'data_type' => 'integer', // 'integrer', 'currency'
+ 'column' => 'session_id'
+
+ ));
+ }
+
$this->registerMetric(
'visitors',
array(
View
@@ -1106,6 +1106,11 @@ function count($column_name) {
return sprintf(OWA_SQL_COUNT, $column_name);
}
+ function sum($column_name) {
+
+ return sprintf(OWA_SQL_SUM, $column_name);
+ }
+
function distinct($column_name) {
return sprintf(OWA_SQL_DISTINCT, $column_name);
View
@@ -247,12 +247,40 @@ function setSelect($column, $as = '') {
function getSelect() {
- return $this->select;
+ if ( $this->select) {
+ // old style metrics populate this explicitly.
+ return $this->select;
+ } else {
+ $db = owa_coreAPI::dbSingleton();
+ switch ( $this->type ) {
+
+ case 'count':
+
+ $statement = $db->count( $this->getColumn() );
+ break;
+
+ case 'distinct_count':
+ $statement = $db->count( $db->distinct( $this->getColumn() ) );
+ break;
+
+ case 'sum':
+ $statement = $db->sum( $this->getColumn() );
+ break;
+ }
+
+ return array( $statement, $this->getName() );
+ }
+
}
function getSelectWithNoAlias() {
- return $this->select[0];
+ if ( $this->select ) {
+ return $this->select[0];
+ } else {
+ $select = $this->getSelect();
+ return $select[0];
+ }
}
function setName($name) {
@@ -322,6 +350,14 @@ function isAggregate() {
return $this->is_aggregate;
}
+
+ function setMetricType( $type ) {
+ $this->type = $type;
+
+ if ( $type === 'calculated' ) {
+ $this->is_calculated = true;
+ }
+ }
}
?>
View
@@ -758,6 +758,107 @@ function registerMetric($metric_name, $classes, $params = array(), $label = '',
}
}
+ /**
+ * Registers a metric definition which is used by the
+ * resultSetExplorer and getResultSet API methods
+ *
+ * This method dynamically creates an owa_metric class and
+ * properly configures it based on the properties passed in.
+ *
+ * Map properties include:
+ *
+ * 'name' => '', // the name of the metric as called via the API
+ * 'label' => '', // the label that will be displayed in result sets
+ * 'description' => '', // the descript displayed in the GUI
+ * 'group' => 'unknown', // the group that this metric will belong to in the UI
+ * 'entity' => '', // the entity to use when calculating this metric
+ * // you must register the same metric for each entity that
+ * // it can be calculated on.
+ * 'metric_type' => '', // 'count', 'distinct_count', 'sum', or 'calculated'
+ * 'data_type' => '', // 'integrer', 'currency', 'average'
+ * 'column' => '', // the column of the entity to use when calculating
+ * 'child_metrics' => array(), // if it's a clculated metric, the child metrics used in the formula.
+ * 'formula' => '' // if it's a calculated metric, the formula to use (e.g. pageViews / visits).
+ *
+ *
+ */
+ function registerMetricDefinition( $params ) {
+
+ $map = array(
+ 'name' => '',
+ 'label' => '',
+ 'description' => '',
+ 'group' => 'unknown',
+ 'entity' => '',
+ 'metric_type' => '',
+ 'data_type' => '',
+ 'column' => '',
+ 'child_metrics' => array(),
+ 'formula' => ''
+ );
+
+ $map = array_intersect_key( array_merge( $map, $params ), $map );
+
+ if ( ! isset( $map['name'] ) ) {
+ // throw exception
+ }
+
+ if ( ! isset( $map['label'] ) ) {
+ $map['label'] = $map['name'];
+ }
+
+ if ( ! isset( $map['entity'] ) ) {
+ // throw exception
+ }
+
+ if ( ! isset( $map['metric_type'] ) ) {
+ // throw exception
+ }
+
+ if ( ! isset( $map['data_type'] ) ) {
+ // throw exception
+ }
+
+ if ( isset( $map['metric_type'] )
+ && $map['metric_type'] != 'calculated'
+ && ! isset( $map['column'] ) )
+ {
+
+ // throw exception
+
+ }
+
+ if ( isset( $map['metric_type'] )
+ && $map['metric_type'] === 'calculated'
+ && ! isset( $map['child_metrics'] ) )
+ {
+
+ // throw exception
+
+ }
+
+ if ( isset( $map['metric_type'] )
+ && $map['metric_type'] === 'calculated'
+ && ! isset( $map['formula'] ) )
+ {
+
+ // throw exception
+
+ }
+
+ $definition = array(
+ 'name' => $map['name'],
+ 'class' => 'base.configurableMetric',
+ 'params' => $map,
+ 'label' => $map['label'],
+ 'description' => $map['description'],
+ 'group' => $map['group']
+ );
+ //print_r($definition);
+ $this->metrics[ $map['name'] ][] = $definition;
+
+ }
+
/**
* Register a dimension
*
@@ -67,6 +67,7 @@
define('OWA_SQL_LIKE', 'LIKE');
define('OWA_SQL_ADD_INDEX', 'ALTER TABLE %s ADD INDEX (%s) %s');
define('OWA_SQL_COUNT', 'COUNT(%s)');
+define('OWA_SQL_SUM', 'SUM(%s)');
define('OWA_SQL_ROUND', 'ROUND(%s)');
define('OWA_SQL_AVERAGE', 'AVG(%s)');
define('OWA_SQL_DISTINCT', 'DISTINCT %s');

0 comments on commit cdb4fa0

Please sign in to comment.