Merge pull request #2261 from CachetHQ/metrics-visibility

Added metric visibility options
This commit is contained in:
James Brooks 2016-12-04 16:55:12 +00:00 committed by GitHub
commit 7dadef99ab
14 changed files with 189 additions and 26 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -37,6 +37,7 @@ class AddMetricCommandHandler
'default_view' => $command->default_view,
'threshold' => $command->threshold,
'order' => $command->order,
'visible' => $command->visible,
]);
event(new MetricWasAddedEvent($metric));

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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])

View File

@ -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);
}
/**

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterTableMetricsAddVisibleColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('metrics', function (Blueprint $table) {
$table->boolean('visible')->after('order')->default(1);
$table->index('visible');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('metrics', function (Blueprint $table) {
$table->dropColumn('visible');
});
}
}

View File

@ -120,18 +120,22 @@ return [
// Metric form fields
'metrics' => [
'name' => 'Name',
'suffix' => 'Suffix',
'description' => 'Description',
'description-help' => 'You may also use Markdown.',
'display-chart' => 'Display chart on status page?',
'default-value' => 'Default value',
'calc_type' => 'Calculation of metrics',
'type_sum' => 'Sum',
'type_avg' => 'Average',
'places' => 'Decimal places',
'default_view' => 'Default view',
'threshold' => 'How many minutes of threshold between metric points?',
'name' => 'Name',
'suffix' => 'Suffix',
'description' => 'Description',
'description-help' => 'You may also use Markdown.',
'display-chart' => 'Display chart on status page?',
'default-value' => 'Default value',
'calc_type' => 'Calculation of metrics',
'type_sum' => 'Sum',
'type_avg' => 'Average',
'places' => 'Decimal places',
'default_view' => 'Default view',
'threshold' => 'How many minutes of threshold between metric points?',
'visibility' => 'Visibility',
'visibility_authenticated' => 'Visible to authenticated users',
'visibility_public' => 'Visible to everybody',
'visibility_hidden' => 'Always hidden',
'points' => [
'value' => 'Value',

View File

@ -66,6 +66,14 @@
{{ trans('forms.metrics.display-chart') }}
</label>
</div>
<div class="form-group">
<label>{{ trans('forms.metrics.visibility') }}</label>
<select name="visible" class="form-control" required>
<option value="0">{{ trans('forms.metrics.visibility_authenticated') }}</option>
<option value="1">{{ trans('forms.metrics.visibility_public') }}</option>
<option value="2">{{ trans('forms.metrics.visibility_hidden') }}</option>
</select>
</div>
</fieldset>
<div class="form-group">
<div class="btn-group">

View File

@ -66,6 +66,14 @@
{{ trans('forms.metrics.display-chart') }}
</label>
</div>
<div class="form-group">
<label>{{ trans('forms.metrics.visibility') }}</label>
<select name="visible" class="form-control" required>
<option value="0" {{ $metric->visible === 0 ? 'selected' : null }}>{{ trans('forms.metrics.visibility_authenticated') }}</option>
<option value="1" {{ $metric->visible === 1 ? 'selected' : null }}>{{ trans('forms.metrics.visibility_public') }}</option>
<option value="2" {{ $metric->visible === 2 ? 'selected' : null }}>{{ trans('forms.metrics.visibility_hidden') }}</option>
</select>
</div>
</fieldset>
<input type="hidden" name="id" value={{$metric->id}}>

View File

@ -39,6 +39,7 @@ class AddMetricCommandTest extends AbstractTestCase
'default_view' => 0,
'threshold' => 0,
'order' => 0,
'visible' => 1,
];
$object = new AddMetricCommand(
@ -51,7 +52,8 @@ class AddMetricCommandTest extends AbstractTestCase
$params['places'],
$params['default_view'],
$params['threshold'],
$params['order']
$params['order'],
$params['visible']
);
return compact('params', 'object');

View File

@ -41,6 +41,7 @@ class UpdateMetricCommandTest extends AbstractTestCase
'default_view' => 0,
'threshold' => 0,
'order' => 0,
'visible' => 1,
];
$object = new UpdateMetricCommand(
@ -54,7 +55,8 @@ class UpdateMetricCommandTest extends AbstractTestCase
$params['places'],
$params['default_view'],
$params['threshold'],
$params['order']
$params['order'],
$params['visible']
);
return compact('params', 'object');