Skip to content

Commit

Permalink
Merge pull request #3 from The-3Labs-Team/grouped
Browse files Browse the repository at this point in the history
Grouped
  • Loading branch information
murdercode authored Aug 5, 2024
2 parents b262122 + c2db393 commit 5f18e48
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 25 deletions.
51 changes: 33 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
48 changes: 42 additions & 6 deletions src/CloudflareAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CloudflareAnalytics

protected array $orderBys = [];

protected $takes = [];
private $takes = [];

/**
* CloudflareAnalytics constructor.
Expand Down Expand Up @@ -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[] = <<<GRAPHQL
$alias: $selector(
filter: {
datetime_gt: "$startDate",
datetime_lt: "$endDate"
datetime_gt: "$startDate",
datetime_lt: "$endDate"
}
limit: $limit
orderBy: [
Expand All @@ -153,6 +156,39 @@ public function get(...$fields)
return $response;
}

private function formatFields(array $fields, $alias)
{
$formattedFields = [];
foreach ($fields as $field) {
$field = str_replace("$alias.", '', $field); // Rimuove il prefisso alias
$parts = explode('.', $field);
$current = &$formattedFields;
foreach ($parts as $part) {
if (! isset($current[$part])) {
$current[$part] = [];
}
$current = &$current[$part];
}
}

return $this->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));
Expand Down
26 changes: 26 additions & 0 deletions tests/CloudflareAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 5f18e48

Please sign in to comment.