Allow setting of decimal places for metric points. Closes #823

This commit is contained in:
James Brooks 2015-08-13 22:58:15 +01:00
parent bef736e865
commit 68a41ae49f
5 changed files with 64 additions and 4 deletions

View File

@ -38,6 +38,7 @@ class Metric extends Model implements HasPresenter
'display_chart' => 1, 'display_chart' => 1,
'default_value' => 0, 'default_value' => 0,
'calc_type' => 0, 'calc_type' => 0,
'places' => 2,
]; ];
/** /**
@ -50,6 +51,7 @@ class Metric extends Model implements HasPresenter
'suffix' => 'required', 'suffix' => 'required',
'display_chart' => 'boolean', 'display_chart' => 'boolean',
'default_value' => 'numeric', 'default_value' => 'numeric',
'places' => 'numeric|min:0|max:4',
]; ];
/** /**
@ -57,7 +59,15 @@ class Metric extends Model implements HasPresenter
* *
* @var string[] * @var string[]
*/ */
protected $fillable = ['name', 'suffix', 'description', 'display_chart', 'default_value', 'calc_type']; protected $fillable = [
'name',
'suffix',
'description',
'display_chart',
'default_value',
'calc_type',
'places',
];
/** /**
* The relations to eager load on every query. * The relations to eager load on every query.
@ -92,11 +102,11 @@ class Metric extends Model implements HasPresenter
if (Config::get('database.default') === 'mysql') { if (Config::get('database.default') === 'mysql') {
if (!isset($this->calc_type) || $this->calc_type == self::CALC_SUM) { if (!isset($this->calc_type) || $this->calc_type == self::CALC_SUM) {
$value = (int) $this->points() $value = $this->points()
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval) ->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
->groupBy(DB::raw('HOUR(created_at)'))->sum('value'); ->groupBy(DB::raw('HOUR(created_at)'))->sum('value');
} elseif ($this->calc_type == self::CALC_AVG) { } elseif ($this->calc_type == self::CALC_AVG) {
$value = (int) $this->points() $value = $this->points()
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval) ->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
->groupBy(DB::raw('HOUR(created_at)'))->avg('value'); ->groupBy(DB::raw('HOUR(created_at)'))->avg('value');
} }
@ -125,7 +135,7 @@ class Metric extends Model implements HasPresenter
return $this->default_value; return $this->default_value;
} }
return $value; return round($value, $this->places);
} }
/** /**

View File

@ -0,0 +1,41 @@
<?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 AlterTableMetricsAddDecimalPlacesColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('metrics', function (Blueprint $table) {
$table->integer('places')->unsigned()->default(2)->after('display_chart');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('metrics', function (Blueprint $table) {
$table->dropColumn('places');
});
}
}

View File

@ -80,6 +80,7 @@ return [
'calc_type' => 'Calculation of metrics', 'calc_type' => 'Calculation of metrics',
'type_sum' => 'Sum', 'type_sum' => 'Sum',
'type_avg' => 'Average', 'type_avg' => 'Average',
'places' => 'Decimal Places',
'points' => [ 'points' => [
'value' => 'Value', 'value' => 'Value',

View File

@ -42,6 +42,10 @@
<label for="metric-default_value">{{ trans('forms.metrics.default-value') }}</label> <label for="metric-default_value">{{ trans('forms.metrics.default-value') }}</label>
<input type="number" class="form-control" name="metric[default_value]" id="metric-default_value" value="{{ Input::old('metric.default_value') }}"> <input type="number" class="form-control" name="metric[default_value]" id="metric-default_value" value="{{ Input::old('metric.default_value') }}">
</div> </div>
<div class="form-group">
<label for="metric-places">{{ trans('forms.metrics.places') }}</label>
<input type="number" min="0" max="4" class="form-control" name="metric[places]" id="metric-places" required value="{{ Input::old('metric.places') }}">
</div>
<div class="form-group"> <div class="form-group">
<label>{{ trans('forms.metrics.display-chart') }}</label> <label>{{ trans('forms.metrics.display-chart') }}</label>
<input type="hidden" value="0" name="metric[display_chart]"> <input type="hidden" value="0" name="metric[display_chart]">

View File

@ -42,6 +42,10 @@
<label for="metric-default_value">{{ trans('forms.metrics.default-value') }}</label> <label for="metric-default_value">{{ trans('forms.metrics.default-value') }}</label>
<input type="number" class="form-control" name="metric[default_value]" id="metric-default_value" value="{{ $metric->default_value }}"> <input type="number" class="form-control" name="metric[default_value]" id="metric-default_value" value="{{ $metric->default_value }}">
</div> </div>
<div class="form-group">
<label for="metric-places">{{ trans('forms.metrics.places') }}</label>
<input type="number" min="0" max="4" class="form-control" name="metric[places]" id="metric-places" required value="{{ $metric->places }}">
</div>
<div class="form-group"> <div class="form-group">
<label>{{ trans('forms.metrics.display-chart') }}</label> <label>{{ trans('forms.metrics.display-chart') }}</label>
<input type="hidden" value="0" name="metric[display_chart]"> <input type="hidden" value="0" name="metric[display_chart]">