PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Insights/Model.php

https://github.com/CodeYellowBV/piwik
PHP | 120 lines | 77 code | 24 blank | 19 comment | 9 complexity | 8b695c78ba8a8bd61d976ea1f2ad7413 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\Insights;
  10. use Piwik\API\Request as ApiRequest;
  11. use Piwik\DataTable;
  12. use Piwik\Period\Range;
  13. use Piwik\Plugins\API\ProcessedReport;
  14. use Piwik\Plugins\VisitsSummary\API as VisitsSummaryAPI;
  15. /**
  16. * API for plugin Insights
  17. *
  18. * @method static \Piwik\Plugins\Insights\API getInstance()
  19. */
  20. class Model
  21. {
  22. public function requestReport($idSite, $period, $date, $reportUniqueId, $metric, $segment)
  23. {
  24. $report = $this->getReportByUniqueId($idSite, $reportUniqueId);
  25. $params = array(
  26. 'method' => $report['module'] . '.' . $report['action'],
  27. 'format' => 'original',
  28. 'idSite' => $idSite,
  29. 'period' => $period,
  30. 'date' => $date,
  31. 'filter_limit' => 1000,
  32. 'showColumns' => $metric
  33. );
  34. if (!empty($segment)) {
  35. $params['segment'] = $segment;
  36. }
  37. if (!empty($report['parameters']) && is_array($report['parameters'])) {
  38. $params = array_merge($params, $report['parameters']);
  39. }
  40. $request = new ApiRequest($params);
  41. $table = $request->process();
  42. return $table;
  43. }
  44. public function getLastDate($date, $period, $comparedToXPeriods)
  45. {
  46. $pastDate = Range::getDateXPeriodsAgo(abs($comparedToXPeriods), $date, $period);
  47. if (empty($pastDate[0])) {
  48. throw new \Exception('Not possible to compare this date/period combination');
  49. }
  50. return $pastDate[0];
  51. }
  52. /**
  53. * Returns either the $totalValue (eg 5500 visits) or the total value of the report
  54. * (eg 2500 visits of 5500 total visits as for instance only 2500 visits came to the website using a search engine).
  55. *
  56. * If the metric total (2500) is much lower than $totalValue, the metric total will be returned, otherwise the
  57. * $totalValue
  58. */
  59. public function getRelevantTotalValue(DataTable $currentReport, $metric, $totalValue)
  60. {
  61. $totalMetric = $this->getMetricTotalValue($currentReport, $metric);
  62. if ($totalMetric > $totalValue) {
  63. return $totalMetric;
  64. }
  65. if (($totalMetric * 2) < $totalValue) {
  66. return $totalMetric;
  67. }
  68. return $totalValue;
  69. }
  70. public function getTotalValue($idSite, $period, $date, $metric)
  71. {
  72. $visits = VisitsSummaryAPI::getInstance()->get($idSite, $period, $date, false, array($metric));
  73. $firstRow = $visits->getFirstRow();
  74. if (empty($firstRow)) {
  75. return 0;
  76. }
  77. $totalValue = $firstRow->getColumn($metric);
  78. return (int) $totalValue;
  79. }
  80. public function getMetricTotalValue(DataTable $currentReport, $metric)
  81. {
  82. $totals = $currentReport->getMetadata('totals');
  83. if (!empty($totals[$metric])) {
  84. $totalValue = (int) $totals[$metric];
  85. } else {
  86. $totalValue = 0;
  87. }
  88. return $totalValue;
  89. }
  90. public function getReportByUniqueId($idSite, $reportUniqueId)
  91. {
  92. $processedReport = new ProcessedReport();
  93. $report = $processedReport->getReportMetadataByUniqueId($idSite, $reportUniqueId);
  94. return $report;
  95. }
  96. }