Completed
Pull Request — develop (#184)
by Neil
25:03
created

Device_bits::buildRRDGraphParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 58
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 51
nc 4
nop 1
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
use Illuminate\Http\Request;
27
use App\Models\Device;
28
use Settings;
29
use App\Models\Port;
30
31
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...
32
{
33
34
    public function buildRRDGraphParams($input) {
35
        $device = $this->getDevice();
36
        if (isset($input->id) && is_numeric($input->id)) {
37
            $port_ids = explode(',', $input->id);
38
            $ports = $device->ports()->whereIn('port_id', $port_ids)->get();
0 ignored issues
show
Bug introduced by
The method ports() does not seem to exist on object<App\Graphs\Device_bits>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        } else {
40
            $ports = $device->ports()->get();
0 ignored issues
show
Bug introduced by
The method ports() does not seem to exist on object<App\Graphs\Device_bits>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
        }
42
        $headers = [];
43
        $defs = " COMMENT:'              Now       Avg      Max' COMMENT:' \\n' ";
44
45
        foreach ($ports as $port) {
46
            $port_id = $port->port_id;
47
            $rrd_file = get_port_rrdfile_path($device, $port->port_id);
48
            $defs .= " DEF:outoctets$port_id=$rrd_file:OUTOCTETS:AVERAGE \
49
                DEF:inoctets$port_id=$rrd_file:INOCTETS:AVERAGE \
50
                DEF:outoctets_max$port_id=$rrd_file:OUTOCTETS:MAX \
51
                DEF:inoctets_max$port_id=$rrd_file:INOCTETS:MAX \
52
                CDEF:octets$port_id=inoctets$port_id,outoctets$port_id,+ \
53
                CDEF:doutoctets$port_id=outoctets$port_id,-1,* \
54
                CDEF:outbits$port_id=outoctets$port_id,8,* \
55
                CDEF:outbits_max$port_id=outoctets_max$port_id,8,* \
56
                CDEF:doutoctets_max$port_id=outoctets_max$port_id,-1,* \
57
                CDEF:doutbits$port_id=doutoctets$port_id,8,* \
58
                CDEF:doutbits_max$port_id=doutoctets_max$port_id,8,* \
59
                CDEF:inbits$port_id=inoctets$port_id,8,* \
60
                CDEF:inbits_max$port_id=inoctets_max$port_id,8,* \
61
                VDEF:totin$port_id=inoctets$port_id,TOTAL \
62
                VDEF:totout$port_id=outoctets$port_id,TOTAL \
63
                VDEF:tot$port_id=octets$port_id,TOTAL COMMENT:' \\n' \
64
                VDEF:95thin$port_id=inbits$port_id,95,PERCENT \
65
                VDEF:95thout$port_id=outbits$port_id,95,PERCENT \
66
                CDEF:d95thoutn$port_id=doutbits$port_id,-1,* \
67
                VDEF:d95thoutn95$port_id=d95thoutn$port_id,95,PERCENT \
68
                CDEF:d95thoutn95n$port_id=doutbits$port_id,doutbits$port_id,-,d95thoutn95$port_id,-1,*,+ \
69
                VDEF:d95thout$port_id=d95thoutn95n$port_id,FIRST  \
70
                AREA:inbits_max$port_id#D7FFC7: \
71
                AREA:inbits$port_id#90B040: \
72
                LINE:inbits$port_id#608720:'In " . str_pad($port['ifDescr'], 6) . "' \
73
                GPRINT:inbits$port_id:LAST:%6.2lf%s \
74
                GPRINT:inbits$port_id:AVERAGE:%6.2lf%s \
75
                GPRINT:inbits_max$port_id:MAX:%6.2lf%s \
76
                AREA:doutbits_max$port_id#E0E0FF: \
77
                AREA:doutbits$port_id#8080C0:  COMMENT:' \\n' \
78
                LINE:doutbits$port_id#606090:'Out ' \
79
                GPRINT:outbits$port_id:LAST:%6.2lf%s \
80
                GPRINT:outbits$port_id:AVERAGE:%6.2lf%s \
81
                GPRINT:outbits_max$port_id:MAX:%6.2lf%s COMMENT:'\n' \
82
                LINE1:95thin$port_id#aa0000 \
83
                LINE1:d95thout$port_id#aa0000";
84
        }
85
86
        return [
87
            'headers' => $headers,
88
            'defs' => $defs,
89
        ];
90
91
    }
92
93
    /**
94
     * @param $input
95
     * @return array
96
     */
97
    public function buildRRDXport($input)
98
    {
99
        $device = $this->getDevice();
100
        if (isset($input->id) && is_numeric($input->id)) {
101
            $port_ids = explode(',', $input->id);
102
            $ports = $device->ports()->whereIn('port_id', $port_ids)->get();
0 ignored issues
show
Bug introduced by
The method ports() does not seem to exist on object<App\Graphs\Device_bits>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
        } else {
104
            $ports = $device->ports()->get();
0 ignored issues
show
Bug introduced by
The method ports() does not seem to exist on object<App\Graphs\Device_bits>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
        }
106
107
        $headers = [];
108
        $defs = '';
109
110
        foreach ($ports as $port) {
111
            $headers[] = 'In: ' . $port['ifDescr'];
112
            $headers[] = 'Out: ' . $port['ifDescr'];
113
            $port_id = $port->port_id;
114
            $rrd_file = get_port_rrdfile_path($device, $port->port_id);
115
            $defs .= "DEF:outoctets$port_id=$rrd_file:OUTOCTETS:AVERAGE \
116
                DEF:inoctets$port_id=$rrd_file:INOCTETS:AVERAGE \
117
                CDEF:doutoctets$port_id=outoctets$port_id,-1,* \
118
                CDEF:doutbits$port_id=doutoctets$port_id,8,* \
119
                CDEF:inbits$port_id=inoctets$port_id,8,* \
120
                XPORT:inbits$port_id:'In' \
121
                XPORT:doutbits$port_id:'Out' ";
122
        }
123
124
        return [
125
            'headers' => $headers,
126
            'defs' => $defs,
127
        ];
128
    }
129
}
130