Skip to content

Commit

Permalink
Add count command
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Mar 6, 2023
1 parent 635b1bf commit 76eed20
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
10 changes: 0 additions & 10 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ public function batch()
if(!config('beacon.enabled') || empty(config('beacon.api_key')))
return;

// $data = Cache::get(config('beacon.cache_key') . '_' . $this->metric->type);

// if(is_array($data)){
// $data[] = $this->metric;
// }
// else {
// $data = [];
// $data[] = $this->metric;
// }

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

}
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 76eed20

Please sign in to comment.