build() 공개 정적인 메소드

This method uses data that is found in query parameters, so the parameters to this function can be string values. If you want to create an Archive instance with an array of Period instances, use {@link Archive::factory()}.
public static build ( string | integer | array $idSites, string $period, Date | string $strDate, boolean | false | string $segment = false, boolean | false | string $_restrictSitesToLogin = false ) : static
$idSites string | integer | array A single ID (eg, `'1'`), multiple IDs (eg, `'1,2,3'` or `array(1, 2, 3)`), or `'all'`.
$period string 'day', `'week'`, `'month'`, `'year'` or `'range'`
$strDate Date | string 'YYYY-MM-DD', magic keywords (ie, 'today'; {@link Date::factory()} or date range (ie, 'YYYY-MM-DD,YYYY-MM-DD').
$segment boolean | false | string Segment definition or false if no segment should be used. {@link Piwik\Segment}
$_restrictSitesToLogin boolean | false | string Used only when running as a scheduled task.
리턴 static
예제 #1
0
파일: API.php 프로젝트: piwik/piwik
 protected function getNumeric($idSite, $period, $date, $segment, $toFetch)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $dataTable = $archive->getDataTableFromNumeric($toFetch);
     return $dataTable;
 }
예제 #2
0
파일: API.php 프로젝트: a4tunado/piwik
 protected function getDataTable($name, $idSite, $period, $date, $segment, $column = Metrics::INDEX_NB_VISITS)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $dataTable = $archive->getDataTable($name);
     $dataTable->queueFilter('ReplaceColumnNames');
     return $dataTable;
 }
예제 #3
0
파일: API.php 프로젝트: diosmosis/piwik
 /**
  * Get a report of all User Ids.
  *
  * @param int $idSite
  *
  * @param string  $period
  * @param int  $date
  * @param string|bool  $segment
  *
  * @return DataTable
  */
 public function getUsers($idSite, $period, $date, $segment = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $dataTable = $archive->getDataTable(Archiver::USERID_ARCHIVE_RECORD);
     $dataTable->queueFilter('ReplaceColumnNames');
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
     return $dataTable;
 }
예제 #4
0
 /**
  * @param string $name
  * @param int $idSite
  * @param string $period
  * @param string $date
  * @param string $segment
  * @return DataTable
  */
 protected function getDataTable($name, $idSite, $period, $date, $segment)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $dataTable = $archive->getDataTable($name);
     $dataTable->queueFilter('ReplaceColumnNames');
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
     return $dataTable;
 }
예제 #5
0
 /**
  * Test that restricting the number of sites to those viewable to another login
  * works when building an archive query object.
  * 
  * @group        Integration
  */
 public function testArchiveSitesWhenRestrictingToLogin()
 {
     try {
         Archive::build('all', 'day', self::$fixture->dateTime, $segment = false, $_restrictToLogin = '******');
         $this->fail("Restricting sites to invalid login did not return 0 sites.");
     } catch (Exception $ex) {
         // pass
     }
 }
예제 #6
0
 /**
  * API method that returns number of visits based on hour parity.
  * @param int    $idSite
  * @param string $period
  * @param string $date
  * @param bool|string $segment
  * @return DataTable
  */
 public function getEvenUnevenTimes($idSite, $period, $date, $segment = false)
 {
     $archive = Archive::build($idSite, $period, $date);
     $oddHoursCount = $archive->getNumeric(Archiver::ODD_HOURS_COUNT_RECORD_NAME);
     $evenHoursCount = $archive->getNumeric(Archiver::EVEN_HOURS_COUNT_RECORD_NAME);
     $table = new DataTable();
     $table->setTableSortedBy('times');
     $table->addRowFromArray(array(Row::COLUMNS => array('times' => Piwik::translate('HourParity_Oddhours'), 'nb_visits' => $oddHoursCount)));
     $table->addRowFromArray(array(Row::COLUMNS => array('times' => Piwik::translate('HourParity_Evenhours'), 'nb_visits' => $evenHoursCount)));
     return $table;
 }
예제 #7
0
 public function getProvider($idSite, $period, $date, $segment = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $dataTable = $archive->getDataTable(Archiver::PROVIDER_RECORD_NAME);
     $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'url', __NAMESPACE__ . '\\getHostnameUrl'));
     $dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\\getPrettyProviderName'));
     $dataTable->queueFilter('ReplaceColumnNames');
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
     return $dataTable;
 }
