Skip to content

Commit 22c8602

Browse files
committed
SegmentEditor.getSegmentData API moved to the API file and renamed parameter
1 parent c09bc02 commit 22c8602

6 files changed

Lines changed: 110 additions & 135 deletions

File tree

plugins/SegmentEditor/API.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
use Piwik\Common;
1616
use Piwik\Container\StaticContainer;
1717
use Piwik\CronArchive\SegmentArchiving;
18+
use Piwik\DataTable\Filter\CalculateEvolutionFilter;
1819
use Piwik\Date;
20+
use Piwik\Period\Range;
1921
use Piwik\Piwik;
2022
use Piwik\Config;
2123
use Piwik\Segment;
24+
use Piwik\Plugins\VisitsSummary;
2225
use Piwik\Cache;
2326
use Piwik\Url;
2427

@@ -564,4 +567,88 @@ private function getMessageCannotEditSegmentCreatedBySuperUser(): string
564567
{
565568
return Piwik::translate('SegmentEditor_UpdatingForeignSegmentPermittedToSuperUser');
566569
}
570+
571+
/**
572+
* @return array{
573+
* nb_visits:int,
574+
* nb_actions:int,
575+
* evolution_visits_direction:string,
576+
* evolution_visits_icon:string,
577+
* evolution_visits:string
578+
* }
579+
*/
580+
public function getSegmentData(int $idSite, string $period, string $date, string $segment): array
581+
{
582+
$segmentDefinition = $segment ?: '';
583+
$this->checkSegmentIsPreProcessed($segmentDefinition);
584+
$data = VisitsSummary\API::getInstance()
585+
->get($idSite, $period, $date, $segmentDefinition)
586+
->getFirstRow()->getArrayCopy();
587+
[$previousDate] = Range::getLastDate($date, $period);
588+
$pastNbVisits = VisitsSummary\API::getInstance()
589+
->getVisits($idSite, $period, $previousDate, $segmentDefinition)
590+
->getFirstRow()->getColumn('nb_visits');
591+
592+
$nbVisits = (int)($data['nb_visits'] ?? 0);
593+
$nbActions = (int)($data['nb_actions'] ?? 0);
594+
$pastNbVisits = (int)$pastNbVisits;
595+
$evolutionDirection = $this->getEvolutionDirection($nbVisits, $pastNbVisits);
596+
597+
return [
598+
'nb_visits' => $nbVisits,
599+
'nb_actions' => $nbActions,
600+
'evolution_visits_direction' => $evolutionDirection,
601+
'evolution_visits_icon' => $this->getEvolutionIcon($evolutionDirection),
602+
'evolution_visits' => CalculateEvolutionFilter::calculate($nbVisits, $pastNbVisits, 0, true, false),
603+
];
604+
}
605+
606+
private function getEvolutionDirection(int $currentValue, int $pastValue): string
607+
{
608+
if ($currentValue > $pastValue) {
609+
return 'positive';
610+
}
611+
612+
if ($currentValue < $pastValue) {
613+
return 'negative';
614+
}
615+
616+
return 'stable';
617+
}
618+
619+
private function getEvolutionIcon(string $direction): string
620+
{
621+
if ($direction === 'positive') {
622+
return 'plugins/MultiSites/images/evolution_up.svg';
623+
}
624+
625+
if ($direction === 'negative') {
626+
return 'plugins/MultiSites/images/evolution_down.svg';
627+
}
628+
629+
return 'plugins/MultiSites/images/evolution_equal.svg';
630+
}
631+
632+
/**
633+
* Throw an exception if the segment is not pre-processed.
634+
* We do not want to compute data for real-time segments to avoid performance issues.
635+
*/
636+
private function checkSegmentIsPreProcessed(string $segmentDefinition): void
637+
{
638+
if (empty($segmentDefinition)) {
639+
return;
640+
}
641+
642+
$normalizedDefinition = Common::unsanitizeInputValue($segmentDefinition);
643+
$segment = $this->model->getSegmentByDefinition($normalizedDefinition);
644+
645+
// Missing segments are allowed since we want data for "All Visits" too
646+
if (!$segment) {
647+
return;
648+
}
649+
650+
if (empty((int)($segment['auto_archive'] ?? 0))) {
651+
throw new Exception(Piwik::translate('SegmentEditor_ManageSegmentsRealtimeNoDataTooltip'));
652+
}
653+
}
567654
}

plugins/SegmentEditor/Controller.php

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,15 @@
99

1010
namespace Piwik\Plugins\SegmentEditor;
1111

