Skip to content

Commit

Permalink
Merge pull request #140 from neo4j-php/analytics
Browse files Browse the repository at this point in the history
added mixpanel analytics
  • Loading branch information
stefanak-michal authored Jul 13, 2024
2 parents 5639c82 + 1d27d7c commit 0d4c229
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ environments for security reasons._

Server state is not reported by server but it is evaluated by received response. You can access current state through property `$protocol->serverState`. This property is updated with every call `getResponse(s)`.

## :bar_chart: Analytics

Bolt does collect anonymous analytics data. These data are stored offline (as files in temp directory) and submitted once a day. You can opt out with environment variable `BOLT_ANALYTICS_OPTOUT`.

Analytics data are public and available at [Mixpanel](https://eu.mixpanel.com/p/7ttVKqvjdqJtGCjLCFgdeC).

## :pushpin: More solutions

If you need simple class to cover basic functionality you can
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require": {
"php": "^8.1",
"ext-mbstring": "*",
"ext-curl": "*",
"psr/simple-cache": "^3.0"
},
"require-dev": {
Expand Down
47 changes: 47 additions & 0 deletions src/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,56 @@ final class Bolt

public function __construct(private IConnection $connection)
{
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR)) {
mkdir(getcwd() . DIRECTORY_SEPARATOR . 'temp');
}
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
$this->track();
}
$this->setProtocolVersions(5.4, 5, 4.4);
}

private function track(): void
{
foreach (glob(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
$time = intval(explode('.', basename($file))[1]);
if ($time < strtotime('today')) {
$count = file_get_contents($file);
unlink($file);

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-eu.mixpanel.com/import?strict=0&project_id=3355308',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => $this->connection->getTimeout(),
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => json_encode([
[
'properties' => [
'$insert_id' => $file,
'distinct_id' => sha1(implode('', [php_uname(), disk_total_space('.'), filectime('/'), phpversion()])),
'amount' => $count,
'time' => $time
],
'event' => 'queries'
]
]),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'accept: application/json',
'authorization: Basic MDJhYjRiOWE2YTM4MThmNWFlZDEzYjNiMmE5M2MxNzQ6',
],
]);
curl_exec($curl);
curl_close($curl);
}
}
}

/**
* Connect via Connection, execute handshake on it, create and return protocol version class
* @throws BoltException
Expand Down
27 changes: 16 additions & 11 deletions src/helpers/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
*/
class FileCache implements CacheInterface
{
private string $tempDir;

public function __construct()
{
if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR))
mkdir(__DIR__ . DIRECTORY_SEPARATOR . 'temp');
$this->tempDir = getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;

if (!file_exists($this->tempDir)) {
mkdir(rtrim($this->tempDir, DIRECTORY_SEPARATOR));
}

// clean old
foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'temp') as $file) {
foreach (scandir($this->tempDir . 'temp') as $file) {
if ($file == '.' || $file == '..')
continue;
if (filemtime(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $file) < strtotime('-1 hour'))
unlink(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $file);
if (filemtime($this->tempDir . $file) < strtotime('-1 hour'))
unlink($this->tempDir . $file);
}
}

Expand All @@ -37,7 +42,7 @@ public function __construct()
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->has($key) ? file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $key) : $default;
return $this->has($key) ? file_get_contents($this->tempDir . $key) : $default;
}

/**
Expand All @@ -53,7 +58,7 @@ public function get(string $key, mixed $default = null): mixed
*/
public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool
{
return is_int(file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $key, $value));
return is_int(file_put_contents($this->tempDir . $key, $value));
}

/**
Expand All @@ -65,18 +70,18 @@ public function set(string $key, mixed $value, \DateInterval|int|null $ttl = nul
*/
public function delete(string $key): bool
{
return $this->has($key) && unlink(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $key);
return $this->has($key) && unlink($this->tempDir . $key);
}

/**
* @inheritDoc
*/
public function clear(): bool
{
foreach (scandir(__DIR__ . DIRECTORY_SEPARATOR . 'temp') as $file) {
foreach (scandir(rtrim($this->tempDir, DIRECTORY_SEPARATOR)) as $file) {
if ($file == '.' || $file == '..')
continue;
unlink(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $file);
unlink($this->tempDir . $file);
}
return true;
}
Expand Down Expand Up @@ -143,6 +148,6 @@ public function deleteMultiple(iterable $keys): bool
*/
public function has(string $key): bool
{
return file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . $key);
return file_exists($this->tempDir . $key);
}
}
13 changes: 12 additions & 1 deletion src/protocol/AProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,19 @@ public function __construct(
*/
protected function write(iterable $generator): void
{
foreach ($generator as $buffer)
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
$this->track();
}
foreach ($generator as $buffer) {
$this->connection->write($buffer);
}
}

private function track(): void
{
$file = getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
file_put_contents($file, $count + 1);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/protocol/ATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public function readCallback(int $length = 2048): string
*/
protected function setUp(): void
{
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR)) {
mkdir(getcwd() . DIRECTORY_SEPARATOR . 'temp');
}

self::$readBuffer = '';
self::$readArray = [];
self::$writeIndex = 0;
Expand Down

0 comments on commit 0d4c229

Please sign in to comment.