Completed
Pull Request — develop (#184)
by Tony
23:35
created

Device_bits::buildRRDGraphParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 58
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 50
nc 4
nop 0
dl 0
loc 58
rs 9.0077
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * BaseGraph.php
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * @package    LibreNMS
19
 * @link       http://librenms.org
20
 * @copyright  2016 Neil Lathwood
21
 * @author     Neil Lathwood <[email protected]>
22
 */
23
24
namespace App\Graphs;
25
26
class Device_bits extends BaseGraph
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
27
{
28
29
    protected function buildRRDGraphParams()
30
    {
31
        if (isset($this->input->id) && is_numeric($this->input->id)) {
32
            $port_ids = explode(',', $this->input->id);
33
            $ports = $this->device->ports()->whereIn('port_id', $port_ids)->get();
34
        } else {
35
            $ports = $this->device->ports()->get();
36
        }
37
        $headers = [];
38
        $defs = " COMMENT:'              Now       Avg      Max' COMMENT:' \\n' ";
39
40
        foreach ($ports as $port) {
41
            $port_id = $port->port_id;
42
            $rrd_file = get_port_rrdfile_path($this->device, $port->port_id);
43
            $defs .= " DEF:outoctets$port_id=$rrd_file:OUTOCTETS:AVERAGE \
44
                DEF:inoctets$port_id=$rrd_file:INOCTETS:AVERAGE \
45
                DEF:outoctets_max$port_id=$rrd_file:OUTOCTETS:MAX \
46
                DEF:inoctets_max$port_id=$rrd_file:INOCTETS:MAX \
47
                CDEF:octets$port_id=inoctets$port_id,outoctets$port_id,+ \
48
                CDEF:doutoctets$port_id=outoctets$port_id,-1,* \
49
                CDEF:outbits$port_id=outoctets$port_id,8,* \
50
                CDEF:outbits_max$port_id=outoctets_max$port_id,8,* \
51
                CDEF:doutoctets_max$port_id=outoctets_max$port_id,-1,* \
52
                CDEF:doutbits$port_id=doutoctets$port_id,8,* \
53
                CDEF:doutbits_max$port_id=doutoctets_max$port_id,8,* \
54
                CDEF:inbits$port_id=inoctets$port_id,8,* \
55
                CDEF:inbits_max$port_id=inoctets_max$port_id,8,* \
56
                VDEF:totin$port_id=inoctets$port_id,TOTAL \
57
                VDEF:totout$port_id=outoctets$port_id,TOTAL \
58
                VDEF:tot$port_id=octets$port_id,TOTAL COMMENT:' \\n' \
59
                VDEF:95thin$port_id=inbits$port_id,95,PERCENT \
60
                VDEF:95thout$port_id=outbits$port_id,95,PERCENT \
61
                CDEF:d95thoutn$port_id=doutbits$port_id,-1,* \
62
                VDEF:d95thoutn95$port_id=d95thoutn$port_id,95,PERCENT \
63
                CDEF:d95thoutn95n$port_id=doutbits$port_id,doutbits$port_id,-,d95thoutn95$port_id,-1,*,+ \
64
                VDEF:d95thout$port_id=d95thoutn95n$port_id,FIRST  \
65
                AREA:inbits_max$port_id#D7FFC7: \
66
                AREA:inbits$port_id#90B040: \
67
                LINE:inbits$port_id#608720:'In " . str_pad($port['ifDescr'], 6) . "' \
68
                GPRINT:inbits$port_id:LAST:%6.2lf%s \
69
                GPRINT:inbits$port_id:AVERAGE:%6.2lf%s \
70
                GPRINT:inbits_max$port_id:MAX:%6.2lf%s \
71
                AREA:doutbits_max$port_id#E0E0FF: \
72
                AREA:doutbits$port_id#8080C0:  COMMENT:' \\n' \
73
                LINE:doutbits$port_id#606090:'Out ' \
74
                GPRINT:outbits$port_id:LAST:%6.2lf%s \
75
                GPRINT:outbits$port_id:AVERAGE:%6.2lf%s \
76
                GPRINT:outbits_max$port_id:MAX:%6.2lf%s COMMENT:'\n' \
77
                LINE1:95thin$port_id#aa0000 \
78
                LINE1:d95thout$port_id#aa0000";
79
        }
80
81
        return [
82
            'headers' => $headers,
83
            'defs' => $defs,
84
        ];
85
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function buildRRDXport()
92
    {
93
        if (isset($this->input->id) && is_numeric($this->input->id)) {
94
            $port_ids = explode(',', $this->input->id);
95
            $ports = $this->device->ports()->whereIn('port_id', $port_ids)->get();
96
        } else {
97
            $ports = $this->device->ports()->get();
98
        }
99
100
        $headers = [];
101
        $defs = '';
102
103
        foreach ($ports as $port) {
104
            $headers[] = 'In: ' . $port['ifDescr'];
105
            $headers[] = 'Out: ' . $port['ifDescr'];
106
            $port_id = $port->port_id;
107
            $rrd_file = get_port_rrdfile_path($this->device, $port->port_id);
108
            $defs .= "DEF:outoctets$port_id=$rrd_file:OUTOCTETS:AVERAGE \
109
                DEF:inoctets$port_id=$rrd_file:INOCTETS:AVERAGE \
110
                CDEF:doutoctets$port_id=outoctets$port_id,-1,* \
111
                CDEF:doutbits$port_id=doutoctets$port_id,8,* \
112
                CDEF:inbits$port_id=inoctets$port_id,8,* \
113
                XPORT:inbits$port_id:'In' \
114
                XPORT:doutbits$port_id:'Out' ";
115
        }
116
117
        return [
118
            'headers' => $headers,
119
            'defs' => $defs,
120
        ];
121
    }
122
}
123