Skip to content

Commit 13b7629

Browse files
committed
add dry run option to models-count
1 parent 8343e4c commit 13b7629

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Eloquentize provides a full-featured monitoring toolkit for Laravel applications
99

1010
[<img src="https://alpha.eloquentize.com/images/eloquentize-logo-tr.svg" width="128px" />](https://alpha.eloquentize.com/docs)
1111

12-
# Getting start
12+
# Getting started
1313

1414
## Installation
1515

src/Commands/BaseCommand.php

+12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
2424
if (env('APP_ENV') === null) {
2525
throw new \Exception('app.env is not set in .env');
2626
}
27+
if (env('APP_URL') === "http://localhost") {
28+
throw new \Exception('app.url is set to http://localhost. Please change these values in .env bevause eloquentize identifies the environment by the app.url and app.env values.');
29+
}
2730

2831
return parent::execute($input, $output);
2932
}
33+
34+
protected function cleanAppUrl($url)
35+
{
36+
$url = preg_replace('#^https?://#', '', $url);
37+
$url = preg_replace('#^http?://#', '', $url);
38+
$url = rtrim($url, '/');
39+
40+
return $url;
41+
}
3042
}

src/Commands/ModelsCount.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ class ModelsCount extends BaseCommand
1515
{
1616
use BuildPeriod, DateArgument, GatherModels, HasVerbose, ModelsOption, PrepareMetricsData, SendMetricsData;
1717

18-
protected $signature = 'eloquentize:models-count {date?} {--event=created_at} {--periodType=daily} {--dateFormat=} {--M|models=} {--modelsPath=} {--scope=} {--scopeValue=} ';
18+
protected $signature = 'eloquentize:models-count {date?} {--event=created_at} {--periodType=daily} {--dateFormat=} {--M|models=} {--modelsPath=} {--scope=} {--scopeValue=} {--dry} ';
1919

2020
protected $description = 'Send to Eloquentize the counts of all models for a given date and event.';
2121

2222
protected $verbose = false;
2323

24+
protected $dry = false;
25+
2426
public function performModelCount(array $models, CarbonPeriod $period, string $event, ?string $modelsPath = null, ?string $scope = null, ?string $scopeValue = null)
2527
{
2628
$metrics = [];
@@ -72,6 +74,7 @@ public function handle()
7274
$scope = $this->option('scope');
7375
$scopeValue = $this->option('scopeValue');
7476
$filteredModels = $this->parseModelsOption($this->option('models'));
77+
$this->dry = $this->option('dry') ?? false;
7578

7679
if ($scope && ! $filteredModels) {
7780
$this->error('"scope" option requires "--models" option to be set. models provided should have a corresponding scope.');
@@ -98,7 +101,27 @@ public function handle()
98101
$metricsData = $this->prepareMetricsData($metrics, $period, $event);
99102

100103
$this->verbose('Sending models count data to eloquentize...'.config('eloquentize.api_url').'/api/metrics/models');
101-
$this->sendMetricsData($metricsData, env('ELOQUENTIZE_API_TOKEN'), $event);
104+
if ($this->dry) {
105+
$this->line('');
106+
$this->warn('Dry run enabled. Data NOT sent to eloquentize.');
107+
$this->line('');
108+
$this->line('----- Source data -----');
109+
$this->line('The data will be stored in source :');
110+
$this->info('***** '.$this->cleanAppUrl(env('APP_URL')) . '-' . env('APP_ENV').' *****');
111+
$this->line('Be sure to define a comprehensive source name by setting APP_URL ');
112+
$this->line('');
113+
$this->line('----- Models tracked -----');
114+
$this->info(implode(', ', $models));
115+
$this->line('');
116+
117+
// $this->line('Metrics data:');
118+
// $this->line(json_encode($metricsData, JSON_PRETTY_PRINT));
119+
120+
return 0;
121+
}else{
122+
$this->sendMetricsData($metricsData, env('ELOQUENTIZE_API_TOKEN'), $event);
123+
}
124+
102125

103126
$this->line('Models count data sent to eloquentize.');
104127

src/Commands/Traits/GatherModels.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function isModelValid(string $modelClass, string $column): bool
130130
{
131131

132132
if (! class_exists($modelClass)) {
133-
$this->defaultErrorMessage();
133+
//$this->defaultErrorMessage();
134134
$this->verbose("Model class $modelClass did not exists.", 'warn');
135135

136136
return false;
@@ -139,28 +139,28 @@ protected function isModelValid(string $modelClass, string $column): bool
139139
try {
140140
$instance = new $modelClass;
141141
} catch (\Throwable $e) {
142-
$this->defaultErrorMessage();
142+
//$this->defaultErrorMessage();
143143
$this->verbose("Model class $modelClass is not instanciable.", 'warn');
144144

145145
return false;
146146
}
147147

148148
if (! $instance instanceof \Illuminate\Database\Eloquent\Model) {
149-
$this->defaultErrorMessage();
149+
//$this->defaultErrorMessage();
150150
$this->verbose("Model class $modelClass is not an instance of \Illuminate\Database\Eloquent\Model.", 'warn');
151151

152152
return false;
153153
}
154154

155155
if (! $instance->usesTimestamps()) {
156-
$this->defaultErrorMessage();
156+
//$this->defaultErrorMessage();
157157
$this->verbose("Model class $modelClass does not use timestamps.", 'warn');
158158

159159
return false;
160160
}
161161

162162
if (! Schema::connection($instance->getConnectionName())->hasColumn($instance->getTable(), $column)) {
163-
$this->defaultErrorMessage();
163+
//$this->defaultErrorMessage();
164164
$this->verbose("Model class $modelClass does not have a column named ".$column, 'warn');
165165

166166
return false;

0 commit comments

Comments
 (0)