Skip to content

Commit

Permalink
if models-count use a scope ( and scope value ) , add the scope (and …
Browse files Browse the repository at this point in the history
…the scope value) to the metric label
  • Loading branch information
eloquentize committed Mar 1, 2024
1 parent 1f7111e commit 297df9e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Commands/ModelsCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ public function performModelCount(array $models, CarbonPeriod $period, string $e
}

$count = $query->count();
$this->verbose("Counting $model - count: ".$count);
$metrics[] = (object) ['label' => $model, 'count' => $count];

$label = $model;
if ($scope && $scopeValue) {
$label .= '::'.$scope.'('.$scopeValue.')';
} else if ($scope) {
$label .= '::'.$scope;
}



$this->verbose("Counting $label - count: ".$count);
$metrics[] = (object) ['label' => $label, 'count' => $count];

}

Expand Down
25 changes: 25 additions & 0 deletions tests/commands/RunModelCountWithScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\Testing\Models\Bill;
use App\Testing\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\BufferedOutput;

it('ensure models-count is callable', function () {
// Http::fake([
// config('eloquentize.api_url').'/api/metrics/models' => Http::response(['status' => 'ok'], 200),
// ]);

// $this->artisan('eloquentize:models-count --modelsPath=Testing/Models -v --scope=PriceOver --scopeValue=1 ')
// ->assertExitCode(Command::SUCCESS);

$this->output = new BufferedOutput();
Artisan::call('eloquentize:models-count --modelsPath=Testing/Models -v --models=Bill --scope=PriceOver --scopeValue=1 -v',[], $this->output);
$outputContent = $this->output->fetch();
echo "out :".$outputContent;
})->with([
fn () => Bill::factory()->create(['ref' => 'BILL_0000001', 'price' => 1000]),
fn () => Bill::factory()->create(['ref' => 'BILL_0000002', 'price' => 500]),
])->only();

0 comments on commit 297df9e

Please sign in to comment.