예제 #8
0
 /**
  * Returns the list of metrics (pages, downloads, outlinks)
  *
  * @param int $idSite
  * @param string $period
  * @param string $date
  * @param bool|string $segment
  * @param bool|array $columns
  * @return DataTable
  */
 public function get($idSite, $period, $date, $segment = false, $columns = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $metrics = Archiver::$actionsAggregateMetrics;
     $metrics['Actions_avg_time_generation'] = 'avg_time_generation';
     // get requested columns
     $columns = Piwik::getArrayFromApiParameter($columns);
     if (!empty($columns)) {
         // get the columns that are available and requested
         $columns = array_intersect($columns, array_values($metrics));
         $columns = array_values($columns);
         // make sure indexes are right
         $nameReplace = array();
         foreach ($columns as $i => $column) {
             $fullColumn = array_search($column, $metrics);
             $columns[$i] = $fullColumn;
             $nameReplace[$fullColumn] = $column;
         }
         if (false !== ($avgGenerationTimeRequested = array_search('Actions_avg_time_generation', $columns))) {
             unset($columns[$avgGenerationTimeRequested]);
             $avgGenerationTimeRequested = true;
         }
     } else {
         // get all columns
         unset($metrics['Actions_avg_time_generation']);
         $columns = array_keys($metrics);
         $nameReplace =& $metrics;
         $avgGenerationTimeRequested = true;
     }
     if ($avgGenerationTimeRequested) {
         $tempColumns[] = Archiver::METRIC_SUM_TIME_RECORD_NAME;
         $tempColumns[] = Archiver::METRIC_HITS_TIMED_RECORD_NAME;
         $columns = array_merge($columns, $tempColumns);
         $columns = array_unique($columns);
         $nameReplace[Archiver::METRIC_SUM_TIME_RECORD_NAME] = 'sum_time_generation';
         $nameReplace[Archiver::METRIC_HITS_TIMED_RECORD_NAME] = 'nb_hits_with_time_generation';
     }
     $table = $archive->getDataTableFromNumeric($columns);
     // replace labels (remove Actions_)
     $table->filter('ReplaceColumnNames', array($nameReplace));
     // compute avg generation time
     if ($avgGenerationTimeRequested) {
         $table->filter('ColumnCallbackAddColumnQuotient', array('avg_time_generation', 'sum_time_generation', 'nb_hits_with_time_generation', 3));
         $table->deleteColumns(array('sum_time_generation', 'nb_hits_with_time_generation'));
     }
     return $table;
 }
예제 #9
0
 public function getPlugin($idSite, $period, $date, $segment = false)
 {
     // fetch all archive data required
     $dataTable = $this->getDataTable(Archiver::PLUGIN_RECORD_NAME, $idSite, $period, $date, $segment);
     $browserVersions = $this->getDataTable(DDArchiver::BROWSER_VERSION_RECORD_NAME, $idSite, $period, $date, $segment);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $visitsSums = $archive->getDataTableFromNumeric('nb_visits');
     // check whether given tables are arrays
     if ($dataTable instanceof DataTable\Map) {
         $dataTableMap = $dataTable->getDataTables();
         $browserVersionsArray = $browserVersions->getDataTables();
         $visitSumsArray = $visitsSums->getDataTables();
     } else {
         $dataTableMap = array($dataTable);
         $browserVersionsArray = array($browserVersions);
         $visitSumsArray = array($visitsSums);
     }
     // walk through the results and calculate the percentage
     foreach ($dataTableMap as $key => $table) {
         // Calculate percentage, but ignore IE users because plugin detection doesn't work on IE
         $ieVisits = 0;
         $browserVersionsToExclude = array('IE;10.0', 'IE;9.0', 'IE;8.0', 'IE;7.0', 'IE;6.0');
         foreach ($browserVersionsToExclude as $browserVersionToExclude) {
             $ieStats = $browserVersionsArray[$key]->getRowFromLabel($browserVersionToExclude);
             if ($ieStats !== false) {
                 $ieVisits += $ieStats->getColumn(Metrics::INDEX_NB_VISITS);
             }
         }
         // get according visitsSum
         $visits = $visitSumsArray[$key];
         if ($visits->getRowsCount() == 0) {
             $visitsSumTotal = 0;
         } else {
             $visitsSumTotal = (double) $visits->getFirstRow()->getColumn('nb_visits');
         }
         $visitsSum = $visitsSumTotal - $ieVisits;
         $extraProcessedMetrics = $table->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME);
         $extraProcessedMetrics[] = new VisitsPercent($visitsSum);
         $table->setMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME, $extraProcessedMetrics);
     }
     $dataTable->queueFilter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\\getPluginsLogo'));
     $dataTable->queueFilter('ColumnCallbackReplace', array('label', 'ucfirst'));
     $dataTable->queueFilter('RangeCheck', array('nb_visits_percentage', 0, 1));
     return $dataTable;
 }
예제 #10
0
 /**
  * Returns the list of metrics (pages, downloads, outlinks)
  *
  * @param int $idSite
  * @param string $period
  * @param string $date
  * @param bool|string $segment
  * @param bool|array $columns
  * @return DataTable
  */
 public function get($idSite, $period, $date, $segment = false, $columns = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $report = Report::factory("Actions", "get");
     $archive = Archive::build($idSite, $period, $date, $segment);
     $requestedColumns = Piwik::getArrayFromApiParameter($columns);
     $columns = $report->getMetricsRequiredForReport($allColumns = null, $requestedColumns);
     $inDbColumnNames = array_map(function ($value) {
         return 'Actions_' . $value;
     }, $columns);
     $dataTable = $archive->getDataTableFromNumeric($inDbColumnNames);
     $dataTable->deleteColumns(array_diff($requestedColumns, $columns));
     $newNameMapping = array_combine($inDbColumnNames, $columns);
     $dataTable->filter('ReplaceColumnNames', array($newNameMapping));
     $columnsToShow = $requestedColumns ?: $report->getAllMetrics();
     $dataTable->queueFilter('ColumnDelete', array($columnsToRemove = array(), $columnsToShow));
     return $dataTable;
 }
