TrafficConsumption   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 56
c 3
b 0
f 0
dl 0
loc 138
ccs 0
cts 79
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A clientRegisterCss() 0 4 1
A renderBlockFooter() 0 3 1
A run() 0 14 2
A init() 0 25 3
A renderBlockHeader() 0 3 1
A renderCanvasData() 0 30 1
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\widgets;
12
13
use dosamigos\chartjs\ChartJs;
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\base\Widget;
17
use yii\helpers\Html;
18
19
class TrafficConsumption extends Widget
20
{
21
    /**
22
     * @var array Lable for each [[data]] item. Will be rendered on chart axis.
23
     * Example:
24
     *
25
     * ```php
26
     *  ['June 2017', 'July 2017']
27
     * ```
28
     */
29
    public $labels;
30
31
    /**
32
     * @var array Array of arrays of the traffic data. Each data point will be rendered on chart
33
     * Key - traffic type name, must be corresponding with [[consumptionBase]]
34
     * Value - array of traffic amount values for each period according to the [[lables]]
35
     *
36
     * Example:
37
     * ```php
38
     * [
39
     *    'server_traf_in' => [221.2, 312],
40
     *    'server_traf' => [1232, 3411]
41
     * ]
42
     * ```
43
     */
44
    public $data = [];
45
46
    /**
47
     * @var string
48
     */
49
    public $chartType = 'line';
50
51
    /**
52
     * @var string name of consumption property
53
     */
54
    public $consumptionBase = 'server_traf';
55
56
    /**
57
     * @var array Messages which would be shown if data is empty
58
     */
59
    protected $emptyMessage;
60
61
    /**
62
     * @var array Legends for datasets
63
     */
64
    protected $legends;
65
66
    public function init()
67
    {
68
        parent::init();
69
70
        if ($this->labels === null) {
71
            throw new InvalidConfigException('Please specify the "labels" property.');
72
        }
73
74
        if ($this->id === null) {
75
            $this->id = $this->consumptionBase . '_consumption_chart';
76
        }
77
78
        $this->emptyMessage = [
79
            'server_traf' => Yii::t('hipanel:server', 'Traffic consumption history is not available for this server'),
80
            'server_traf95' => Yii::t('hipanel:server', 'Bandwidth consumption history is not available for this server'),
81
        ];
82
83
        $this->legends = [
84
            'server_traf' => Yii::t('hipanel:server', 'Total outgoing traffic, Gb'),
85
            'server_traf_in' => Yii::t('hipanel:server', 'Total incoming traffic, Gb'),
86
            'server_traf95' => Yii::t('hipanel:server', '95th percentile for outgoing bandwidth, Mbit/s'),
87
            'server_traf95_in' => Yii::t('hipanel:server', '95th percentile for incoming bandwidth, Mbit/s'),
88
        ];
89
90
        $this->labels = array_values($this->labels);
91
    }
92
93
    public function run()
94
    {
95
        $html = '';
96
        $this->clientRegisterCss();
97
98
        $html .= $this->renderBlockHeader();
99
        if ($this->data === []) {
100
            $html .=  $this->emptyMessage[$this->consumptionBase];
101
        } else {
102
            $html .=  $this->renderCanvasData();
103
        }
104
        $html .=  $this->renderBlockFooter();
105
106
        return $html;
107
    }
108
109
    protected function renderBlockHeader()
110
    {
111
        return "<div class='row {$this->consumptionBase}-chart-wrapper'><div class='col-md-12'>";
112
    }
113
114
    protected function renderCanvasData()
115
    {
116
        return Html::tag('div', ChartJs::widget([
117
            'id' => $this->id,
118
            'type' => $this->chartType,
119
            'data' => [
120
                'labels' => $this->labels,
121
                'datasets' => [
122
                    [
123
                        'label' => $this->legends[$this->consumptionBase],
124
                        'backgroundColor' => 'rgba(139, 195, 74, 0.5)',
125
                        'borderColor' => 'rgba(139, 195, 74, 1)',
126
                        'pointBackgroundColor' => 'rgba(139, 195, 74, 1)',
127
                        'pointBorderColor' => '#fff',
128
                        'data' => (array) $this->data[$this->consumptionBase],
129
                    ],
130
                    [
131
                        'label' => $this->legends["{$this->consumptionBase}_in"],
132
                        'backgroundColor' => 'rgba(151,187,205,0.5)',
133
                        'borderColor' => 'rgba(151,187,205,1)',
134
                        'pointBackgroundColor' => 'rgba(151,187,205,1)',
135
                        'pointBorderColor' => '#fff',
136
                        'data' => (array) $this->data["{$this->consumptionBase}_in"],
137
                    ],
138
                ],
139
            ],
140
            'clientOptions' => [
141
                'bezierCurve' => false,
142
                'responsive' => true,
143
                'maintainAspectRatio' => true,
144
            ],
145
        ]));
146
    }
147
148
    protected function renderBlockFooter()
149
    {
150
        return '</div></div>';
151
    }
152
153
    protected function clientRegisterCss()
154
    {
155
        $view = $this->getView();
156
        $view->registerCss('
157
            ul.line-legend {
158
                list-style: none;
159
                margin: 0;
160
                padding: 0;
161
            }
162
        ');
163
    }
164
}
165