File size: 9,712 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\System;

use Piwik\Archive\ArchivePurger;
use Piwik\Archive\Chunk;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
use Piwik\Tests\Fixtures\VisitsOverSeveralDays;

/**
 * Tests some API using range periods & makes sure the correct amount of blob/numeric
 * archives are created.
 *
 * @group OneVisitorOneWebsiteSeveralDaysDateRangeArchivingTest
 * @group Core
 */
class OneVisitorOneWebsiteSeveralDaysDateRangeArchivingTest extends SystemTestCase
{
    /**
     * @var VisitsOverSeveralDays
     */
    public static $fixture = null; // initialized below test definition

    public static function getOutputPrefix()
    {
        return 'oneVisitor_oneWebsite_severalDays_DateRange';
    }

    /**
     * @dataProvider getApiForTesting
     */
    public function testApi($api, $params)
    {
        $this->runApiTests($api, $params);
    }

    public function getApiForTesting()
    {
        $idSite = self::$fixture->idSite;

        $apiToCall = array('Actions.getPageUrls',
                           'VisitsSummary.get',
                           'Resolution.getResolution',
                           'VisitFrequency.get',
                           'VisitTime.getVisitInformationPerServerTime');

        // 2 segments: ALL and another way of expressing ALL but triggering the Segment code path
        // 2 segments: ALL and another way of expressing ALL but triggering the Segment code path
        $segments = array(
            false,
            'countryCode!=aa',
            'pageUrl!=ThisIsNotKnownPageUrl',
        );

        // Running twice just as health check that second call also works
        $result = array();
        for ($i = 0; $i <= 1; $i++) {
            foreach ($segments as $segment) {
                $result[] = array($apiToCall, array('idSite'  => $idSite, 'date' => '2010-12-15,2011-01-15',
                                                    'periods' => array('range'),
                                                    'segment' => $segment,
                                                    'otherRequestParameters' => array(
                                                        'flat'                   => '0',
                                                        'expanded'               => '0',
                                                    ),
                ));
            }
        }

        // Testing Date range in January only
        // Because of flat=1, this test will archive all sub-tables
        $result[] = array('Actions.getPageUrls', array('idSite'  => $idSite, 'date' => '2011-01-01,2011-02-01',
                                                       'periods' => array('range'),
                                                       'otherRequestParameters' => array(
                                                           'flat'                   => '1',
                                                           'expanded'               => '0',
                                                       ),
                                                       'testSuffix' => '_periodIsRange_flattened_'),
        );
        // testing the same with expanded=1 should not create new archive records
        $result[] = array('Actions.getPageUrls', array('idSite'  => $idSite, 'date' => '2011-01-01,2011-02-01',
                                                       'periods' => array('range'),
                                                       'otherRequestParameters' => array(
                                                           'flat'                   => '0',
                                                           'expanded'               => '1',
                                                       ),
                                                       'testSuffix' => '_periodIsRange_expanded_'),
        );
        return $result;
    }