예제 #11
0
파일: API.php 프로젝트: carriercomm/piwik
 /**
  * Returns datatable describing the number of visits for each day of the week.
  *
  * @param string $idSite The site ID. Cannot refer to multiple sites.
  * @param string $period The period type: day, week, year, range...
  * @param string $date The start date of the period. Cannot refer to multiple dates.
  * @param bool|string $segment The segment.
  * @throws Exception
  * @return DataTable
  */
 public function getByDayOfWeek($idSite, $period, $date, $segment = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     // metrics to query
     $metrics = Metrics::getVisitsMetricNames();
     unset($metrics[Metrics::INDEX_MAX_ACTIONS]);
     // disabled for multiple dates
     if (Period::isMultiplePeriod($date, $period)) {
         throw new Exception("VisitTime.getByDayOfWeek does not support multiple dates.");
     }
     // get metric data for every day within the supplied period
     $oPeriod = Period\Factory::makePeriodFromQueryParams(Site::getTimezoneFor($idSite), $period, $date);
     $dateRange = $oPeriod->getDateStart()->toString() . ',' . $oPeriod->getDateEnd()->toString();
     $archive = Archive::build($idSite, 'day', $dateRange, $segment);
     // disabled for multiple sites
     if (count($archive->getParams()->getIdSites()) > 1) {
         throw new Exception("VisitTime.getByDayOfWeek does not support multiple sites.");
     }
     $dataTable = $archive->getDataTableFromNumeric($metrics)->mergeChildren();
     // if there's no data for this report, don't bother w/ anything else
     if ($dataTable->getRowsCount() == 0) {
         return $dataTable;
     }
     // group by the day of the week (see below for dayOfWeekFromDate function)
     $dataTable->filter('GroupBy', array('label', __NAMESPACE__ . '\\dayOfWeekFromDate'));
     // create new datatable w/ empty rows, then add calculated datatable
     $rows = array();
     foreach (array(1, 2, 3, 4, 5, 6, 7) as $day) {
         $rows[] = array('label' => $day, 'nb_visits' => 0);
     }
     $result = new DataTable();
     $result->addRowsFromSimpleArray($rows);
     $result->addDataTable($dataTable);
     // set day of week integer as metadata
     $result->filter('ColumnCallbackAddMetadata', array('label', 'day_of_week'));
     // translate labels
     $result->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\\translateDayOfWeek'));
     // set datatable metadata for period start & finish
     $result->setMetadata('date_start', $oPeriod->getDateStart());
     $result->setMetadata('date_end', $oPeriod->getDateEnd());
     return $result;
 }
예제 #12
0
 public function test_subtables_willBeSplitIntoChunks()
 {
     $numSubtablesToGenerate = 1053;
     $blobs = $this->generateDataTableWithManySubtables($numSubtablesToGenerate);
     $this->assertCount($numSubtablesToGenerate + 1, $blobs);
     // +1 for the root table
     $recordName = 'Actions_MyRecord';
     $archiver = $this->createPluginsArchiver();
     $archiver->archiveProcessor->insertBlobRecord($recordName, $blobs);
     $archiver->finalizeArchive();
     // verify they were split into chunks
     $archiveRows = $this->getAllRowsFromArchiveBlobTable('name');
     $expectedArchiveNames = array($recordName, $recordName . '_chunk_0_99', $recordName . '_chunk_1000_1099', $recordName . '_chunk_100_199', $recordName . '_chunk_200_299', $recordName . '_chunk_300_399', $recordName . '_chunk_400_499', $recordName . '_chunk_500_599', $recordName . '_chunk_600_699', $recordName . '_chunk_700_799', $recordName . '_chunk_800_899', $recordName . '_chunk_900_999');
     $this->assertEquals($expectedArchiveNames, array_column($archiveRows, 'name'));
     // verify all have same archiveIds
     $expectedArchiveIds = array_fill(0, count($expectedArchiveNames), $archiveId = '1');
     $this->assertEquals($expectedArchiveIds, array_column($archiveRows, 'idarchive'));
     // verify the subtables were actually splitted into chunks
     foreach ($archiveRows as $row) {
         $value = unserialize(gzuncompress($row['value']));
         $this->assertTrue(is_array($value));
         if ($row['name'] == $recordName) {
             $this->assertCount($numSubtablesToGenerate, $value);
             // 1053 rows
         } elseif ($row['name'] == $recordName . '_chunk_1000_1099') {
             $this->assertCount($numSubtablesToGenerate % Archive\Chunk::NUM_TABLES_IN_CHUNK + 1, $value);
             // 53 subtables
         } elseif ($row['name'] == $recordName . '_chunk_0_99') {
             $this->assertCount(Archive\Chunk::NUM_TABLES_IN_CHUNK - 1, $value);
             // one less as we do not store the root table here
         } else {
             $this->assertCount(Archive\Chunk::NUM_TABLES_IN_CHUNK, $value);
         }
     }
     // should be able to rebuild the datatable
     $archive = Archive::build(1, 'day', $this->date);
     $table = $archive->getDataTable($recordName);
     $this->assertSame(1053, $table->getRowsCount());
     $this->assertSame('Label Test 1', $table->getFirstRow()->getColumn('label'));
     $this->assertSame(1, $table->getFirstRow()->getColumn('nb_visits'));
 }