12-
use Piwik\DataTable\Filter\CalculateEvolutionFilter;
13-
use Piwik\DataTable\Renderer\Json;
14-
use Piwik\Container\StaticContainer;
15-
use Piwik\Log\LoggerInterface;
16-
use Piwik\Period\Range;
1712
use Piwik\Piwik;
1813
use Piwik\Plugins\SegmentEditor;
1914
use Piwik\Plugins\SegmentEditor\API as SegmentEditorAPI;
20-
use Piwik\Plugins\VisitsSummary;
2115
use Piwik\Request;
2216
use Piwik\Url;
2317
use Piwik\View;
2418

2519
class Controller extends \Piwik\Plugin\Controller
2620
{
27-
private const POSITIVE = 'positive';
28-
private const NEGATIVE = 'negative';
29-
private const STABLE = 'stable';
30-
3121
/** The requested period */
3222
protected $period;
3323

@@ -81,85 +71,11 @@ public function manageSegments(): string
8171
return $view->render();
8272
}
8373

84-
public function getSegmentData(): string
85-
{
86-
$segmentDefinition = Request::fromRequest()->getStringParameter('segmentDefinition', '');
87-
Json::sendHeaderJSON();
88-
89-
try {
90-
$data = VisitsSummary\API::getInstance()
91-
->get($this->idSite, $this->period, $this->strDate, $segmentDefinition)
92-
->getFirstRow()->getArrayCopy();
93-
[$previousDate] = Range::getLastDate($this->strDate, $this->period);
94-
$pastNbVisits = VisitsSummary\API::getInstance()
95-
->getVisits($this->idSite, $this->period, $previousDate, $segmentDefinition)
96-
->getFirstRow()->getColumn('nb_visits');
97-
98-
$nbVisits = (int)($data['nb_visits'] ?? 0);
99-
$nbActions = (int)($data['nb_actions'] ?? 0);
100-
$pastNbVisits = (int)$pastNbVisits;
101-
$evolutionDirection = $this->getEvolutionDirection($nbVisits, $pastNbVisits);
102-
103-
return json_encode([
104-
'nb_visits' => $nbVisits,
105-
'nb_actions' => $nbActions,
106-
'evolution_visits_direction' => $evolutionDirection,
107-
'evolution_visits_icon' => $this->getEvolutionIcon($evolutionDirection),
108-
'evolution_visits' => CalculateEvolutionFilter::calculate($nbVisits, $pastNbVisits, 0, true, false),
109-
]);
110-
} catch (\Throwable $e) {
111-
StaticContainer::get(LoggerInterface::class)->warning(
112-
'SegmentEditor.getSegmentData failed (idSite: {idSite}, period: {period}, date: {date}, segmentDefinition: {segmentDefinition}): {exception}',
113-
[
114-
'idSite' => $this->idSite,
115-
'period' => $this->period,
116-
'date' => $this->strDate,
117-
'segmentDefinition' => $segmentDefinition,
118-
'exception' => $e,
119-
]
120-
);
121-
122-
return json_encode([
123-
'result' => 'error',
124-
'message' => Piwik::translate('General_ErrorRequest'),
125-
]);
126-
}
127-
}
128-
12974
private function isRealtimeSegment(array $segment): bool
13075
{
13176
return !empty($segment['definition']) && empty((int)$segment['auto_archive']);
13277
}
13378

