Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IndexedByDate.php « Array « Archive « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91bbab2a78c2f6f1145f7f202c2330ba19acd976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * 
 * @category Piwik
 * @package Piwik
 */

/**
 * @package Piwik
 * @subpackage Piwik_Archive
 */
class Piwik_Archive_Array_IndexedByDate extends Piwik_Archive_Array 
{
	/**
	 * Builds an array of Piwik_Archive of a given date range
	 *
	 * @param Piwik_Site     $oSite
	 * @param string         $strPeriod eg. 'day' 'week' etc.
	 * @param string         $strDate A date range, eg. 'last10', 'previous5' or 'YYYY-MM-DD,YYYY-MM-DD'
	 * @param Piwik_Segment  $segment
	 */
	public function __construct(Piwik_Site $oSite, $strPeriod, $strDate, Piwik_Segment $segment)
	{
		$rangePeriod = new Piwik_Period_Range($strPeriod, $strDate, $oSite->getTimezone());
		foreach($rangePeriod->getSubperiods() as $subPeriod)
		{
			$startDate = $subPeriod->getDateStart();
			$archive = Piwik_Archive::build($oSite->getId(), $strPeriod, $startDate, $segment->getString() );
			$archive->setSegment($segment);
			$this->archives[] = $archive;
		}
		$this->setSite($oSite);
	}

	/**
	 * @return string
	 */
	protected function getIndexName()
	{
		return 'date';
	}

	/**
	 * @param Piwik_Archive  $archive
	 * @return mixed
	 */
	protected function getDataTableLabelValue( $archive )
	{
		return $archive->getPrettyDate();
	}
	
	/**
	 * Given a list of fields defining numeric values, it will return a Piwik_DataTable_Array
	 * which is an array of Piwik_DataTable_Simple, ordered by chronological order
	 *
	 * @param array|string  $fields  array( fieldName1, fieldName2, ...)  Names of the mysql table fields to load
	 * @return Piwik_DataTable_Array
	 */
	public function getDataTableFromNumeric( $fields )
	{
		$inNames = Piwik_Common::getSqlStringFieldsArray($fields);
		
		// we select in different shots
		// one per distinct table (case we select last 300 days, maybe we will  select from 10 different tables)
		$queries = array();
		foreach($this->archives as $archive) 
		{
			$archive->setRequestedReport( is_string($fields) ? $fields : current($fields) );
			$archive->prepareArchive();
			if(!$archive->isThereSomeVisits)
			{
				continue;
			}
			
			$table = $archive->archiveProcessing->getTableArchiveNumericName();

			// for every query store IDs
			$queries[$table][] = $archive->getIdArchive();
		}
		// we select the requested value
		$db = Zend_Registry::get('db');
		
		// date => array( 'field1' =>X, 'field2'=>Y)
		// date2 => array( 'field1' =>X2, 'field2'=>Y2)		
		
		$arrayValues = array();
		foreach($queries as $table => $aIds)
		{
			$inIds = implode(', ', array_filter($aIds));
			if(empty($inIds))
			{
				// Probable timezone configuration error, i.e., mismatch between PHP and MySQL server.
				continue;
			}

			$sql = "SELECT value, name, date1 as startDate
					FROM $table
					WHERE idarchive IN ( $inIds )
					AND name IN ( $inNames )
					ORDER BY date1, name";
			$values = $db->fetchAll($sql, $fields);
			foreach($values as $value)
			{
				$timestamp = Piwik_Date::factory($value['startDate'])->getTimestamp();
				$arrayValues[$timestamp][$value['name']] = $this->formatNumericValue($value['value']);
			}			
		}
		
		$contentArray = array();
		// we add empty tables so that every requested date has an entry, even if there is nothing
		// example: <result date="2007-01-01" />
		$archiveByTimestamp = array();
		foreach($this->archives as $archive)
		{
			$timestamp = $archive->getTimestampStartDate();
			$archiveByTimestamp[$timestamp] = $archive;
			$contentArray[$timestamp]['table'] = $archive->makeDataTable($simple = true);
			$contentArray[$timestamp]['prettyDate'] = $archive->getPrettyDate();
		}

		foreach($arrayValues as $timestamp => $aNameValues)
		{
			// undefined in some edge/unknown cases see http://dev.piwik.org/trac/ticket/2578
			if(isset($contentArray[$timestamp]['table']))
			{
				$contentArray[$timestamp]['table']->addRowsFromArray($aNameValues);
			}
		}

		$tableArray = $this->getNewDataTableArray();
		foreach($contentArray as $timestamp => $aData)
		{
			$tableArray->addTable($aData['table'], $aData['prettyDate']);
		}
		return $tableArray;
	}
}