예제 #13
0
파일: API.php 프로젝트: carriercomm/piwik
 public function getPlugin($idSite, $period, $date, $segment = false)
 {
     // fetch all archive data required
     $dataTable = $this->getDataTable(Archiver::PLUGIN_RECORD_NAME, $idSite, $period, $date, $segment);
     $browserTypes = $this->getDataTable(Archiver::BROWSER_TYPE_RECORD_NAME, $idSite, $period, $date, $segment);
     $archive = Archive::build($idSite, $period, $date, $segment);
     $visitsSums = $archive->getDataTableFromNumeric('nb_visits');
     // check whether given tables are arrays
     if ($dataTable instanceof DataTable\Map) {
         $dataTableMap = $dataTable->getDataTables();
         $browserTypesArray = $browserTypes->getDataTables();
         $visitSumsArray = $visitsSums->getDataTables();
     } else {
         $dataTableMap = array($dataTable);
         $browserTypesArray = array($browserTypes);
         $visitSumsArray = array($visitsSums);
     }
     // walk through the results and calculate the percentage
     foreach ($dataTableMap as $key => $table) {
         // get according browserType table
         foreach ($browserTypesArray as $k => $browsers) {
             if ($k == $key) {
                 $browserType = $browsers;
             }
         }
         // get according visitsSum
         foreach ($visitSumsArray as $k => $visits) {
             if ($k == $key) {
                 if (is_object($visits)) {
                     if ($visits->getRowsCount() == 0) {
                         $visitsSumTotal = 0;
                     } else {
                         $visitsSumTotal = (double) $visits->getFirstRow()->getColumn('nb_visits');
                     }
                 } else {
                     $visitsSumTotal = (double) $visits;
                 }
             }
         }
         // Calculate percentage, but ignore IE users because plugin detection doesn't work on IE
         $ieVisits = 0;
         $ieStats = $browserType->getRowFromLabel('ie');
         if ($ieStats !== false) {
             $ieVisits = $ieStats->getColumn(Metrics::INDEX_NB_VISITS);
         }
         $visitsSum = $visitsSumTotal - $ieVisits;
         // When Truncate filter is applied, it will call AddSummaryRow which tries to sum all rows.
         // We tell the object to skip the column nb_visits_percentage when aggregating (since it's not correct to sum % values)
         $columnAggregationOps = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME);
         $columnAggregationOps['nb_visits_percentage'] = 'skip';
         $table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnAggregationOps);
         // The filter must be applied now so that the new column can
         // be sorted by the generic filters (applied right after this loop exits)
         $table->filter('ColumnCallbackAddColumnPercentage', array('nb_visits_percentage', Metrics::INDEX_NB_VISITS, $visitsSum, 1));
         $table->filter('RangeCheck', array('nb_visits_percentage'));
     }
     $dataTable->queueFilter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\\getPluginsLogo'));
     $dataTable->queueFilter('ColumnCallbackReplace', array('label', 'ucfirst'));
     return $dataTable;
 }
 public function test_Archive_getNumeric_shouldInvalidateRememberedReportsOncePerRequestIfNeeded()
 {
     $this->markTestSkippedOnPhp53();
     // Tests that getting a visits summary metric (nb_visits) & a Goal's metric (Goal_revenue)
     // at the same time works.
     $dateTimeRange = '2010-01-03,2010-01-06';
     $columns = array('nb_visits', 'Goal_nb_conversions', 'nb_actions');
     $idSite1 = self::$fixture->idSite1;
     $archive = Archive::build($idSite1, 'range', $dateTimeRange);
     $result = $archive->getNumeric($columns);
     $this->assertEquals(array('nb_visits' => 5, 'Goal_nb_conversions' => 6, 'nb_actions' => 13), $result);
     $cache = Cache::getTransientCache();
     $this->assertEquals(array(self::$fixture->idSite1, self::$fixture->idSite2), $cache->fetch('Archive.SiteIdsOfRememberedReportsInvalidated'));
     $invalidator = StaticContainer::get('Piwik\\Archive\\ArchiveInvalidator');
     self::$fixture->trackVisits();
     // trackVisits should remember to invalidate archived reports
     $this->assertNotEmpty($invalidator->getRememberedArchivedReportsThatShouldBeInvalidated());
     // although there were new tracked visists it doesn'T change as the report invalidation is cached and was
     // already invalidated in previous Archive::get();
     $archive = Archive::build($idSite1, 'range', $dateTimeRange);
     $result = $archive->getNumeric($columns);
     $this->assertEquals(array('nb_visits' => 5, 'Goal_nb_conversions' => 6, 'nb_actions' => 13), $result);
     // make sure the caching in archive::get() worked and they are still to be invalidated
     $this->assertCount(10, $invalidator->getRememberedArchivedReportsThatShouldBeInvalidated());
     // now we force to actually invalidate archived reports again and then archive will be rebuilt for requsted siteId = 1
     $cache->delete('Archive.SiteIdsOfRememberedReportsInvalidated');
     $archive = Archive::build($idSite1, 'range', $dateTimeRange);
     $result = $archive->getNumeric($columns);
     // archive::get() should have invalidated siteId 1 and siteId 2 should be still to be done
     $expectedArchiveReportsLeft = array('2010-01-04' => array(2));
     $this->assertEquals($expectedArchiveReportsLeft, $invalidator->getRememberedArchivedReportsThatShouldBeInvalidated());
     $this->assertEquals(array('nb_visits' => 6, 'Goal_nb_conversions' => 7, 'nb_actions' => 26), $result);
 }