134-
protected function getEvolutionDirection(int $currentValue, int $pastValue): string
135-
{
136-
if ($currentValue > $pastValue) {
137-
return self::POSITIVE;
138-
}
139-
140-
if ($currentValue < $pastValue) {
141-
return self::NEGATIVE;
142-
}
143-
144-
return self::STABLE;
145-
}
146-
147-
/*
148-
* @param self::POSITIVE|self::NEGATIVE|self::STABLE $direction
149-
*/
150-
protected function getEvolutionIcon(string $direction): string
151-
{
152-
if ($direction === self::POSITIVE) {
153-
return 'plugins/MultiSites/images/arrow_up.png';
154-
}
155-
156-
if ($direction === self::NEGATIVE) {
157-
return 'plugins/MultiSites/images/arrow_down.png';
158-
}
159-
160-
return 'plugins/MultiSites/images/stop.png';
161-
}
162-
16379
protected function getSegmentSparklineUrl(array $segment): string
16480
{
16581
$params = $this->getGraphParamsModified([

plugins/SegmentEditor/javascripts/manageSegmentsPage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ function initManageSegmentsPage() {
211211
const date = getReportingParam('date');
212212
const ajaxHandler = new ajaxHelper();
213213
ajaxHandler.addParams({
214-
module: 'SegmentEditor',
215-
action: 'getSegmentData',
214+
module: 'API',
215+
method: 'SegmentEditor.getSegmentData',
216216
format: 'json',
217-
segmentDefinition,
217+
segment: segmentDefinition,
218218
idSite,
219219
period,
220220
date,

plugins/SegmentEditor/tests/Integration/ControllerTest.php

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
namespace Piwik\Plugins\SegmentEditor\tests\Integration;
1111

12+
use Exception;
1213
use Piwik\ArchiveProcessor\Rules;
1314
use Piwik\Container\StaticContainer;
1415
use Piwik\NoAccessException;
1516
use Piwik\Option;
1617
use Piwik\Plugins\SegmentEditor\API;
17-
use Piwik\Plugins\SegmentEditor\Controller;
1818
use Piwik\Tests\Framework\Fixture;
1919
use Piwik\Tests\Framework\Mock\FakeAccess;
2020
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
@@ -172,19 +172,14 @@ public function testGetSegmentDataReturnsMetricsForGivenSegment(): void
172172
{
173173
Rules::setBrowserTriggerArchiving(false);
174174

175-
$_GET = [
176-
'idSite' => '1',
177-
'period' => 'range',
178-
'date' => '2010-03-06,2010-03-08',
179-
'segmentDefinition' => '',
180-
];
181-
$_REQUEST = $_GET;
182-
183-
$response = (new Controller())->getSegmentData();
184-
$data = json_decode($response, true);
175+
$data = API::getInstance()->getSegmentData(
176+
1,
177+
'range',
178+
'2010-03-06,2010-03-08',
179+
''
180+
);
185181

186182
$this->assertIsArray($data);
187-
$this->assertArrayNotHasKey('result', $data);
188183
$this->assertArrayHasKey('nb_visits', $data);
189184
$this->assertArrayHasKey('nb_actions', $data);
190185
$this->assertArrayHasKey('evolution_visits_direction', $data);
@@ -203,34 +198,19 @@ public function testGetSegmentDataReturnsErrorForInvalidSegmentDefinition(): voi
203198
{
204199
Rules::setBrowserTriggerArchiving(false);
205200

206-
$_GET = [
207-
'idSite' => '1',
208-
'period' => 'range',
209-
'date' => '2010-03-06,2010-03-08',
210-
'segmentDefinition' => 'thisSegmentDefinitelyDoesNotExist==1',
211-
];
212-
$_REQUEST = $_GET;
213-
214-
$response = (new Controller())->getSegmentData();
215-
$data = json_decode($response, true);
216-
217-
$this->assertIsArray($data);
218-
$this->assertSame('error', $data['result'] ?? null);
219-
$this->assertNotEmpty($data['message'] ?? null);
220-
$this->assertStringNotContainsString('thisSegmentDefinitelyDoesNotExist', $data['message']);
201+
$this->expectException(Exception::class);
202+
API::getInstance()->getSegmentData(
203+
1,
204+
'range',
205+
'2010-03-06,2010-03-08',
206+
'thisSegmentDefinitelyDoesNotExist==1'
207+
);
221208
}
222209

223210
public function testGetSegmentDataRequiresValidSiteInRequest(): void
224211
{
225-
$_GET = [
226-
'period' => 'range',
227-
'date' => '2010-03-06,2010-03-08',
228-
'segmentDefinition' => '',
229-
];
230-
$_REQUEST = $_GET;
231-
232212
$this->expectException(\Exception::class);
233-
new Controller();
213+
API::getInstance()->getSegmentData(999999, 'range', '2010-03-06,2010-03-08', '');
234214
}
235215

236216
public function testGetSegmentDataRequiresViewAccess(): void
@@ -240,16 +220,8 @@ public function testGetSegmentDataRequiresViewAccess(): void
240220
StaticContainer::getContainer()->set('Piwik\Access', $fakeAccess);
241221

242222
try {
243-
$_GET = [
244-
'idSite' => '1',
245-
'period' => 'range',
246-
'date' => '2010-03-06,2010-03-08',
247-
'segmentDefinition' => '',
248-
];
249-
$_REQUEST = $_GET;
250-
251223
$this->expectException(NoAccessException::class);
252-
new Controller();
224+
API::getInstance()->getSegmentData(1, 'range', '2010-03-06,2010-03-08', '');
253225
} finally {
254226
StaticContainer::getContainer()->set('Piwik\Access', $originalAccess);
255227
}
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)