Skip to content

Commit 3480c8a

Browse files
committed
ImageGraph now requests and processes raw metrics
1 parent 7998f15 commit 3480c8a

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

plugins/ImageGraph/API.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ public function get(
425425
'hideMetricsDoc' => false,
426426
'idSubtable' => $idSubtable,
427427
'showRawMetrics' => false,
428+
'format_metrics' => 0,
428429
];
429430
/** @var array $processedReport */
430431
$processedReport = Request::processRequest('API.getProcessedReport', $parameters);
@@ -470,8 +471,7 @@ public function get(
470471
}
471472
$i++;
472473
}
473-
} else // if the report has no dimension we have multiple reports each with only one row within the reportData
474-
{
474+
} else { // if the report has no dimension we have multiple reports each with only one row within the reportData
475475
/** @var DataTable[] $periodsData */
476476
$periodsData = array_values($reportData->getDataTables());
477477
$periodsCount = count($periodsData);
@@ -592,9 +592,13 @@ private function setFilterTruncate(int $default): void
592592
$_GET['filter_truncate'] = PiwikRequest::fromRequest()->getIntegerParameter('filter_truncate', $default);
593593
}
594594

595-
private static function parseOrdinateValue($ordinateValue)
595+
private static function parseOrdinateValue($ordinateValue): float
596596
{
597-
$ordinateValue = @str_replace(',', '.', $ordinateValue);
597+
if (is_int($ordinateValue) || is_float($ordinateValue)) {
598+
return (float) $ordinateValue;
599+
}
600+
601+
$ordinateValue = @str_replace(',', '.', (string) $ordinateValue);
598602

599603
// convert hh:mm:ss formatted time values to number of seconds
600604
if (preg_match('/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\.[0-9]{2})?)/', $ordinateValue, $matches)) {
@@ -606,7 +610,13 @@ private static function parseOrdinateValue($ordinateValue)
606610
}
607611

608612
// OK, only numbers from here please (strip out currency sign)
609-
return preg_replace('/[^0-9.]/', '', $ordinateValue);
613+
$ordinateValue = preg_replace('/[^0-9.]/', '', $ordinateValue);
614+
615+
if ($ordinateValue === null || $ordinateValue === '' || $ordinateValue === '.') {
616+
return 0.0;
617+
}
618+
619+
return is_numeric($ordinateValue) ? (float) $ordinateValue : 0.0;
610620
}
611621

612622
private static function getFontPath(string $font): string
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Piwik\Plugins\ImageGraph\tests\Unit;
4+
5+
use Piwik\Plugins\ImageGraph\API;
6+
use ReflectionMethod;
7+
8+
/**
9+
* @group ImageGraph
10+
* @group APITest
11+
*/
12+
class APITest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @dataProvider getOrdinateValuesToParse
16+
*/
17+
public function testParseOrdinateValueReturnsFloat($value, float $expected): void
18+
{
19+
$method = new ReflectionMethod(API::class, 'parseOrdinateValue');
20+
21+
if (PHP_VERSION_ID < 80100) {
22+
$method->setAccessible(true);
23+
}
24+
25+
$actual = $method->invoke(null, $value);
26+
27+
$this->assertIsFloat($actual);
28+
$this->assertSame($expected, $actual);
29+
}
30+
31+
public function getOrdinateValuesToParse(): array
32+
{
33+
return [
34+
[2.05, 2.05],
35+
['2.05', 2.05],
36+
[205, 205.0],
37+
['01:02:03.50', 3723.5],
38+
['', 0.0],
39+
];
40+
}
41+
}

0 commit comments

Comments
 (0)