예제 #15
0
파일: Archive.php 프로젝트: piwik/piwik
 /**
  * Helper function that creates an Archive instance and queries for report data using
  * query parameter data. API methods can use this method to reduce code redundancy.
  *
  * @param string $recordName The name of the report to return.
  * @param int|string|array $idSite @see {@link build()}
  * @param string $period @see {@link build()}
  * @param string $date @see {@link build()}
  * @param string $segment @see {@link build()}
  * @param bool $expanded If true, loads all subtables. See {@link getDataTableExpanded()}
  * @param bool $flat If true, loads all subtables and disabled all recursive filters.
  * @param int|null $idSubtable See {@link getDataTableExpanded()}
  * @param int|null $depth See {@link getDataTableExpanded()}
  * @return DataTable|DataTable\Map
  */
 public static function createDataTableFromArchive($recordName, $idSite, $period, $date, $segment, $expanded = false, $flat = false, $idSubtable = null, $depth = null)
 {
     Piwik::checkUserHasViewAccess($idSite);
     if ($flat && !$idSubtable) {
         $expanded = true;
     }
     $archive = Archive::build($idSite, $period, $date, $segment, $_restrictSitesToLogin = false);
     if ($idSubtable === false) {
         $idSubtable = null;
     }
     if ($expanded) {
         $dataTable = $archive->getDataTableExpanded($recordName, $idSubtable, $depth);
     } else {
         $dataTable = $archive->getDataTable($recordName, $idSubtable);
     }
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
     $dataTable->queueFilter('ReplaceColumnNames');
     if ($expanded) {
         $dataTable->queueFilterSubtables('ReplaceColumnNames');
     }
     if ($flat) {
         $dataTable->disableRecursiveFilters();
     }
     return $dataTable;
 }
예제 #16
0
파일: API.php 프로젝트: brienomatty/elmsln
 /**
  * Utility method that retrieve an archived DataTable for a specific site, date range,
  * segment and goal. If not goal is specified, this method will retrieve and sum the
  * data for every goal.
  *
  * @param string $recordName The archive entry name.
  * @param int|string $idSite The site(s) to select data for.
  * @param string $period The period type.
  * @param string $date The date type.
  * @param string $segment The segment.
  * @param int|bool $idGoal The id of the goal to get data for. If this is set to false,
  *                         data for every goal that belongs to $idSite is returned.
  * @return false|DataTable
  */
 protected function getGoalSpecificDataTable($recordName, $idSite, $period, $date, $segment, $idGoal)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     // check for the special goal ids
     $realGoalId = $idGoal != true ? false : self::convertSpecialGoalIds($idGoal);
     // get the data table
     $dataTable = $archive->getDataTable(Archiver::getRecordName($recordName, $realGoalId), $idSubtable = null);
     $dataTable->queueFilter('ReplaceColumnNames');
     return $dataTable;
 }
예제 #17
0
 /**
  * Helper function that creates an Archive instance and queries for report data using
  * query parameter data. API methods can use this method to reduce code redundancy.
  *
  * @param string $name The name of the report to return.
  * @param int|string|array $idSite @see {@link build()}
  * @param string $period @see {@link build()}
  * @param string $date @see {@link build()}
  * @param string $segment @see {@link build()}
  * @param bool $expanded If true, loads all subtables. See {@link getDataTableExpanded()}
  * @param int|null $idSubtable See {@link getDataTableExpanded()}
  * @param int|null $depth See {@link getDataTableExpanded()}
  * @throws \Exception
  * @return DataTable|DataTable\Map See {@link getDataTable()} and
  *                                 {@link getDataTableExpanded()} for more
  *                                 information
  * @deprecated Since Piwik 2.12.0 Use Archive::createDataTableFromArchive() instead
  */
 public static function getDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $idSubtable = null, $depth = null)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment, $_restrictSitesToLogin = false);
     if ($idSubtable === false) {
         $idSubtable = null;
     }
     if ($expanded) {
         $dataTable = $archive->getDataTableExpanded($name, $idSubtable, $depth);
     } else {
         $dataTable = $archive->getDataTable($name, $idSubtable);
     }
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
     return $dataTable;
 }
예제 #18
0
 private function getNumeric($name, $idSite, $period, $date, $segment)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     return $archive->getDataTableFromNumeric($name);
 }