    /**
     *  Check that requesting period "Range" means only processing
     *  the requested Plugin blob (Actions in this case), not all Plugins blobs
     *
     * @depends      testApi
     */
    public function testCheckArchiveRecordsWhenPeriodIsRange()
    {
        $archivePurger = StaticContainer::get(ArchivePurger::class);
        foreach (self::$fixture->dateTimes as $date) {
            $archivePurger->purgeInvalidatedArchivesFrom(Date::factory($date));
        }

        // we expect 1 blobs for Actions plugins, because we query for only one Actions report and flat=1 or expanded=1 was not set
        // so we only archived the parent table
        $expectedActionsBlobs = 1;

        // When flat=1, Actions plugin will process 5 + 1 extra chunk blobs (URL = 'http://example.org/sub1/sub2/sub3/news')
        $expectedActionsBlobsWhenFlattened = $expectedActionsBlobs + 1;

        $tests = array(
            'archive_blob_2010_12'    => ( ($expectedActionsBlobs + 1) /*Actions*/
                    + 1 /* Resolution */
                    + 1 /* VisitTime */) * 3,

            /**
             * segments: 9 (including all visits)
             * plugins: 4 different plugins
             *   VisitsSummary: 9 archives (8 segments + all visits) (8 metrics in each)
             *   Actions: 3 archives (2 segments + all visits) (0 metrics in each, since no metrics are requested for period=range)
             *   Resolution: 3 archives (2 segments + all visits) (0 metrics in each)
             *   VisitTime: 3 archives (2 segments + all visits) (0 metrics in each)
             *
             * Total: 9 VisitsSummary done flags + (9 * 8) VisitsSummary metrics
             *   + 3 Actions done flags
             *   + 3 Resolution done flags
             *   + 3 VisitTime done flags
             * = 90
             */
            'archive_numeric_2010_12' => 90,

            /**
             * In the January date range,
             * we archive only Actions plugins.
             * It is flattened so all 3 sub-tables should be archived.
             */
            'archive_blob_2011_01'    => $expectedActionsBlobsWhenFlattened,

            /**
             *   8 metrics + 1 flag // VisitsSummary
             * + 1 flag // Actions (no metrics, just blobs)
             */
            'archive_numeric_2011_01' => (9 + 1),

            // nothing in Feb
            'archive_blob_2011_02'    => 0,
            'archive_numeric_2011_02' => 0,
        );
        foreach ($tests as $table => $expectedRows) {
            if ($expectedRows === 0 && !$this->tableExists($table)) {
                continue;
            }

            $sql = "SELECT count(*) FROM " . Common::prefixTable($table) . " WHERE period = " . Piwik::$idPeriods['range'];
            $countBlobs = Db::get()->fetchOne($sql);

            if ($expectedRows != $countBlobs) {
                $this->printDebugWhenTestFails($table);
            }
            $this->assertEquals($expectedRows, $countBlobs, "$table expected $expectedRows, got $countBlobs");
        }
    }

    /**
     *  Check that requesting period "Range" means only processing
     *  the requested Plugin blob (Actions in this case), not all Plugins blobs
     *
     * @depends      testApi
     */
    public function testCheckArchiveRecordsShouldMergeSubtablesIntoOneRow()
    {
        $tests = array(
            'archive_blob_2010_12' => 3,

            /**
             * In the January date range,
             * we archive only Actions plugins.
             * It is flattened so all 3 sub-tables should be archived.
             */
            'archive_blob_2011_01'    => 3,
        );
        $chunk = new Chunk();
        $chunkName = $chunk->getRecordNameForTableId('Actions_actions_url', 0);

        foreach ($tests as $table => $expectedNumSubtables) {
            $sql = "SELECT value FROM " . Common::prefixTable($table) . " WHERE period = " . Piwik::$idPeriods['range'] . " and `name` ='$chunkName'";
            $blob = Db::get()->fetchOne($sql);
            $blob = gzuncompress($blob);
            $blob = unserialize($blob);
            $countSubtables = count($blob);

            $this->assertEquals($expectedNumSubtables, $countSubtables, "Actions_actions_url_chunk_0_99 in $table expected to contain $expectedNumSubtables subtables, got $countSubtables");
        }
    }

    /**
     * @param $table
     */
    protected function printDebugWhenTestFails($table)
    {
        $data = Db::get()->fetchAll("SELECT * FROM " . Common::prefixTable($table) . " WHERE period = " . Piwik::$idPeriods['range'] . " ORDER BY idarchive ASC");
        if (strpos($table, 'blob') !== false) {
            $data = array_map(function ($r) {
                unset($r['value']);
                return $r;
            }, $data);
        }
        var_export($data);

        $idArchives = array();
        foreach ($data as $row) {
            $idArchives[] = $row['idarchive'];
        }
        $idArchives = array_unique($idArchives);
        foreach ($idArchives as $idArchive) {
            $numericTable = str_replace("blob", "numeric", Common::prefixTable($table));
            var_export(Db::get()->fetchAll("SELECT idarchive, name FROM " . $numericTable . " WHERE idarchive = ? AND name LIKE 'done%' LIMIT 1 ", $idArchive));
        }
    }

    private function tableExists($table)
    {
        return (bool) Db::fetchOne("SHOW TABLES LIKE '$table'");
    }
}

OneVisitorOneWebsiteSeveralDaysDateRangeArchivingTest::$fixture = new VisitsOverSeveralDays();