diff --git a/README.md b/README.md index a58bce6..385e9c5 100644 --- a/README.md +++ b/README.md @@ -58,46 +58,61 @@ The `get` method will return an array with the results. ## Default fields -- `datetime`: 1 day +- `datetime`: 1 hour - `take`: 10 - `orderBy`: `datetime` ## 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/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": { diff --git a/src/CloudflareAnalytics.php b/src/CloudflareAnalytics.php index 3a58b41..1bac0b9 100755 --- a/src/CloudflareAnalytics.php +++ b/src/CloudflareAnalytics.php @@ -20,7 +20,7 @@ class CloudflareAnalytics protected array $orderBys = []; - protected $takes = []; + private $takes = []; /** * CloudflareAnalytics constructor. @@ -114,19 +114,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..d0d3b93 100644 --- a/tests/CloudflareAnalyticsTest.php +++ b/tests/CloudflareAnalyticsTest.php @@ -6,6 +6,32 @@ $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'); + + $this->assertIsArray($results); + $this->assertGreaterThan(0, $results); +}); + it('can get firewall data', function () { $cf = new CloudflareAnalytics;