From bc5fcf87360a2c080b37c8fe602c190b15bd25b1 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 13 Jan 2017 03:19:39 -0800 Subject: [PATCH] Fixed dashboard chart --- .../Api/StatuslabelsController.php | 44 ++++++++++++++++ .../Controllers/StatuslabelsController.php | 37 ------------- resources/views/dashboard.blade.php | 33 +++++++++--- routes/api.php | 52 +++++++++++-------- 4 files changed, 99 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.php index 21e33fc424..6d81c60639 100644 --- a/app/Http/Controllers/Api/StatuslabelsController.php +++ b/app/Http/Controllers/Api/StatuslabelsController.php @@ -6,6 +6,7 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Helpers\Helper; use App\Models\Statuslabel; +use App\Models\Asset; class StatuslabelsController extends Controller { @@ -100,4 +101,47 @@ class StatuslabelsController extends Controller return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/statuslabels/message.delete.success'))); } + + + + /** + * Show a count of assets by status label for pie chart + * + * @author [A. Gianotto] [] + * @since [v3.0] + * @return \Illuminate\Http\Response + */ + + public function getAssetCountByStatuslabel() + { + + $statusLabels = Statuslabel::with('assets')->get(); + $labels=[]; + $points=[]; + $colors=[]; + foreach ($statusLabels as $statusLabel) { + if ($statusLabel->assets()->count() > 0) { + $labels[]=$statusLabel->name; + $points[]=$statusLabel->assets()->whereNull('assigned_to')->count(); + if ($statusLabel->color!='') { + $colors[]=$statusLabel->color; + } + } + } + $labels[]='Deployed'; + $points[]=Asset::whereNotNull('assigned_to')->count(); + + $colors_array = array_merge($colors, Helper::chartColors()); + + $result= [ + "labels" => $labels, + "datasets" => [ [ + "data" => $points, + "backgroundColor" => $colors_array, + "hoverBackgroundColor" => $colors_array + ]] + ]; + return $result; + } + } diff --git a/app/Http/Controllers/StatuslabelsController.php b/app/Http/Controllers/StatuslabelsController.php index c57caa67f8..da184f5750 100755 --- a/app/Http/Controllers/StatuslabelsController.php +++ b/app/Http/Controllers/StatuslabelsController.php @@ -37,43 +37,6 @@ class StatuslabelsController extends Controller } - /** - * Show a count of assets by status label - * - * @return array - */ - - public function getAssetCountByStatuslabel() - { - - $statusLabels = Statuslabel::with('assets')->get(); - $labels=[]; - $points=[]; - $colors=[]; - foreach ($statusLabels as $statusLabel) { - if ($statusLabel->assets()->count() > 0) { - $labels[]=$statusLabel->name; - $points[]=$statusLabel->assets()->whereNull('assigned_to')->count(); - if ($statusLabel->color!='') { - $colors[]=$statusLabel->color; - } - } - } - $labels[]='Deployed'; - $points[]=Asset::whereNotNull('assigned_to')->count(); - - $colors_array = array_merge($colors, Helper::chartColors()); - - $result= [ - "labels" => $labels, - "datasets" => [ [ - "data" => $points, - "backgroundColor" => $colors_array, - "hoverBackgroundColor" => $colors_array - ]] - ]; - return $result; - } /** diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 59fa4ea05a..c4c9a63e45 100755 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -147,15 +147,34 @@ var pieChart = new Chart(pieChartCanvas); var ctx = document.getElementById("statusPieChart"); - $.get('{{ route('api.statuslabels.assets') }}', function (data) { - var myPieChart = new Chart(ctx,{ - type: 'doughnut', - data: data, - options: pieOptions - }); - // document.getElementById('my-doughnut-legend').innerHTML = myPieChart.generateLegend(); + + $.ajax({ + type: 'GET', + url: '{{ route('api.statuslabels.assets.bytype') }}', + headers: { + "X-Requested-With": 'XMLHttpRequest', + "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content') + }, + + dataType: 'json', + success: function (data) { + var myPieChart = new Chart(ctx,{ + + type: 'doughnut', + data: data, + options: pieOptions + }); + }, + error: function (data) { + // AssetRequest Validator will flash all errors to session, this just refreshes to see them. + window.location.reload(true); + // console.log(JSON.stringify(data)); + // console.log('error submitting'); + } }); + + diff --git a/routes/api.php b/routes/api.php index 6ff965d3d7..82e5977abf 100644 --- a/routes/api.php +++ b/routes/api.php @@ -112,6 +112,31 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { ] ); + + /*---Status Label API---*/ + Route::group([ 'prefix' => 'statuslabels'], function () { + + + Route::get('{id}/deployable', function ($statuslabelId) { + + $statuslabel = Statuslabel::find($statuslabelId); + if (( $statuslabel->deployable == '1' ) && ( $statuslabel->pending != '1' ) + && ( $statuslabel->archived != '1' ) + ) { + return '1'; + } else { + return '0'; + } + + }); + + // Pie chart for dashboard + + Route::get('assets', [ 'as' => 'api.statuslabels.assets.bytype', 'uses' => 'StatuslabelsController@getAssetCountByStatuslabel' ]); + + }); + + Route::resource('statuslabels', 'StatuslabelsController', ['names' => [ @@ -124,6 +149,10 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { ] ); + + + + Route::resource('consumables', 'ConsumablesController', ['names' => [ @@ -188,30 +217,7 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { - /*---Status Label API---*/ - Route::group([ 'prefix' => 'statuslabels' ,'middleware' => ['authorize:admin']], function () { - Route::resource('statuslabels', 'StatuslabelsController', [ - 'parameters' => ['statuslabel' => 'statuslabel_id'] - ]); - - Route::get('{statuslabelId}/deployable', function ($statuslabelId) { - - $statuslabel = Statuslabel::find($statuslabelId); - if (( $statuslabel->deployable == '1' ) && ( $statuslabel->pending != '1' ) - && ( $statuslabel->archived != '1' ) - ) { - return '1'; - } else { - return '0'; - } - - }); - - Route::get('list', [ 'as' => 'api.statuslabels.list', 'uses' => 'StatuslabelsController@getDatatable' ]); - Route::get('assets', [ 'as' => 'api.statuslabels.assets', 'uses' => 'StatuslabelsController@getAssetCountByStatuslabel' ]); - - }); /*---Accessories API---*/ Route::group([ 'prefix' => 'accessories' ], function () {