Added metric visibility options. Closes #2244
This commit is contained in:
@@ -83,6 +83,13 @@ final class AddMetricCommand
|
||||
*/
|
||||
public $order;
|
||||
|
||||
/**
|
||||
* The visibility of the metric.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $visible;
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
*
|
||||
@@ -100,6 +107,7 @@ final class AddMetricCommand
|
||||
'default_view' => 'required|int|between:0,3',
|
||||
'threshold' => 'nullable|numeric|between:0,10',
|
||||
'order' => 'nullable|int',
|
||||
'visible' => 'required|int|between:0,2',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -115,10 +123,11 @@ final class AddMetricCommand
|
||||
* @param int $default_view
|
||||
* @param int $threshold
|
||||
* @param int $order
|
||||
* @param int $visible
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = 0)
|
||||
public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = 0, $visible = 1)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->suffix = $suffix;
|
||||
@@ -130,5 +139,6 @@ final class AddMetricCommand
|
||||
$this->default_view = $default_view;
|
||||
$this->threshold = $threshold;
|
||||
$this->order = $order;
|
||||
$this->visible = $visible;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,13 @@ final class UpdateMetricCommand
|
||||
*/
|
||||
public $order;
|
||||
|
||||
/**
|
||||
* The visibility of the metric.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $visible;
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
*
|
||||
@@ -109,6 +116,7 @@ final class UpdateMetricCommand
|
||||
'default_view' => 'nullable|numeric|between:0,4',
|
||||
'threshold' => 'nullable|numeric|between:0,10',
|
||||
'order' => 'nullable|int',
|
||||
'visible' => 'required|int|between:0,2',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -125,10 +133,11 @@ final class UpdateMetricCommand
|
||||
* @param int $default_view
|
||||
* @param int $threshold
|
||||
* @param int|null $order
|
||||
* @param int $visible
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = null)
|
||||
public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = null, $visible = null)
|
||||
{
|
||||
$this->metric = $metric;
|
||||
$this->name = $name;
|
||||
@@ -141,5 +150,6 @@ final class UpdateMetricCommand
|
||||
$this->default_view = $default_view;
|
||||
$this->threshold = $threshold;
|
||||
$this->order = $order;
|
||||
$this->visible = $visible;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ class AddMetricCommandHandler
|
||||
'default_view' => $command->default_view,
|
||||
'threshold' => $command->threshold,
|
||||
'order' => $command->order,
|
||||
'visible' => $command->visible,
|
||||
]);
|
||||
|
||||
event(new MetricWasAddedEvent($metric));
|
||||
|
||||
@@ -55,6 +55,7 @@ class UpdateMetricCommandHandler
|
||||
'default_view' => $command->default_view,
|
||||
'threshold' => $command->threshold,
|
||||
'order' => $command->order,
|
||||
'visible' => $command->visible,
|
||||
];
|
||||
|
||||
return array_filter($params, function ($val) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
namespace CachetHQ\Cachet\Composers\Modules;
|
||||
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
||||
@@ -30,16 +31,25 @@ class MetricsComposer
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* The user session object.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Auth\Guard
|
||||
*/
|
||||
protected $guard;
|
||||
|
||||
/**
|
||||
* Create a new metrics composer instance.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Config\Repository $config
|
||||
* @param \Illuminate\Contracts\Auth\Guard $guard
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Repository $config)
|
||||
public function __construct(Repository $config, Guard $guard)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->guard = $guard;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,12 +61,32 @@ class MetricsComposer
|
||||
*/
|
||||
public function compose(View $view)
|
||||
{
|
||||
$metrics = null;
|
||||
if ($displayMetrics = $this->config->get('setting.display_graphs')) {
|
||||
$metrics = Metric::displayable()->orderBy('order')->orderBy('id')->get();
|
||||
}
|
||||
$displayMetrics = $this->config->get('setting.display_graphs');
|
||||
$metrics = $this->getVisibleMetrics($displayMetrics);
|
||||
|
||||
$view->withDisplayMetrics($displayMetrics)
|
||||
->withMetrics($metrics);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get visible grouped components.
|
||||
*
|
||||
* @param bool $displayMetrics
|
||||
*
|
||||
* @return \Illuminate\Support\Collection|void
|
||||
*/
|
||||
protected function getVisibleMetrics($displayMetrics)
|
||||
{
|
||||
if (!$displayMetrics) {
|
||||
return;
|
||||
}
|
||||
|
||||
$metrics = Metric::displayable();
|
||||
|
||||
if (!$this->guard->check()) {
|
||||
$metrics->visible();
|
||||
}
|
||||
|
||||
return $metrics->orderBy('order')->orderBy('id')->get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,8 @@ class MetricController extends AbstractApiController
|
||||
Binput::get('places', 2),
|
||||
Binput::get('default_view', Binput::get('view', 1)),
|
||||
Binput::get('threshold', 5),
|
||||
Binput::get('order', 0)
|
||||
Binput::get('order', 0),
|
||||
Binput::get('visible', 1)
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
@@ -116,7 +117,8 @@ class MetricController extends AbstractApiController
|
||||
Binput::get('places'),
|
||||
Binput::get('default_view', Binput::get('view')),
|
||||
Binput::get('threshold'),
|
||||
Binput::get('order')
|
||||
Binput::get('order'),
|
||||
Binput::get('visible')
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
|
||||
@@ -79,7 +79,9 @@ class MetricController extends Controller
|
||||
$metricData['display_chart'],
|
||||
$metricData['places'],
|
||||
$metricData['default_view'],
|
||||
$metricData['threshold']
|
||||
$metricData['threshold'],
|
||||
0, // Default order
|
||||
$metricData['visible']
|
||||
));
|
||||
} catch (ValidationException $e) {
|
||||
return cachet_redirect('dashboard.metrics.create')
|
||||
@@ -152,7 +154,9 @@ class MetricController extends Controller
|
||||
Binput::get('display_chart', null, false),
|
||||
Binput::get('places', null, false),
|
||||
Binput::get('default_view', null, false),
|
||||
Binput::get('threshold', null, false)
|
||||
Binput::get('threshold', null, false),
|
||||
null,
|
||||
Binput::get('visible', null, false)
|
||||
));
|
||||
} catch (ValidationException $e) {
|
||||
return cachet_redirect('dashboard.metrics.edit', [$metric->id])
|
||||
|
||||
+39
-1
@@ -36,6 +36,27 @@ class Metric extends Model implements HasPresenter
|
||||
*/
|
||||
const CALC_AVG = 1;
|
||||
|
||||
/**
|
||||
* Viewable only authenticated users.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const VISIBLE_AUTHENTICATED = 0;
|
||||
|
||||
/**
|
||||
* Viewable by public.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const VISIBLE_GUEST = 1;
|
||||
|
||||
/**
|
||||
* Viewable by nobody.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const VISIBLE_HIDDEN = 2;
|
||||
|
||||
/**
|
||||
* The model's attributes.
|
||||
*
|
||||
@@ -50,6 +71,7 @@ class Metric extends Model implements HasPresenter
|
||||
'default_view' => 1,
|
||||
'threshold' => 5,
|
||||
'order' => 0,
|
||||
'visible' => 1,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -66,6 +88,7 @@ class Metric extends Model implements HasPresenter
|
||||
'default_view' => 'int',
|
||||
'threshold' => 'int',
|
||||
'order' => 'int',
|
||||
'visible' => 'int',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -84,6 +107,7 @@ class Metric extends Model implements HasPresenter
|
||||
'default_view',
|
||||
'threshold',
|
||||
'order',
|
||||
'visible',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -99,6 +123,7 @@ class Metric extends Model implements HasPresenter
|
||||
'places' => 'required|numeric|between:0,4',
|
||||
'default_view' => 'required|numeric|between:0,3',
|
||||
'threshold' => 'required|numeric|between:0,10',
|
||||
'visible' => 'required|numeric|between:0,2',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -113,6 +138,7 @@ class Metric extends Model implements HasPresenter
|
||||
'default_value',
|
||||
'calc_type',
|
||||
'order',
|
||||
'visible',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -134,7 +160,19 @@ class Metric extends Model implements HasPresenter
|
||||
*/
|
||||
public function scopeDisplayable(Builder $query)
|
||||
{
|
||||
return $query->where('display_chart', '=', true);
|
||||
return $query->where('display_chart', '=', true)->where('visible', '!=', self::VISIBLE_HIDDEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all metrics which are visible to public.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeVisible(Builder $query)
|
||||
{
|
||||
return $query->where('visible', '=', self::VISIBLE_GUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user