Skip to content

Commit

Permalink
added traffic chart
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Feb 22, 2016
1 parent 12b0733 commit d088c8e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Server extends \hipanel\base\Model
const STATE_BLOCKED = 'blocked';
const STATE_DELETED = 'deleted';

/**
/**
* @return array the list of attributes for this record
*/
// public function attributes()
Expand Down Expand Up @@ -90,6 +90,7 @@ public function rules()
[['id', 'osimage'], 'required', 'on' => ['boot-live']],
[['type', 'comment'], 'required', 'on' => ['enable-block']],
[['comment'], 'safe', 'on' => ['disable-block']],
[['uses'], 'safe', 'on' => ['never']],
];
}

Expand Down
69 changes: 69 additions & 0 deletions src/views/server/_consumption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use dosamigos\chartjs\ChartJs;
use yii\helpers\Html;

$labels = [];
$sets = [];

$usesPerMonth = (array)$model->uses;

?>

<div class="row">
<div class="col-md-12">
<?php

if ($usesPerMonth === []) {
echo Yii::t('hipanel/server', 'Traffic consumption history is not available for this server');
} else {
ksort($usesPerMonth);

foreach ($usesPerMonth as $month => $uses) {
$labels[] = Yii::$app->formatter->asDate(strtotime($month), 'LLL y');

foreach ($uses as $use) {
if (in_array($use['type'], ['server_traf95', 'server_traf95_in', 'server_traf95_max'])) {
$value = $use['last'];
} else {
$value = $use['total'];
}

$data[$use['type']][] = $value;
}
}

echo Html::tag('div', ChartJs::widget([
'type' => 'Line',
'data' => [
'labels' => $labels,
'datasets' => [
[
'label' => 'out',
'fillColor' => "rgba(220,220,220,0.5)",
'strokeColor' => "rgba(220,220,220,1)",
'pointColor' => "rgba(220,220,220,1)",
'pointStrokeColor' => "#fff",
'data' => $data['server_traf']
],
[
'label' => 'out',
'fillColor' => "rgba(151,187,205,0.5)",
'strokeColor' => "rgba(151,187,205,1)",
'pointColor' => "rgba(151,187,205,1)",
'pointStrokeColor' => "#fff",
'data' => $data['server_traf_in']
]
]
],
'clientOptions' => [
'bezierCurve' => false,
'responsive' => true,
'maintainAspectRatio' => true,
]
]));
}

?>
</div>
</div>
4 changes: 2 additions & 2 deletions src/views/server/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@
</div>
</div>
<?php Pjax::begin(['enablePushState' => false]) ?>
<div class="col-md-4">
<div class="col-md-5">
<?php
$box = Box::begin(['renderBody' => false]);
$box->beginHeader();
echo $box->renderTitle(Yii::t('hipanel/server', 'Resources consumption'));
$box->endHeader();
$box->beginBody();

echo $this->render('_consumption', ['model' => $model]);
$box->endBody();
$box->end();
?>
Expand Down

0 comments on commit d088c8e

Please sign in to comment.