Skip to content

Commit

Permalink
chore: Refactor CloudflareAnalytics constructor and add dotenv support
Browse files Browse the repository at this point in the history
  • Loading branch information
murdercode committed Aug 5, 2024
1 parent b909ba6 commit 1ba6d42
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 70 deletions.
113 changes: 44 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Cloudflare Analytics GraphQL API PHP Client

This package is a simple PHP client for the Cloudflare Analytics GraphQL API.

## Installation

You can install the package via composer:

```bash
composer require the3labsteam/php-cloudflare-analytics
```

## Configuration

Add in your .env file the following variables:

Expand All @@ -6,96 +19,58 @@ CLOUDFLARE_API_TOKEN='your_cloudflare_api_token'
CLOUDFLARE_ZONE_TAG_ID='zoneTag'
```

## Refactor

Usabe fields: firewallEventsAdaptive,
or you can pass the token and zoneTag as parameters in the constructor.

```php

use The3LabsTeam\PhpCloudflareAnalytics\CloudflareAnalytics;

$cf = new CloudflareAnalytics(
token: 'your_cloudflare_api_token', zoneTag: 'zoneTag'
);
```

$cf = new CloudflareAnalytics();

// Get results
// $cf->get();

// // Filter by date
// $cf->whereBetween('2021-01-01', '2021-01-31')->get();

// // Limit the results
// $cf->take(10)->get();

// // Order the results
// $cf->orderBy('datetiime', 'DESC')->get();
## Usage

You can use the following methods to build your query:

-- multiple filters --
```php
use The3LabsTeam\PhpCloudflareAnalytics\CloudflareAnalytics;
$startDate = (new DateTime)->sub(new DateInterval('P1D'))->format('c');
$endDate = (new DateTime)->format('c');

$cf->select('firewallEventsAdaptive AS last10Events', 'httpRequestsAdaptiveGroups AS top3DeviceTypes')
->whereBetween('last10Events.2021-01-01', 'last10Events.2021-01-31')
->whereBetween('top3DeviceTypes.2021-01-01', 'top3DeviceTypes.2021-01-31')
->orderBy('last10Events.datetime', 'DESC')
->orderBy('top3DeviceTypes.count', 'DESC')
->get();
$cf = new CloudflareAnalytics;

$results = $cf->select('firewallEventsAdaptive AS firewall')
->whereBetween('firewall', $startDate, $endDate)
->orderBy('firewall.datetime', 'DESC')
->take('firewall', 2)
->get('firewall.datetime', 'firewall.action');
```

In the example above, we are querying the `firewallEventsAdaptive` field, filtering by the last 24 hours, ordering by datetime in descending order, and limiting the results to 2.

DEMO MULTIPLE FILTERS

```graphql

{
viewer {
zones(filter: { zoneTag: $tag }) {
last10Events: firewallEventsAdaptive(
filter: {
datetime_gt: $start
datetime_lt: $end
}
limit: 10
orderBy: [
datetime_DESC
]
) {
action
datetime
host: clientRequestHTTPHost
}
top3DeviceTypes: httpRequestsAdaptiveGroups(
filter: {
date: $ts
}
limit: 10
orderBy: [
count_DESC
]
) {
count
dimensions {
device: clientDeviceType
}
}
}
}
}
The `get` method will return an array with the results.

```
## Available fields

- `firewallEventsAdaptive`
- `httpRequests1mGroups`
- `httpRequestsAdaptiveGroups`
- `threatsAdaptiveGroups`
- `threatsByCountryAdaptiveGroups`

## Usage
## Testing

```bash
composer test
```

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

## Credits

and you can pass the following parameters to get the data:
- [The3LabsTeam](https://3labs.it)

- PARAM => `sum` or `uniq`
- PARAM TYPE
- SUM: `request`, `pageViews`, `cachedBytes`, `cachedRequests`, `threats`
- UNIQ: `uniques`
// TODO: Add the rest of the file
2 changes: 1 addition & 1 deletion src/CloudflareAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function get(...$fields)
$orderBy = $this->orderBys[$alias] ?? [];
$limit = isset($this->takes[$alias]) ? $this->takes[$alias] : 10;

$fieldsList = implode("\n", array_map(fn ($f) => str_replace("$alias.", '', $f), $fields));
$fieldsList = implode("\n", array_map(fn ($f) => str_replace("$alias.", "", $f), $fields));

$queries[] = <<<GRAPHQL
$alias: $selector(
Expand Down
12 changes: 12 additions & 0 deletions tests/CloudflareAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
$this->assertGreaterThan(0, $results);
});

it('can get total views', function () {
$cf = new CloudflareAnalytics;

$results = $cf->select('httpRequests1mGroups AS http')
->get('http.sum.requests');

dd($results);

$this->assertIsArray($results);
$this->assertGreaterThan(0, $results);
});

// it('can get total views between two dates', function () {
// $startDate = date('Y-m-d', strtotime('-2 months'));
// $endDate = date('Y-m-d');
Expand Down

0 comments on commit 1ba6d42

Please sign in to comment.