diff --git a/app/Models/Metric.php b/app/Models/Metric.php index a7565f60b..18aa4f205 100644 --- a/app/Models/Metric.php +++ b/app/Models/Metric.php @@ -38,6 +38,7 @@ class Metric extends Model implements HasPresenter 'display_chart' => 1, 'default_value' => 0, 'calc_type' => 0, + 'places' => 2, ]; /** @@ -50,6 +51,7 @@ class Metric extends Model implements HasPresenter 'suffix' => 'required', 'display_chart' => 'boolean', 'default_value' => 'numeric', + 'places' => 'numeric|min:0|max:4', ]; /** @@ -57,7 +59,15 @@ class Metric extends Model implements HasPresenter * * @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. @@ -92,11 +102,11 @@ class Metric extends Model implements HasPresenter if (Config::get('database.default') === 'mysql') { 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) ->groupBy(DB::raw('HOUR(created_at)'))->sum('value'); } elseif ($this->calc_type == self::CALC_AVG) { - $value = (int) $this->points() + $value = $this->points() ->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval) ->groupBy(DB::raw('HOUR(created_at)'))->avg('value'); } @@ -125,7 +135,7 @@ class Metric extends Model implements HasPresenter return $this->default_value; } - return $value; + return round($value, $this->places); } /** diff --git a/database/migrations/2015_08_13_214123_AlterTableMetricsAddDecimalPlacesColumn.php b/database/migrations/2015_08_13_214123_AlterTableMetricsAddDecimalPlacesColumn.php new file mode 100644 index 000000000..a30704391 --- /dev/null +++ b/database/migrations/2015_08_13_214123_AlterTableMetricsAddDecimalPlacesColumn.php @@ -0,0 +1,41 @@ +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'); + }); + } +} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index 33ea7c07b..9b304627e 100755 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -80,6 +80,7 @@ return [ 'calc_type' => 'Calculation of metrics', 'type_sum' => 'Sum', 'type_avg' => 'Average', + 'places' => 'Decimal Places', 'points' => [ 'value' => 'Value', diff --git a/resources/views/dashboard/metrics/add.blade.php b/resources/views/dashboard/metrics/add.blade.php index 655a1758d..7ff9d76ad 100644 --- a/resources/views/dashboard/metrics/add.blade.php +++ b/resources/views/dashboard/metrics/add.blade.php @@ -42,6 +42,10 @@ +
+ + +
diff --git a/resources/views/dashboard/metrics/edit.blade.php b/resources/views/dashboard/metrics/edit.blade.php index e695eef8f..7fd624b48 100644 --- a/resources/views/dashboard/metrics/edit.blade.php +++ b/resources/views/dashboard/metrics/edit.blade.php @@ -42,6 +42,10 @@
+
+ + +