예제 #19
0
파일: API.php 프로젝트: bossrabbit/piwik
 private function buildDataTable($sitesToProblablyAdd, $period, $date, $segment, $_restrictSitesToLogin, $enhanced, $multipleWebsitesRequested, $showColumns)
 {
     $idSites = array();
     if (!empty($sitesToProblablyAdd)) {
         foreach ($sitesToProblablyAdd as $site) {
             $idSites[] = $site['idsite'];
         }
     }
     // build the archive type used to query archive data
     $archive = Archive::build($idSites, $period, $date, $segment, $_restrictSitesToLogin);
     // determine what data will be displayed
     $fieldsToGet = array();
     $columnNameRewrites = array();
     $apiECommerceMetrics = array();
     $apiMetrics = API::getApiMetrics($enhanced);
     foreach ($apiMetrics as $metricName => $metricSettings) {
         if (!empty($showColumns) && !in_array($metricName, $showColumns)) {
             unset($apiMetrics[$metricName]);
             continue;
         }
         $fieldsToGet[] = $metricSettings[self::METRIC_RECORD_NAME_KEY];
         $columnNameRewrites[$metricSettings[self::METRIC_RECORD_NAME_KEY]] = $metricName;
         if ($metricSettings[self::METRIC_IS_ECOMMERCE_KEY]) {
             $apiECommerceMetrics[$metricName] = $metricSettings;
         }
     }
     $dataTable = $archive->getDataTableFromNumericAndMergeChildren($fieldsToGet);
     $this->populateLabel($dataTable);
     $totalMetrics = $this->preformatApiMetricsForTotalsCalculation($apiMetrics);
     $this->setMetricsTotalsMetadata($dataTable, $totalMetrics);
     // if the period isn't a range & a lastN/previousN date isn't used, we get the same
     // data for the last period to show the evolution of visits/actions/revenue
     list($strLastDate, $lastPeriod) = Range::getLastDate($date, $period);
     if ($strLastDate !== false) {
         if ($lastPeriod !== false) {
             // NOTE: no easy way to set last period date metadata when range of dates is requested.
             //       will be easier if DataTable\Map::metadata is removed, and metadata that is
             //       put there is put directly in DataTable::metadata.
             $dataTable->setMetadata(self::getLastPeriodMetadataName('date'), $lastPeriod);
         }
         $pastArchive = Archive::build($idSites, $period, $strLastDate, $segment, $_restrictSitesToLogin);
         $pastData = $pastArchive->getDataTableFromNumericAndMergeChildren($fieldsToGet);
         $this->populateLabel($pastData);
         // labels are needed to calculate evolution
         $this->calculateEvolutionPercentages($dataTable, $pastData, $apiMetrics);
         $this->setPastTotalVisitsMetadata($dataTable, $pastData);
         if ($dataTable instanceof DataTable) {
             // needed for MultiSites\Dashboard
             $dataTable->setMetadata('pastData', $pastData);
         }
     }
     // move the site id to a metadata column
     $dataTable->queueFilter('MetadataCallbackAddMetadata', array('idsite', 'group', array('\\Piwik\\Site', 'getGroupFor'), array()));
     $dataTable->queueFilter('MetadataCallbackAddMetadata', array('idsite', 'main_url', array('\\Piwik\\Site', 'getMainUrlFor'), array()));
     // set the label of each row to the site name
     if ($multipleWebsitesRequested) {
         $dataTable->queueFilter('ColumnCallbackReplace', array('label', '\\Piwik\\Site::getNameFor'));
     } else {
         $dataTable->queueFilter('ColumnDelete', array('label'));
     }
     // replace record names with user friendly metric names
     $dataTable->queueFilter('ReplaceColumnNames', array($columnNameRewrites));
     // filter rows without visits
     // note: if only one website is queried and there are no visits, we can not remove the row otherwise
     // ResponseBuilder throws 'Call to a member function getColumns() on a non-object'
     if ($multipleWebsitesRequested && !$enhanced) {
         $dataTable->filter('ColumnCallbackDeleteRow', array(self::NB_VISITS_METRIC, function ($value) {
             return $value == 0;
         }));
     }
     if ($multipleWebsitesRequested && $dataTable->getRowsCount() === 1 && $dataTable instanceof DataTable\Simple) {
         $simpleTable = $dataTable;
         $dataTable = $simpleTable->getEmptyClone();
         $dataTable->addRow($simpleTable->getFirstRow());
         unset($simpleTable);
     }
     return $dataTable;
 }
예제 #20
0
 /**
  * Helper function that creates an Archive instance and queries for report data using
  * query parameter data. API methods can use this method to reduce code redundancy.
  * 
  * @param string $name The name of the report to return.
  * @param int|string|array $idSite @see {@link build()}
  * @param string $period @see {@link build()}
  * @param string $date @see {@link build()}
  * @param string $segment @see {@link build()}
  * @param bool $expanded If true, loads all subtables. See {@link getDataTableExpanded()}
  * @param int|null $idSubtable See {@link getDataTableExpanded()}
  * @param bool $skipAggregationOfSubTables Whether or not we should skip the aggregation of all sub-tables and only aggregate parent DataTable.
  * @param int|null $depth See {@link getDataTableExpanded()}
  * @return DataTable|DataTable\Map See {@link getDataTable()} and
  *                                 {@link getDataTableExpanded()} for more
  *                                 information
  */
 public static function getDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $idSubtable = null, $skipAggregationOfSubTables = false, $depth = null)
 {
     Piwik::checkUserHasViewAccess($idSite);
     if ($skipAggregationOfSubTables && ($expanded || $idSubtable)) {
         throw new \Exception("Not expected to skipAggregationOfSubTables when expanded=1 or idSubtable is set.");
     }
     $archive = Archive::build($idSite, $period, $date, $segment, $_restrictSitesToLogin = false, $skipAggregationOfSubTables);
     if ($idSubtable === false) {
         $idSubtable = null;
     }
     if ($expanded) {
         $dataTable = $archive->getDataTableExpanded($name, $idSubtable, $depth);
     } else {
         $dataTable = $archive->getDataTable($name, $idSubtable);
     }
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
     return $dataTable;
 }
