Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Aug 20, 2023
2 parents 32b1f84 + b300e2d commit ffc0a7b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function batch()
if(!config('beacon.enabled') || empty(config('beacon.api_key')))
return;

Cache::put(config('beacon.cache_key') . $this->metric->type.microtime(true), $this->metric);
Cache::put(config('beacon.cache_key') . $this->metric->type.microtime(true), $this->metric, 1800);

}
}
13 changes: 5 additions & 8 deletions src/CollectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Turbo124\Beacon;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;
use Turbo124\Beacon\Collector;
use Turbo124\Beacon\Jobs\BatchMetrics;
use Illuminate\Support\ServiceProvider;
use Turbo124\Beacon\Commands\ForceSend;
use Turbo124\Beacon\Commands\CountMetrics;
use Illuminate\Console\Scheduling\Schedule;
use Turbo124\Beacon\Commands\PurgeAnalytics;
use Turbo124\Beacon\Jobs\BatchMetrics;

class CollectorServiceProvider extends ServiceProvider
{
Expand All @@ -28,6 +29,7 @@ public function boot()
$this->commands([
ForceSend::class,
PurgeAnalytics::class,
CountMetrics::class,
]);
}
}
Expand All @@ -40,11 +42,6 @@ public function register()
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/beacon.php', 'beacon');

// Register the main class to use with the facade
// $this->app->singleton('collector', function () {
// return new Collector;
// });

$this->app->bind('collector', function (){
return new Collector;
});
Expand Down
52 changes: 52 additions & 0 deletions src/Commands/CountMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Turbo124\Beacon\Commands;

use App;
use Illuminate\Console\Command;
use Illuminate\Support\Facades;

class CountMetrics extends Command
{
/**
* @var string
*/
protected $name = 'beacon:count';

/**
* @var string
*/
protected $description = 'Counting any analytics in the cache';

protected $log = '';

public function handle()
{
$this->logMessage('Counting metrics');

$metric_types = ['counter', 'gauge', 'multi_metric', 'mixed_metric'];

foreach ($metric_types as $type) {


$redis = Facades\Redis::connection(config('beacon.cache_connection',''));

$prefix = config('cache.prefix').':'.config('beacon.cache_key').$type.'*';

$keys = $redis->keys($prefix);

$this->logMessage("{$type} - ".count($keys)." keys");

}

$this->logMessage('Finished Counting metrics');

}

private function logMessage($str)
{
$str = date('Y-m-d h:i:s') . ' ' . $str;
$this->info($str);
$this->log .= $str . " \n";
}
}
27 changes: 18 additions & 9 deletions src/Commands/PurgeAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use App;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Turbo124\Beacon\Jobs\BatchMetrics;

use Illuminate\Support\Facades;

class PurgeAnalytics extends Command
{
Expand All @@ -26,19 +24,30 @@ public function handle()
{
$this->logMessage('Purging Data');

$metric_types = ['counter', 'gauge', 'multi_metric', 'mixed_metric'];
$metric_types = ['counter', 'gauge', 'multi_metric', 'mixed_metric'];

foreach ($metric_types as $type) {

foreach($metric_types as $type)
{
$this->logMessage("purging {$type}");

$redis = Facades\Redis::connection(config('beacon.cache_connection',''));

$this->logMessage("purging {$type}");
$prefix = config('cache.prefix').':'.config('beacon.cache_key').$type.'*';

Cache::forget(config('beacon.cache_key') . '_' . $type);
$keys = $redis->keys($prefix);

if (count($keys) > 0) {
$redis->pipeline(function ($pipe) use ($keys) {
foreach ($keys as $key) {
$pipe->del($key);
}
});
}

}

$this->logMessage('Finished Purging Data');


}

private function logMessage($str)
Expand Down

0 comments on commit ffc0a7b

Please sign in to comment.