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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;
use Piwik\Container\StaticContainer;
use Piwik\CronArchive;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\Date;
use Piwik\Db;
use Piwik\Plugins\CoreAdminHome\tests\Framework\Mock\API;
use Piwik\Plugins\SegmentEditor\Model;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeLogger;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\SegmentEditor\API as SegmentAPI;
use Piwik\Version;
/**
* @group Archiver
* @group CronArchive
*/
class CronArchiveTest extends IntegrationTestCase
{
public function test_getColumnNamesFromTable()
{
Fixture::createWebsite('2014-12-12 00:01:02');
Fixture::createWebsite('2014-12-12 00:01:02');
$ar = StaticContainer::get('Piwik\Archive\ArchiveInvalidator');
$ar->rememberToInvalidateArchivedReportsLater(1, Date::factory('2014-04-05'));
$ar->rememberToInvalidateArchivedReportsLater(2, Date::factory('2014-04-05'));
$ar->rememberToInvalidateArchivedReportsLater(2, Date::factory('2014-04-06'));
$api = API::getInstance();
$cronarchive = new TestCronArchive(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php');
$cronarchive->setApiToInvalidateArchivedReport($api);
$cronarchive->invalidateArchivedReportsForSitesThatNeedToBeArchivedAgain();
/**
* should look like this but the result is random
* array(
array(array(1,2), '2014-04-05'),
array(array(2), '2014-04-06')
)
*/
$invalidatedReports = $api->getInvalidatedReports();
$this->assertCount(2, $invalidatedReports);
sort($invalidatedReports[0][0]);
sort($invalidatedReports[1][0]);
usort($invalidatedReports, function ($a, $b) {
return strcmp($a[1], $b[1]);
});
$this->assertSame(array(1,2), $invalidatedReports[0][0]);
$this->assertSame('2014-04-05', $invalidatedReports[0][1]);
$this->assertSame(array(2), $invalidatedReports[1][0]);
$this->assertSame('2014-04-06', $invalidatedReports[1][1]);
}
public function test_wasSegmentCreatedRecently()
{
Fixture::createWebsite('2014-12-12 00:01:02');
SegmentAPI::getInstance()->add('foo', 'actions>=1', 1, true, true);
$id = SegmentAPI::getInstance()->add('barb', 'actions>=2', 1, true, true);
$segments = new Model();
$segments->updateSegment($id, array('ts_created' => Date::now()->subHour(30)->getDatetime()));
$allSegments = $segments->getSegmentsToAutoArchive(1);
$cronarchive = new TestCronArchive(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php');
$this->assertTrue($cronarchive->wasSegmentChangedRecently('actions>=1', $allSegments));
// created 30 hours ago...
$this->assertFalse($cronarchive->wasSegmentChangedRecently('actions>=2', $allSegments));
// not configured segment
$this->assertFalse($cronarchive->wasSegmentChangedRecently('actions>=999', $allSegments));
}
public function test_skipSegmentsToday()
{
\Piwik\Tests\Framework\Mock\FakeCliMulti::$specifiedResults = array(
'/method=API.get/' => json_encode(array(array('nb_visits' => 1)))
);
Fixture::createWebsite('2014-12-12 00:01:02');
SegmentAPI::getInstance()->add('foo', 'actions>=1', 1, true, true);
$id = SegmentAPI::getInstance()->add('barb', 'actions>=2', 1, true, true);
$segments = new Model();
$segments->updateSegment($id, array('ts_created' => Date::now()->subHour(30)->getDatetime()));
$logger = new FakeLogger();
$archiver = new CronArchive(null, $logger);
$archiver->skipSegmentsToday = true;
$archiver->shouldArchiveAllSites = true;
$archiver->shouldArchiveAllPeriodsSince = true;
$archiver->init();
$archiver->run();
self::assertStringContainsString('Will skip segments archiving for today unless they were created recently', $logger->output);
self::assertStringContainsString('Segment "actions>=1" was created or changed recently and will therefore archive today', $logger->output);
self::assertStringNotContainsString('Segment "actions>=2" was created recently', $logger->output);
}
public function test_output()
{
\Piwik\Tests\Framework\Mock\FakeCliMulti::$specifiedResults = array(
'/method=API.get/' => json_encode(array(array('nb_visits' => 1)))
);
Fixture::createWebsite('2014-12-12 00:01:02');
SegmentAPI::getInstance()->add('foo', 'actions>=2', 1, true, true);
SegmentAPI::getInstance()->add('burr', 'actions>=4', 1, true, true);
$tracker = Fixture::getTracker(1, '2019-12-12 02:03:00');
$tracker->setUrl('http://someurl.com');
Fixture::checkResponse($tracker->doTrackPageView('abcdefg'));
$tracker->setForceVisitDateTime('2019-12-11 03:04:05');
$tracker->setUrl('http://someurl.com/2');
Fixture::checkResponse($tracker->doTrackPageView('abcdefg2'));
$tracker->setForceVisitDateTime('2019-12-10 03:04:05');
$tracker->setUrl('http://someurl.com/3');
Fixture::checkResponse($tracker->doTrackPageView('abcdefg3'));
$tracker->setForceVisitDateTime('2019-12-02 03:04:05');
$tracker->setUrl('http://someurl.com/4');
Fixture::checkResponse($tracker->doTrackPageView('abcdefg4'));
$logger = new FakeLogger();
$archiver = new CronArchive(null, $logger);
$archiveFilter = new CronArchive\ArchiveFilter();
$archiveFilter->setSegmentsToForce(['actions>=2;browserCode=FF', 'actions>=2']);
$archiver->setArchiveFilter($archiveFilter);
$archiver->init();
$archiver->run();
$version = Version::VERSION;
$expected = <<<LOG
---------------------------
INIT
Running Matomo $version as Super User
---------------------------
NOTES
- If you execute this script at least once per hour (or more often) in a crontab, you may disable 'Browser trigger archiving' in Matomo UI > Settings > General Settings.
See the doc at: https://matomo.org/docs/setup-auto-archiving/
- Async process archiving supported, using CliMulti.
- Reports for today will be processed at most every 900 seconds. You can change this value in Matomo UI > Settings > General Settings.
- Limiting segment archiving to following segments:
* actions>=2;browserCode=FF
* actions>=2
---------------------------
START
Starting Matomo reports archiving...
Checking for queued invalidations...
Will invalidate archived reports for 2019-12-12 for following websites ids: 1
Will invalidate archived reports for 2019-12-11 for following websites ids: 1
Will invalidate archived reports for 2019-12-10 for following websites ids: 1
Will invalidate archived reports for 2019-12-02 for following websites ids: 1
Today archive can be skipped due to no visits, skipping invalidation...
Yesterday archive can be skipped due to no visits, skipping invalidation...
Segment "actions>=2" was created or changed recently and will therefore archive today (for site ID = 1)
Segment "actions>=4" was created or changed recently and will therefore archive today (for site ID = 1)
Done invalidating
Start processing archives for site 1.
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2020-01-01 - 2020-01-01, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2020-01-01 - 2020-01-31, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2020-01-01 - 2020-12-31, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-31 - 2019-12-31, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-30 - 2019-12-30, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-30 - 2020-01-05, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-23 - 2019-12-23, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-23 - 2019-12-29, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-16 - 2019-12-16, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-16 - 2019-12-22, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Found invalidated archive we can skip (no visits or latest archive is not invalidated). [idSite = 1, dates = 2019-12-09 - 2019-12-09, segment = actions>=2]
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found archive with different period than others in concurrent batch, skipping until next batch: 1
Found archive with different period than others in concurrent batch, skipping until next batch: 1
Found archive with different period than others in concurrent batch, skipping until next batch: 1
Found archive with different done flag type (segment vs. no segment) in concurrent batch, skipping until next batch: done
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
No next invalidated archive.
Starting archiving for ?module=API&method=API.get&idSite=1&period=week&date=2019-12-09&format=json&segment=actions%3E%3D2&trigger=archivephp
Starting archiving for ?module=API&method=API.get&idSite=1&period=week&date=2019-12-02&format=json&segment=actions%3E%3D2&trigger=archivephp
Archived website id 1, period = week, date = 2019-12-09, segment = 'actions%3E%3D2', 0 visits found. Time elapsed: %fs
Archived website id 1, period = week, date = 2019-12-02, segment = 'actions%3E%3D2', 0 visits found. Time elapsed: %fs
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found archive with different period than others in concurrent batch, skipping until next batch: 2
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 3
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
No next invalidated archive.
Starting archiving for ?module=API&method=API.get&idSite=1&period=day&date=2019-12-02&format=json&segment=actions%3E%3D2&trigger=archivephp
Archived website id 1, period = day, date = 2019-12-02, segment = 'actions%3E%3D2', 0 visits found. Time elapsed: %fs
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
Found archive with different period than others in concurrent batch, skipping until next batch: 4
No next invalidated archive.
Starting archiving for ?module=API&method=API.get&idSite=1&period=month&date=2019-12-01&format=json&segment=actions%3E%3D2&trigger=archivephp
Archived website id 1, period = month, date = 2019-12-01, segment = 'actions%3E%3D2', 0 visits found. Time elapsed: %fs
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment '' is not in --force-idsegments
Skipping invalidated archive : segment 'actions>=4' is not in --force-idsegments
No next invalidated archive.
Starting archiving for ?module=API&method=API.get&idSite=1&period=year&date=2019-01-01&format=json&segment=actions%3E%3D2&trigger=archivephp
Archived website id 1, period = year, date = 2019-01-01, segment = 'actions%3E%3D2', 0 visits found. Time elapsed: %fs
No next invalidated archive.
Finished archiving for site 1, 5 API requests, Time elapsed: %fs [1 / 1 done]
No more sites left to archive, stopping.
LOG;
$this->assertStringMatchesFormat($expected, $logger->output);
}
public function test_shouldNotStopProcessingWhenOneSiteIsInvalid()
{
\Piwik\Tests\Framework\Mock\FakeCliMulti::$specifiedResults = array(
'/method=API.get/' => json_encode(array(array('nb_visits' => 1)))
);
Fixture::createWebsite('2014-12-12 00:01:02');
$logger = new FakeLogger();
$archiver = new CronArchive(null, $logger);
$archiver->shouldArchiveSpecifiedSites = array(99999, 1);
$archiver->init();
$archiver->run();
$expected = <<<LOG
- Will process 2 websites (--force-idsites)
- Will process specified sites: 1
---------------------------
START
Starting Matomo reports archiving...
Checking for queued invalidations...
Today archive can be skipped due to no visits, skipping invalidation...
Yesterday archive can be skipped due to no visits, skipping invalidation...
Done invalidating
Start processing archives for site 1.
No next invalidated archive.
LOG;
self::assertStringContainsString($expected, $logger->output);
}
public function provideContainerConfig()
{
Date::$now = strtotime('2020-02-03 04:05:06');
return array(
'Piwik\CliMulti' => \DI\object('Piwik\Tests\Framework\Mock\FakeCliMulti')
);
}
protected static function configureFixture($fixture)
{
parent::configureFixture($fixture);
$fixture->createSuperUser = true;
}
}
class TestCronArchive extends CronArchive
{
protected function checkPiwikUrlIsValid()
{
}
protected function initPiwikHost($piwikUrl = false)
{
}
public function wasSegmentChangedRecently($definition, $allSegments)
{
return parent::wasSegmentChangedRecently($definition, $allSegments);
}
}
|