Skip to content

Commit

Permalink
Fixed #11521 - switch to using status meta from status label name
Browse files Browse the repository at this point in the history
Signed-off-by: snipe <[email protected]>
  • Loading branch information
snipe committed Sep 29, 2022
1 parent 3d48dd1 commit 0b2ce7b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 30 deletions.
65 changes: 39 additions & 26 deletions app/Http/Controllers/Api/StatuslabelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use App\Models\Asset;
use App\Models\Statuslabel;
use Illuminate\Http\Request;
use App\Http\Transformers\PieChartTransformer;
use Illuminate\Support\Arr;

class StatuslabelsController extends Controller
{
Expand Down Expand Up @@ -188,43 +190,54 @@ public function destroy($id)
*
* @author [A. Gianotto] [<[email protected]>]
* @since [v3.0]
* @return \Illuminate\Http\Response
* @return array
*/
public function getAssetCountByStatuslabel()
{
$this->authorize('view', Statuslabel::class);

$statuslabels = Statuslabel::withCount('assets')->get();

$labels = [];
$points = [];
$default_color_count = 0;
$colors_array = [];

foreach ($statuslabels as $statuslabel) {
if ($statuslabel->assets_count > 0) {
$labels[] = $statuslabel->name.' ('.number_format($statuslabel->assets_count).')';
$points[] = $statuslabel->assets_count;

if ($statuslabel->color != '') {
$colors_array[] = $statuslabel->color;
} else {
$colors_array[] = Helper::defaultChartColors($default_color_count);
}
$default_color_count++;

$total[$statuslabel->name]['label'] = $statuslabel->name;
$total[$statuslabel->name]['count'] = $statuslabel->assets_count;

if ($statuslabel->color != '') {
$total[$statuslabel->name]['color'] = $statuslabel->color;
}
}

$result = [
'labels' => $labels,
'datasets' => [[
'data' => $points,
'backgroundColor' => $colors_array,
'hoverBackgroundColor' => $colors_array,
]],
];
return (new PieChartTransformer())->transformPieChartDate($total);

}

/**
* Show a count of assets by meta status type for pie chart
*
* @author [A. Gianotto] [<[email protected]>]
* @since [v6.0.11]
* @return array
*/
public function getAssetCountByMetaStatus()
{
$this->authorize('view', Statuslabel::class);

$total['rtd']['label'] = trans('general.ready_to_deploy');
$total['rtd']['count'] = Asset::RTD()->count();

$total['deployed']['label'] = trans('general.deployed');
$total['deployed']['count'] = Asset::Deployed()->count();

$total['archived']['label'] = trans('general.archived');
$total['archived']['count'] = Asset::Archived()->count();

$total['pending']['label'] = trans('general.pending');
$total['pending']['count'] = Asset::Pending()->count();

$total['undeployable']['label'] = trans('general.undeployable');
$total['undeployable']['count'] = Asset::Undeployable()->count();

return $result;
return (new PieChartTransformer())->transformPieChartDate($total);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions app/Http/Transformers/PieChartTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Http\Transformers;


use App\Helpers\Helper;/**
* Class PieChartTransformer
*
* This handles the standardized formatting of the API response we need to provide for
* the pie charts
*
* @return \Illuminate\Http\Response
*@since [v6.0.11]
* @author [A. Gianotto] [<[email protected]>]
*/
class PieChartTransformer
{
public function transformPieChartDate($totals)
{

$labels = [];
$counts = [];
$default_color_count = 0;
$colors_array = [];

foreach ($totals as $total) {

if ($total['count'] > 0) {

$labels[] = $total['label'];
$counts[] = $total['count'];

if (isset($total['color'])) {
$colors_array[] = $total['color'];
} else {
$colors_array[] = Helper::defaultChartColors($default_color_count);
$default_color_count++;
}
}
}

$results = [
'labels' => $labels,
'datasets' => [[
'data' => $counts,
'backgroundColor' => $colors_array,
'hoverBackgroundColor' => $colors_array,
]],
];


return $results;
}
}
2 changes: 2 additions & 0 deletions resources/lang/en/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@
'bulk_checkin_success' => 'The items for the selected users have been checked in.',
'set_to_null' => 'Delete values for this asset|Delete values for all :asset_count assets ',
'na_no_purchase_date' => 'N/A - No purchase date provided',
'assets_by_status' => 'Assets by Status',
'assets_by_status_type' => 'Assets by Status Type',


];
6 changes: 3 additions & 3 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class="table table-striped snipe-table"
<div class="col-md-4">
<div class="box box-default">
<div class="box-header with-border">
<h2 class="box-title">{{ trans('general.assets') }} {{ trans('general.bystatus') }}</h2>
<h2 class="box-title">{{ trans('general.assets_by_status_type') }}</h2>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" aria-hidden="true">
<i class="fas fa-minus" aria-hidden="true"></i>
Expand All @@ -261,7 +261,7 @@ class="table table-striped snipe-table"
<div class="row">
<div class="col-md-12">
<div class="chart-responsive">
<canvas id="statusPieChart" height="290"></canvas>
<canvas id="statusPieChart" height="260"></canvas>
</div> <!-- ./chart-responsive -->
</div> <!-- /.col -->
</div> <!-- /.row -->
Expand Down Expand Up @@ -438,7 +438,7 @@ class="table table-striped snipe-table"
dataType: 'json',
success: function (data) {
var myPieChart = new Chart(ctx,{
type : 'doughnut',
type : 'pie',
data : data,
options: pieOptions
});
Expand Down
9 changes: 8 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,18 @@
]
)->name('api.statuslabels.selectlist');

Route::get('assets',
Route::get('assets/name',
[
Api\StatuslabelsController::class,
'getAssetCountByStatuslabel'
]
)->name('api.statuslabels.assets.byname');

Route::get('assets/type',
[
Api\StatuslabelsController::class,
'getAssetCountByMetaStatus'
]
)->name('api.statuslabels.assets.bytype');

Route::get('{id}/assetlist',
Expand Down

0 comments on commit 0b2ce7b

Please sign in to comment.