예제 #21
0
 private function buildDataTable($idSitesOrIdSite, $period, $date, $segment, $_restrictSitesToLogin, $enhanced, $multipleWebsitesRequested)
 {
     $allWebsitesRequested = $idSitesOrIdSite == 'all';
     if ($allWebsitesRequested) {
         // First clear cache
         Site::clearCache();
         // Then, warm the cache with only the data we should have access to
         if (Piwik::isUserIsSuperUser() && !TaskScheduler::isTaskBeingExecuted()) {
             $sites = APISitesManager::getInstance()->getAllSites();
         } else {
             $sites = APISitesManager::getInstance()->getSitesWithAtLeastViewAccess($limit = false, $_restrictSitesToLogin);
         }
         // Both calls above have called Site::setSitesFromArray. We now get these sites:
         $sitesToProblablyAdd = Site::getSites();
     } else {
         $sitesToProblablyAdd = array(APISitesManager::getInstance()->getSiteFromId($idSitesOrIdSite));
     }
     // build the archive type used to query archive data
     $archive = Archive::build($idSitesOrIdSite, $period, $date, $segment, $_restrictSitesToLogin);
     // determine what data will be displayed
     $fieldsToGet = array();
     $columnNameRewrites = array();
     $apiECommerceMetrics = array();
     $apiMetrics = API::getApiMetrics($enhanced);
     foreach ($apiMetrics as $metricName => $metricSettings) {
         $fieldsToGet[] = $metricSettings[self::METRIC_RECORD_NAME_KEY];
         $columnNameRewrites[$metricSettings[self::METRIC_RECORD_NAME_KEY]] = $metricName;
         if ($metricSettings[self::METRIC_IS_ECOMMERCE_KEY]) {
             $apiECommerceMetrics[$metricName] = $metricSettings;
         }
     }
     // get the data
     // $dataTable instanceOf Set
     $dataTable = $archive->getDataTableFromNumeric($fieldsToGet);
     $dataTable = $this->mergeDataTableMapAndPopulateLabel($idSitesOrIdSite, $multipleWebsitesRequested, $dataTable);
     if ($dataTable instanceof DataTable\Map) {
         foreach ($dataTable->getDataTables() as $table) {
             $this->addMissingWebsites($table, $fieldsToGet, $sitesToProblablyAdd);
         }
     } else {
         $this->addMissingWebsites($dataTable, $fieldsToGet, $sitesToProblablyAdd);
     }
     // calculate total visits/actions/revenue
     $this->setMetricsTotalsMetadata($dataTable, $apiMetrics);
     // if the period isn't a range & a lastN/previousN date isn't used, we get the same
     // data for the last period to show the evolution of visits/actions/revenue
     list($strLastDate, $lastPeriod) = Range::getLastDate($date, $period);
     if ($strLastDate !== false) {
         if ($lastPeriod !== false) {
             // NOTE: no easy way to set last period date metadata when range of dates is requested.
             //       will be easier if DataTable\Map::metadata is removed, and metadata that is
             //       put there is put directly in DataTable::metadata.
             $dataTable->setMetadata(self::getLastPeriodMetadataName('date'), $lastPeriod);
         }
         $pastArchive = Archive::build($idSitesOrIdSite, $period, $strLastDate, $segment, $_restrictSitesToLogin);
         $pastData = $pastArchive->getDataTableFromNumeric($fieldsToGet);
         $pastData = $this->mergeDataTableMapAndPopulateLabel($idSitesOrIdSite, $multipleWebsitesRequested, $pastData);
         // use past data to calculate evolution percentages
         $this->calculateEvolutionPercentages($dataTable, $pastData, $apiMetrics);
         $this->setPastDataMetadata($dataTable, $pastData, $apiMetrics);
     }
     // remove eCommerce related metrics on non eCommerce Piwik sites
     // note: this is not optimal in terms of performance: those metrics should not be retrieved in the first place
     if ($enhanced) {
         if ($dataTable instanceof DataTable\Map) {
             foreach ($dataTable->getDataTables() as $table) {
                 $this->removeEcommerceRelatedMetricsOnNonEcommercePiwikSites($table, $apiECommerceMetrics);
             }
         } else {
             $this->removeEcommerceRelatedMetricsOnNonEcommercePiwikSites($dataTable, $apiECommerceMetrics);
         }
     }
     // move the site id to a metadata column
     $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'idsite'));
     // set the label of each row to the site name
     if ($multipleWebsitesRequested) {
         $dataTable->filter('ColumnCallbackReplace', array('label', '\\Piwik\\Site::getNameFor'));
     } else {
         $dataTable->filter('ColumnDelete', array('label'));
     }
     // replace record names with user friendly metric names
     $dataTable->filter('ReplaceColumnNames', array($columnNameRewrites));
     // Ensures data set sorted, for Metadata output
     $dataTable->filter('Sort', array(self::NB_VISITS_METRIC, 'desc', $naturalSort = false));
     // filter rows without visits
     // note: if only one website is queried and there are no visits, we can not remove the row otherwise
     // ResponseBuilder throws 'Call to a member function getColumns() on a non-object'
     if ($multipleWebsitesRequested && !$enhanced) {
         $dataTable->filter('ColumnCallbackDeleteRow', array(self::NB_VISITS_METRIC, function ($value) {
             return $value == 0;
         }));
     }
     return $dataTable;
 }
