From 0d0be08b3d6af4c0802ebf7eeeebe165b94b1e78 Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Mon, 5 Aug 2024 14:22:32 +0200 Subject: [PATCH 1/4] chore: Add spatie/ray dependency to composer.json --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 73c9545..ef4293f 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ }, "require-dev": { "pestphp/pest": "^2.20", - "laravel/pint": "^1.0" + "laravel/pint": "^1.0", + "spatie/ray": "^1.28" }, "autoload": { "psr-4": { From 043bc55d6decfc7b5733c8d957cb43cbadd830f9 Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Mon, 5 Aug 2024 14:38:10 +0200 Subject: [PATCH 2/4] Support for nested filters --- README.md | 2 +- src/CloudflareAnalytics.php | 52 +++++++++++++++++++++++++------ tests/CloudflareAnalyticsTest.php | 28 +++++++++++++++++ 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a58bce6..2fdcec7 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ The `get` method will return an array with the results. ## Default fields -- `datetime`: 1 day +- `datetime`: 1 hour - `take`: 10 - `orderBy`: `datetime` diff --git a/src/CloudflareAnalytics.php b/src/CloudflareAnalytics.php index 3a58b41..e728352 100755 --- a/src/CloudflareAnalytics.php +++ b/src/CloudflareAnalytics.php @@ -2,9 +2,9 @@ namespace The3LabsTeam\PhpCloudflareAnalytics; -use DateInterval; -use DateTime; use Dotenv\Dotenv; +use DateTime; +use DateInterval; class CloudflareAnalytics { @@ -20,7 +20,7 @@ class CloudflareAnalytics protected array $orderBys = []; - protected $takes = []; + private $takes = []; /** * CloudflareAnalytics constructor. @@ -102,7 +102,6 @@ public function orderBy(string $context, string $field, string $direction = 'ASC public function take($alias, $limit) { $this->takes[$alias] = $limit; - return $this; } @@ -114,19 +113,22 @@ public function get(...$fields) $queries = []; foreach ($this->selectors as $alias => $selector) { $filter = $this->filters[$alias] ?? []; - $orderBy = $this->orderBys[$alias] ?? ['datetime_DESC']; + $orderBy = array_filter($this->orderBys[$alias] ?? [], function ($order) { + return strpos($order, 'datetime') === false; + }); $limit = isset($this->takes[$alias]) ? $this->takes[$alias] : 10; - $startDate = $filter['startDate'] ?? (new DateTime)->sub(new DateInterval('P1D'))->format('c'); + $startDate = $filter['startDate'] ?? (new DateTime)->sub(new DateInterval('PT1H'))->format('c'); $endDate = $filter['endDate'] ?? (new DateTime)->format('c'); - $fieldsList = implode("\n", array_map(fn ($f) => str_replace("$alias.", '', $f), $fields)); + // Formatta i campi nidificati + $fieldsList = $this->formatFields($fields, $alias); $queries[] = <<buildFieldString($formattedFields); + } + + private function buildFieldString(array $fields, $indent = 2) + { + $result = ''; + foreach ($fields as $key => $value) { + $result .= str_repeat(' ', $indent) . $key; + if (is_array($value) && !empty($value)) { + $result .= " {\n" . $this->buildFieldString($value, $indent + 2) . str_repeat(' ', $indent) . "}\n"; + } else { + $result .= "\n"; + } + } + return $result; + } + + private function formatOrderBy(array $orderBy) { return implode("\n", array_map(fn ($o) => $o, $orderBy)); diff --git a/tests/CloudflareAnalyticsTest.php b/tests/CloudflareAnalyticsTest.php index 4c69692..e4279cd 100644 --- a/tests/CloudflareAnalyticsTest.php +++ b/tests/CloudflareAnalyticsTest.php @@ -6,6 +6,33 @@ $this->cf = new CloudflareAnalytics; }); +it('can get total views sum by date', function () { + $cf = new CloudflareAnalytics; + + $results = $cf->select('httpRequests1mGroups AS http') + ->take('http', 10) + ->get('sum.countryMap.clientCountryName', 'sum.countryMap.requests', 'sum.countryMap.bytes', 'sum.countryMap.threats', 'dimensions.datetimeHour'); + + $this->assertIsArray($results); + $this->assertGreaterThan(0, $results); +}); + +it('can get total views sum by date between two dates', function () { + $startDate = (new DateTime)->sub(new DateInterval('P51M'))->format('c'); + $endDate = (new DateTime)->format('c'); + + $cf = new CloudflareAnalytics; + + $results = $cf->select('httpRequests1mGroups AS http') + ->whereBetween('http', $startDate, $endDate) + ->take('http', 10) + ->get('sum.countryMap.clientCountryName', 'sum.countryMap.requests', 'sum.countryMap.bytes', 'sum.countryMap.threats', 'dimensions.datetimeHour'); + + dd($results); + $this->assertIsArray($results); + $this->assertGreaterThan(0, $results); +}); + it('can get firewall data', function () { $cf = new CloudflareAnalytics; @@ -86,3 +113,4 @@ $this->assertIsArray($results); $this->assertGreaterThan(0, $results); }); + From f27264be3b426ebc4dfbd033377a944e2ae33eac Mon Sep 17 00:00:00 2001 From: murdercode Date: Mon, 5 Aug 2024 12:38:31 +0000 Subject: [PATCH 3/4] Fix styling --- src/CloudflareAnalytics.php | 18 ++++++++++-------- tests/CloudflareAnalyticsTest.php | 3 +-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/CloudflareAnalytics.php b/src/CloudflareAnalytics.php index e728352..1bac0b9 100755 --- a/src/CloudflareAnalytics.php +++ b/src/CloudflareAnalytics.php @@ -2,9 +2,9 @@ namespace The3LabsTeam\PhpCloudflareAnalytics; -use Dotenv\Dotenv; -use DateTime; use DateInterval; +use DateTime; +use Dotenv\Dotenv; class CloudflareAnalytics { @@ -102,6 +102,7 @@ public function orderBy(string $context, string $field, string $direction = 'ASC public function take($alias, $limit) { $this->takes[$alias] = $limit; + return $this; } @@ -159,16 +160,17 @@ private function formatFields(array $fields, $alias) { $formattedFields = []; foreach ($fields as $field) { - $field = str_replace("$alias.", "", $field); // Rimuove il prefisso alias + $field = str_replace("$alias.", '', $field); // Rimuove il prefisso alias $parts = explode('.', $field); $current = &$formattedFields; foreach ($parts as $part) { - if (!isset($current[$part])) { + if (! isset($current[$part])) { $current[$part] = []; } $current = &$current[$part]; } } + return $this->buildFieldString($formattedFields); } @@ -176,17 +178,17 @@ private function buildFieldString(array $fields, $indent = 2) { $result = ''; foreach ($fields as $key => $value) { - $result .= str_repeat(' ', $indent) . $key; - if (is_array($value) && !empty($value)) { - $result .= " {\n" . $this->buildFieldString($value, $indent + 2) . str_repeat(' ', $indent) . "}\n"; + $result .= str_repeat(' ', $indent).$key; + if (is_array($value) && ! empty($value)) { + $result .= " {\n".$this->buildFieldString($value, $indent + 2).str_repeat(' ', $indent)."}\n"; } else { $result .= "\n"; } } + return $result; } - private function formatOrderBy(array $orderBy) { return implode("\n", array_map(fn ($o) => $o, $orderBy)); diff --git a/tests/CloudflareAnalyticsTest.php b/tests/CloudflareAnalyticsTest.php index e4279cd..2f4b85d 100644 --- a/tests/CloudflareAnalyticsTest.php +++ b/tests/CloudflareAnalyticsTest.php @@ -28,7 +28,7 @@ ->take('http', 10) ->get('sum.countryMap.clientCountryName', 'sum.countryMap.requests', 'sum.countryMap.bytes', 'sum.countryMap.threats', 'dimensions.datetimeHour'); - dd($results); + dd($results); $this->assertIsArray($results); $this->assertGreaterThan(0, $results); }); @@ -113,4 +113,3 @@ $this->assertIsArray($results); $this->assertGreaterThan(0, $results); }); - From 6928453ac6e96e033db5dd7a163da8b45d2063af Mon Sep 17 00:00:00 2001 From: Stefano Novelli Date: Mon, 5 Aug 2024 14:41:40 +0200 Subject: [PATCH 4/4] Better docs & tests --- README.md | 49 ++++++++++++++++++++----------- tests/CloudflareAnalyticsTest.php | 1 - 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2fdcec7..385e9c5 100644 --- a/README.md +++ b/README.md @@ -64,40 +64,55 @@ The `get` method will return an array with the results. ## Demo -Get last http visits: +Get latest 10 firewall events: ```php -$results = $cf->select('httpRequestsAdaptiveGroups AS http') - ->get('http.datetime', 'http.requests'); +$results = $cf->select('firewallEventsAdaptive AS firewall') + ->get('firewall.datetime', 'firewall.action'); ``` -Get last http visits between two dates: +Filter between two dates: ```php -$startDate = (new DateTime)->sub(new DateInterval('P1D'))->format('c'); -$endDate = (new DateTime)->format('c'); +$results = $cf->select('firewallEventsAdaptive AS firewall') + ->where('firewall.datetime', '>=', '2021-10-01T00:00:00Z') + ->where('firewall.datetime', '<=', '2021-10-02T00:00:00Z') + ->get('firewall.datetime', 'firewall.action'); +``` -$results = $cf->select('httpRequestsAdaptiveGroups AS http') - ->whereBetween('http', $startDate, $endDate) - ->get('http.datetime', 'http.requests'); +Limit the results: + +```php +$results = $cf->select('firewallEventsAdaptive AS firewall') + ->take('firewall', 5) + ->get('firewall.datetime', 'firewall.action'); ``` -Get last http visits, limit the results to 2: +Order the results: ```php -$results = $cf->select('httpRequestsAdaptiveGroups AS http') - ->take('http', 2) - ->get('http.datetime', 'http.requests'); +$results = $cf->select('firewallEventsAdaptive AS firewall') + ->orderBy('firewall.datetime', 'desc') + ->get('firewall.datetime', 'firewall.action'); +``` + +Get two fields from two different tables: // TODO: test this + +```php + +$results = $cf->select('firewallEventsAdaptive AS firewall, threatsAdaptiveGroups AS threats') + ->get('firewall.datetime', 'firewall.action', 'threats.datetime', 'threats.action'); ``` -Get last http visits, order by datetime: +Get http visits and sum them: ```php -$results = $cf->select('httpRequestsAdaptiveGroups AS http') - ->orderBy('http', 'datetime') - ->get('http.datetime', 'http.requests'); + $results = $cf->select('httpRequests1mGroups AS http') + ->take('http', 10) + ->get('sum.countryMap.clientCountryName', 'sum.countryMap.requests', 'sum.countryMap.bytes', 'sum.countryMap.threats', 'dimensions.datetimeHour'); ``` + ## Testing ```bash diff --git a/tests/CloudflareAnalyticsTest.php b/tests/CloudflareAnalyticsTest.php index e4279cd..ad5ad50 100644 --- a/tests/CloudflareAnalyticsTest.php +++ b/tests/CloudflareAnalyticsTest.php @@ -28,7 +28,6 @@ ->take('http', 10) ->get('sum.countryMap.clientCountryName', 'sum.countryMap.requests', 'sum.countryMap.bytes', 'sum.countryMap.threats', 'dimensions.datetimeHour'); - dd($results); $this->assertIsArray($results); $this->assertGreaterThan(0, $results); });