From 351590d0dec9e7cd10d336bbf0f38a69f74e3adc Mon Sep 17 00:00:00 2001 From: BeezyT <timo@ezdesign.de> Date: Wed, 7 Mar 2012 14:49:06 +0000 Subject: [PATCH] refs #534: using API.getRowEvolution in RowEvolution.php git-svn-id: http://dev.piwik.org/svn/trunk@5986 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- .../CoreHome/DataTableAction/RowEvolution.php | 187 ++++++------------ ...on_pageTitles__API.getRowEvolution_day.xml | 11 +- ...n_pageTitles__API.getRowEvolution_week.xml | 11 +- ...on_pageUrls__API.getRowEvolution_range.xml | 11 +- ...ion_referrer1__API.getRowEvolution_day.xml | 25 +-- ...ion_referrer2__API.getRowEvolution_day.xml | 13 +- ...eportMetadata__API.getRowEvolution_day.xml | 4 + 7 files changed, 76 insertions(+), 186 deletions(-) create mode 100644 tests/integration/expected/test_apiGetReportMetadata__API.getRowEvolution_day.xml diff --git a/plugins/CoreHome/DataTableAction/RowEvolution.php b/plugins/CoreHome/DataTableAction/RowEvolution.php index 7a7612abd1..b0096976d4 100644 --- a/plugins/CoreHome/DataTableAction/RowEvolution.php +++ b/plugins/CoreHome/DataTableAction/RowEvolution.php @@ -33,12 +33,12 @@ class Piwik_CoreHome_DataTableAction_RowEvolution /** The requested date */ protected $date; - /** The meta data for the requested report */ - protected $metaData; - /** The metrics that are available for the requested report and period */ protected $availableMetrics; + /** The name of the dimension of the current report */ + protected $dimension; + /** * The data * @var Piwik_DataTable_Array @@ -78,18 +78,10 @@ class Piwik_CoreHome_DataTableAction_RowEvolution if (empty($this->period)) throw new Exception("Parameter period not set."); $this->idSite = $idSite; - $this->availableMetrics = $this->getAvailableMetrics(); - if ($this->period == 'range') - { - // handle range: display all days in range - $this->period = 'day'; - $this->date = Piwik_Common::getRequestVar('date', ''); - if (empty($this->date)) throw new Exception("Parameter date not set."); - } - else + if ($this->period != 'range') { - // handle day, week, month and year: display last 30 periods + // handle day, week, month and year: display last X periods $end = $date->toString(); if ($this->period == 'year') $start = $date->subYear(10)->toString(); else if ($this->period == 'month') $start = $date->subMonth(30)->toString(); @@ -99,73 +91,6 @@ class Piwik_CoreHome_DataTableAction_RowEvolution } } - /** Get available metrics from metadata API */ - protected function getAvailableMetrics() - { - list($apiModule, $apiAction) = explode('.', $this->apiMethod); - - $this->metaData = Piwik_API_API::getInstance()->getMetadata( - $this->idSite, $apiModule, $apiAction, array(), false, $this->period); - - $this->metaData = $this->metaData[0]; - - $availableMetrics = $this->metaData['metrics']; - if (isset($this->metaData['processedMetrics'])) { - $availableMetrics += $this->metaData['processedMetrics']; - } - - return $availableMetrics; - } - - /** - * Load the data table from the API - * This is only done once, the views (evolution graph and spark lines) all use it - */ - protected function loadDataTable() - { - $dataTable = $this->doLoadDataTable(); - - // remove the label from the data table rows - // otherwise the evolution graph legend would show both the label and the metric name - // this would be too long if multiple metrics are selected - foreach ($dataTable->getArray() as $dayTable) - { - $dataTable->applyQueuedFilters(); - if ($dayTable->getRowsCount() > 0) - { - if (!$this->rowLabel) - { - $this->rowLabel = $dayTable->getFirstRow()->getColumn('label'); - $this->rowIcon = $dayTable->getFirstRow()->getMetadata('logo'); - } - - $dayTable->getFirstRow()->setColumn('label', false); - } - } - - return $dataTable; - } - - /** - * Helper method for only the API call - * Used in MultiRowEvolution as well - * @return Piwik_DataTable_Array - */ - protected function doLoadDataTable() - { - $requestString = 'method='.$this->apiMethod.'&format=original' - . '&date='.$this->date.'&period='.$this->period - . '&label='.urlencode($this->label); - - // add "processed metrics" like actions per visit or bounce rate - if (substr($this->apiMethod, 0, 8) != 'Actions.') { - $requestString .= '&filter_add_columns_when_show_all_columns=1'; - } - - $request = new Piwik_API_Request($requestString); - return $request->process(); - } - /** * Render the popup * @param Piwik_CoreHome_Controller @@ -173,7 +98,7 @@ class Piwik_CoreHome_DataTableAction_RowEvolution */ public function renderPopup($controller, $view) { - $this->dataTable = $this->loadDataTable(); + $this->loadEvolutionReport(); // render main evolution graph $this->graphType = 'graphEvolution'; @@ -188,7 +113,7 @@ class Piwik_CoreHome_DataTableAction_RowEvolution if ($this->rowLabel) { $icon = $this->rowIcon ? '<img src="'.$this->rowIcon.'" alt="">' : ''; - $metricsText .= ' '.$this->metaData['dimension'].': '.$icon.' '.$this->rowLabel; + $metricsText .= ' '.$this->dimension.': '.$icon.' '.$this->rowLabel; } $view->availableMetricsText = $metricsText; @@ -196,6 +121,34 @@ class Piwik_CoreHome_DataTableAction_RowEvolution return $view->render(); } + protected function loadEvolutionReport() + { + list($apiModule, $apiAction) = explode('.', $this->apiMethod); + + $parameters = array( + 'method' => 'API.getRowEvolution', + 'label' => $this->label, + 'apiModule' => $apiModule, + 'apiAction' => $apiAction, + 'idSite' => $this->idSite, + 'period' => $this->period, + 'date' => $this->date, + 'format' => 'original', + 'serialize' => '0' + ); + + $url = Piwik_Url::getQueryStringFromParameters($parameters); + + $request = new Piwik_API_Request($url); + $report = $request->process(); + + $this->dataTable = $report['data']; + $this->rowLabel = $report['label']; + $this->rowIcon = $report['logo']; + $this->availableMetrics = $report['metadata']['metrics']; + $this->dimension = $report['metadata']['dimension']; + } + /** * Generic method to get an evolution graph or a sparkline for the row evolution popup. * Do as much as possible from outside the controller. @@ -205,6 +158,7 @@ class Piwik_CoreHome_DataTableAction_RowEvolution { // update period and date in $_GET because this is what is passed to the export icons // under the evolution graph + // TODO: can we find a way around this? $_GET['period'] = $this->period; $_GET['date'] = $this->date; @@ -213,10 +167,15 @@ class Piwik_CoreHome_DataTableAction_RowEvolution $view->setDataTable($this->dataTable); $view->init('CoreHome', 'getRowEvolutionGraph', $this->apiMethod); $view->setColumnsToDisplay(array_keys($this->graphMetrics)); - $view->setColumnsTranslations($this->graphMetrics); $view->hideAllViewsIcons(); - if (method_exists($view, 'addRowEvolutionSeriesToggle')) { + foreach ($this->availableMetrics as $metric => $metadata) + { + $view->setColumnTranslation($metric, $metadata['name']); + } + + if (method_exists($view, 'addRowEvolutionSeriesToggle')) + { $view->addRowEvolutionSeriesToggle($this->initiallyShowAllMetrics); } @@ -226,73 +185,49 @@ class Piwik_CoreHome_DataTableAction_RowEvolution /** Prepare metrics toggles with spark lines */ protected function getMetricsToggles($controller) { - // calculate meta-metrics - $subDataTables = $this->dataTable->getArray(); - $firstDataTable = current($subDataTables); - $firstDataTableRow = $firstDataTable->getFirstRow(); - $lastDataTable = end($subDataTables); - $lastDataTableRow = $lastDataTable->getFirstRow(); - $maxValues = array(); - $minValues = array(); - foreach ($subDataTables as $subDataTable) - { - // $subDataTable is the report for one period, it has only one row - $firstRow = $subDataTable->getFirstRow(); - foreach ($this->availableMetrics as $metric => $label) - { - $value = $firstRow ? floatval($firstRow->getColumn($metric)) : 0; - if (!isset($minValues[$metric]) || $minValues[$metric] > $value) - { - $minValues[$metric] = $value; - } - if (!isset($maxValues[$metric]) || $maxValues[$metric] < $value) - { - $maxValues[$metric] = $value; - } - } - } - $chart = new Piwik_Visualization_Chart_Evolution; $colors = $chart->getSeriesColors(); - // put together metric info $i = 0; $metrics = array(); - foreach ($this->availableMetrics as $metric => $label) + foreach ($this->availableMetrics as $metric => $metricData) { - if ($maxValues[$metric] == 0 && !($this instanceof Piwik_CoreHome_DataTableAction_MultiRowEvolution)) + $max = $metricData['max']; + $min = $metricData['min']; + $change = $metricData['change']; + + if ($max == 0 && !($this instanceof Piwik_CoreHome_DataTableAction_MultiRowEvolution)) { // series with only 0 cause trouble in js continue; } - $first = $firstDataTableRow ? floatval($firstDataTableRow->getColumn($metric)) : 0; - $last = $lastDataTableRow ? floatval($lastDataTableRow->getColumn($metric)) : 0; - $changePercent = $first > 0 ? round((($last / $first) * 100) - 100) : 100; - $changePercentHtml = $changePercent.'%'; - if ($changePercent > 0) + if (substr($change, 0, 1) == '+') { - $changePercentHtml = '+'.$changePercentHtml; $changeClass = 'up'; $changeImage = 'arrow_up'; } + else if (substr($change, 0, 1) == '-') + { + $changeClass = 'down'; + $changeImage = 'arrow_down'; + } else { - $changeClass = $changePercent < 0 ? 'down' : 'nochange'; - $changeImage = $changePercent < 0 ? 'arrow_down' : false; + $changeClass = 'nochange'; + $changeImage = false; } - $changePercentHtml = '<span class="'.$changeClass.'">' + $change = '<span class="'.$changeClass.'">' .($changeImage ? '<img src="plugins/MultiSites/images/'.$changeImage.'.png" /> ' : '') - .$changePercentHtml.'</span>'; + .$change.'</span>'; - $details = Piwik_Translate('RowEvolution_MetricDetailsText', array( - $minValues[$metric], $maxValues[$metric], $changePercentHtml)); + $details = Piwik_Translate('RowEvolution_MetricDetailsText', array($min, $max, $change)); $color = $colors[ $i % count($colors) ]; $metrics[] = array( - 'label' => $label, + 'label' => $metricData['name'], 'color' => $color, 'details' => $details, 'sparkline' => $this->getSparkline($metric, $controller) diff --git a/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_day.xml b/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_day.xml index 03e6239beb..aa04d3615c 100644 --- a/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_day.xml +++ b/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_day.xml @@ -119,8 +119,6 @@ <name>Pageviews</name> <min>0</min> <max>1</max> - <first>0</first> - <last>1</last> <change>+100%</change> </nb_hits> @@ -128,8 +126,6 @@ <name>Unique Pageviews</name> <min>0</min> <max>1</max> - <first>0</first> - <last>1</last> <change>+100%</change> </nb_visits> @@ -137,8 +133,6 @@ <name>Bounce Rate</name> <min>0</min> <max>100</max> - <first>0</first> - <last>100</last> <change>+100%</change> </bounce_rate> @@ -146,8 +140,6 @@ <name>Avg. time on page</name> <min>0</min> <max>0</max> - <first>0</first> - <last>0</last> <change>0%</change> </avg_time_on_page> @@ -155,13 +147,12 @@ <name>Exit rate</name> <min>0</min> <max>100</max> - <first>0</first> - <last>100</last> <change>+100%</change> </exit_rate> </metrics> + <dimension>Page Name</dimension> </metadata> diff --git a/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_week.xml b/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_week.xml index c4f23e5465..587ea36330 100644 --- a/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_week.xml +++ b/tests/integration/expected/test_RowEvolution_pageTitles__API.getRowEvolution_week.xml @@ -55,8 +55,6 @@ <name>Pageviews</name> <min>2</min> <max>3</max> - <first>2</first> - <last>2</last> <change>0%</change> </nb_hits> @@ -64,8 +62,6 @@ <name>Unique Pageviews</name> <min>2</min> <max>3</max> - <first>2</first> - <last>2</last> <change>0%</change> </nb_visits> @@ -73,8 +69,6 @@ <name>Bounce Rate</name> <min>100</min> <max>100</max> - <first>100</first> - <last>100</last> <change>0%</change> </bounce_rate> @@ -82,8 +76,6 @@ <name>Avg. time on page</name> <min>0</min> <max>0</max> - <first>0</first> - <last>0</last> <change>0%</change> </avg_time_on_page> @@ -91,13 +83,12 @@ <name>Exit rate</name> <min>100</min> <max>100</max> - <first>100</first> - <last>100</last> <change>0%</change> </exit_rate> </metrics> + <dimension>Page Name</dimension> </metadata> diff --git a/tests/integration/expected/test_RowEvolution_pageUrls__API.getRowEvolution_range.xml b/tests/integration/expected/test_RowEvolution_pageUrls__API.getRowEvolution_range.xml index d4f1bb143d..b0e57ebfb5 100644 --- a/tests/integration/expected/test_RowEvolution_pageUrls__API.getRowEvolution_range.xml +++ b/tests/integration/expected/test_RowEvolution_pageUrls__API.getRowEvolution_range.xml @@ -24,8 +24,6 @@ <name>Pageviews</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_hits> @@ -33,8 +31,6 @@ <name>Unique Pageviews</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_visits> @@ -42,8 +38,6 @@ <name>Bounce Rate</name> <min>0</min> <max>100</max> - <first>0</first> - <last>0</last> <change>0%</change> </bounce_rate> @@ -51,8 +45,6 @@ <name>Avg. time on page</name> <min>0</min> <max>0</max> - <first>0</first> - <last>0</last> <change>0%</change> </avg_time_on_page> @@ -60,13 +52,12 @@ <name>Exit rate</name> <min>0</min> <max>100</max> - <first>0</first> - <last>0</last> <change>0%</change> </exit_rate> </metrics> + <dimension>Page URL</dimension> </metadata> diff --git a/tests/integration/expected/test_RowEvolution_referrer1__API.getRowEvolution_day.xml b/tests/integration/expected/test_RowEvolution_referrer1__API.getRowEvolution_day.xml index 1316767489..f6c02dfb30 100644 --- a/tests/integration/expected/test_RowEvolution_referrer1__API.getRowEvolution_day.xml +++ b/tests/integration/expected/test_RowEvolution_referrer1__API.getRowEvolution_day.xml @@ -12,7 +12,7 @@ <nb_actions_per_visit>1</nb_actions_per_visit> <avg_time_on_site>0</avg_time_on_site> <bounce_rate>100%</bounce_rate> - <idsubdatatable>39</idsubdatatable> + </row> </result> <result defaultKeyName="2010-02-08" /> @@ -27,7 +27,7 @@ <nb_actions_per_visit>1</nb_actions_per_visit> <avg_time_on_site>0</avg_time_on_site> <bounce_rate>100%</bounce_rate> - <idsubdatatable>189</idsubdatatable> + </row> </result> <result defaultKeyName="2010-02-13" /> @@ -42,7 +42,7 @@ <nb_actions_per_visit>1</nb_actions_per_visit> <avg_time_on_site>0</avg_time_on_site> <bounce_rate>100%</bounce_rate> - <idsubdatatable>339</idsubdatatable> + </row> </result> <result defaultKeyName="2010-02-18" /> @@ -57,7 +57,7 @@ <nb_actions_per_visit>1</nb_actions_per_visit> <avg_time_on_site>0</avg_time_on_site> <bounce_rate>100%</bounce_rate> - <idsubdatatable>489</idsubdatatable> + </row> </result> <result defaultKeyName="2010-02-23" /> @@ -72,7 +72,7 @@ <nb_actions_per_visit>1</nb_actions_per_visit> <avg_time_on_site>0</avg_time_on_site> <bounce_rate>100%</bounce_rate> - <idsubdatatable>639</idsubdatatable> + </row> </result> <result defaultKeyName="2010-02-28" /> @@ -87,7 +87,7 @@ <nb_actions_per_visit>1</nb_actions_per_visit> <avg_time_on_site>0</avg_time_on_site> <bounce_rate>100%</bounce_rate> - <idsubdatatable>789</idsubdatatable> + </row> </result> <result defaultKeyName="2010-03-05" /> @@ -99,8 +99,6 @@ <name>Visits</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_visits> @@ -108,8 +106,6 @@ <name>Unique visitors</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_uniq_visitors> @@ -117,8 +113,6 @@ <name>Actions</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_actions> @@ -126,8 +120,6 @@ <name>Actions per Visit</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_actions_per_visit> @@ -135,8 +127,6 @@ <name>Avg. Time on Website</name> <min>0</min> <max>0</max> - <first>0</first> - <last>0</last> <change>0%</change> </avg_time_on_site> @@ -144,13 +134,12 @@ <name>Bounce Rate</name> <min>0</min> <max>100</max> - <first>0</first> - <last>0</last> <change>0%</change> </bounce_rate> </metrics> + <dimension>Website</dimension> </metadata> diff --git a/tests/integration/expected/test_RowEvolution_referrer2__API.getRowEvolution_day.xml b/tests/integration/expected/test_RowEvolution_referrer2__API.getRowEvolution_day.xml index 78cf8870d8..f3ed1887fc 100644 --- a/tests/integration/expected/test_RowEvolution_referrer2__API.getRowEvolution_day.xml +++ b/tests/integration/expected/test_RowEvolution_referrer2__API.getRowEvolution_day.xml @@ -66,8 +66,6 @@ <name>Visits</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_visits> @@ -75,8 +73,6 @@ <name>Unique visitors</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_uniq_visitors> @@ -84,8 +80,6 @@ <name>Actions</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_actions> @@ -93,8 +87,6 @@ <name>Actions per Visit</name> <min>0</min> <max>1</max> - <first>0</first> - <last>0</last> <change>0%</change> </nb_actions_per_visit> @@ -102,8 +94,6 @@ <name>Avg. Time on Website</name> <min>0</min> <max>0</max> - <first>0</first> - <last>0</last> <change>0%</change> </avg_time_on_site> @@ -111,13 +101,12 @@ <name>Bounce Rate</name> <min>0</min> <max>100</max> - <first>0</first> - <last>0</last> <change>0%</change> </bounce_rate> </metrics> + <dimension>Website</dimension> </metadata> diff --git a/tests/integration/expected/test_apiGetReportMetadata__API.getRowEvolution_day.xml b/tests/integration/expected/test_apiGetReportMetadata__API.getRowEvolution_day.xml new file mode 100644 index 0000000000..cac1d1f365 --- /dev/null +++ b/tests/integration/expected/test_apiGetReportMetadata__API.getRowEvolution_day.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8" ?> +<result> + <error message="Please specify a value for 'label'." /> +</result> \ No newline at end of file -- GitLab