예제 #22
0
 protected static function _addReportData()
 {
     $date = Date::factory(self::$dateTime);
     $archive = Archive::build(self::$idSite, 'year', $date);
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'year', $date);
     //        APIVisitorInterest::getInstance()->get(self::$idSite, 'month', $date, $segment = false, self::$idSite);
     // months are added via the 'year' period, but weeks must be done manually
     for ($daysAgo = self::$daysAgoStart; $daysAgo > 0; $daysAgo -= 7) {
         $dateTime = $date->subDay($daysAgo);
         $archive = Archive::build(self::$idSite, 'week', $dateTime);
         $archive->getNumeric('nb_visits');
         APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'week', $dateTime);
     }
     // add segment for one day
     $archive = Archive::build(self::$idSite, 'day', '2012-01-14', 'browserCode==FF');
     $archive->getNumeric('nb_visits', 'nb_hits');
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'day', '2012-01-14', 'browserCode==FF');
     // add range within January
     $rangeEnd = Date::factory('2012-01-29');
     $rangeStart = $rangeEnd->subDay(1);
     $range = $rangeStart->toString('Y-m-d') . "," . $rangeEnd->toString('Y-m-d');
     $rangeArchive = Archive::build(self::$idSite, 'range', $range);
     $rangeArchive->getNumeric('nb_visits', 'nb_hits');
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'range', $range);
     // add range between January & February
     $rangeStart = $rangeEnd;
     $rangeEnd = $rangeStart->addDay(3);
     $range = $rangeStart->toString('Y-m-d') . "," . $rangeEnd->toString('Y-m-d');
     $rangeArchive = Archive::build(self::$idSite, 'range', $range);
     $rangeArchive->getNumeric('nb_visits', 'nb_hits');
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'range', $range);
     // when archiving is initiated, the archive metrics & reports for EVERY loaded plugin
     // are archived. don't want this test to depend on every possible metric, so get rid of
     // the unwanted archive data now.
     $metricsToSave = array('nb_visits', 'nb_actions', Archiver::getRecordName('revenue'), Archiver::getRecordName('nb_conversions', 1), Archiver::getRecordName('revenue', GoalManager::IDGOAL_ORDER));
     $archiveTables = self::_getArchiveTableNames();
     foreach ($archiveTables['numeric'] as $table) {
         $realTable = Common::prefixTable($table);
         $sql = "DELETE FROM {$realTable} WHERE name NOT IN ('" . implode("','", $metricsToSave) . "') AND name NOT LIKE 'done%'";
         Db::query($sql);
     }
     foreach ($archiveTables['blob'] as $table) {
         $realTable = Common::prefixTable($table);
         Db::query("DELETE FROM {$realTable} WHERE name NOT IN ('VisitorInterest_timeGap')");
     }
     // add garbage metrics
     $janDate1 = '2012-01-05';
     $febDate1 = '2012-02-04';
     $sql = "INSERT INTO %s (idarchive,name,idsite,date1,date2,period,ts_archived,value)\n                        VALUES (10000,?,1,?,?,?,?,?)";
     // one metric for jan & one for feb
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['numeric'][0])), array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 1, 100));
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['numeric'][1])), array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 1, 200));
     // add garbage reports
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['blob'][0])), array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 10, 'blobval'));
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['blob'][1])), array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 20, 'blobval'));
 }
예제 #23
0
파일: API.php 프로젝트: piwik/piwik
 public function getNumberOfDistinctCountries($idSite, $period, $date, $segment = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     return $archive->getDataTableFromNumeric(Archiver::DISTINCT_COUNTRIES_METRIC);
 }
예제 #24
0
 /**
  * Attach the country information on demand.
  *
  * @param $content
  * @param $idSite
  * @param $period
  * @param $date
  * @param $segment
  * @return \Piwik\DataTable|\Piwik\DataTable\Map
  */
 private function attachStatsbyCountry($content, $idSite, $period, $date, $segment)
 {
     $data = new DataTable();
     if ($iso2 = $this->getIso2Country($content->data[0])) {
         $segmentByCountry = $segment . ';countryCode==' . $iso2;
         $archiveByCountry = Archive::build($idSite, $period, $date, $segmentByCountry);
         $data = $archiveByCountry->getDataTableFromNumeric(array('nb_visits', 'Actions_nb_downloads'));
         $data->getRowFromId(0)->addColumn('label', $content->data[0]->label . ' - in country');
         $downloads = $this->attachDownloadByType($idSite, $period, $date, $segmentByCountry);
         $data->getRowFromId(0)->addColumns($downloads);
     }
     return $data;
 }