diff --git a/src/Collector.php b/src/Collector.php index c704e9a..d60a8df 100644 --- a/src/Collector.php +++ b/src/Collector.php @@ -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); } } diff --git a/src/CollectorServiceProvider.php b/src/CollectorServiceProvider.php index 4e848b2..f52a309 100644 --- a/src/CollectorServiceProvider.php +++ b/src/CollectorServiceProvider.php @@ -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 { @@ -28,6 +29,7 @@ public function boot() $this->commands([ ForceSend::class, PurgeAnalytics::class, + CountMetrics::class, ]); } } @@ -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; }); diff --git a/src/Commands/CountMetrics.php b/src/Commands/CountMetrics.php new file mode 100644 index 0000000..057ef20 --- /dev/null +++ b/src/Commands/CountMetrics.php @@ -0,0 +1,52 @@ +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"; + } +} diff --git a/src/Commands/PurgeAnalytics.php b/src/Commands/PurgeAnalytics.php index ded9c2d..a5b6647 100644 --- a/src/Commands/PurgeAnalytics.php +++ b/src/Commands/PurgeAnalytics.php @@ -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 { @@ -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)