Completed
Push — graph-cache ( 447735 )
by Tony
10:09
created

RRDXport   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A fetch() 0 11 3
A getOutput() 0 10 1
A jsonSerialize() 0 19 2
A csvSerialize() 0 16 2
1
<?php
2
/**
3
 * RRDXport.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Data;
27
28
use Cache;
29
use Carbon\Carbon;
30
31
class RRDXport extends RRD implements \JsonSerializable
32
{
33
    protected $supported_formats = ['json', 'csv'];
34
    private $headers;
35
    private $key;
36
37
    /**
38
     * RRDXport constructor.
39
     *
40
     * @param string $definition rrdtool definition
41
     * @param array $headers headers for rrdtool
42
     * @param string $start start time
43
     * @param string $end end time
44
     */
45
    public function __construct($definition, $headers, $start, $end)
46
    {
47
        $args = " xport --json -s $start -e $end $definition";
48
        parent::__construct($args);
49
50
        $this->headers = $headers;
51
        $this->key = $this->genKey('xport', $definition, implode(',', $headers), $this->roundTime($start), $this->roundTime($end));
52
    }
53
54
    /**
55
     * Return the output from this data source
56
     * May be base64 encoded PNG, Json Data, or CSV Data
57
     *
58
     * @param string $format png, json, or csv
59
     * @return string
60
     */
61
    public function fetch($format)
62
    {
63
        $this->checkFormatSupported($format);
64
65
        if ($format == 'json') {
66
            return $this->jsonSerialize();
67
        } elseif ($format == 'csv') {
68
            return $this->csvSerialize();
69
        }
70
        return null;  // shouldn't get here
71
    }
72
73
    /**
74
     * Get and cache the output of rrdtool as a json object
75
     *
76
     * @return mixed
77
     */
78
    private function getOutput()
79
    {
80
        return Cache::tags('graphs')->remember($this->key, 5, function () {
81
            $output = $this->run();
82
            $output = preg_replace('/\'/', '"', $output);
83
            $output = preg_replace('/about\:/', '"meta":', $output);
84
            $output = preg_replace('/meta\:/', '"meta":', $output);
85
            return json_decode($output);
86
        });
87
    }
88
89
    function jsonSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
    {
91
        $response = $this->getOutput();
92
        $step = $response->meta->step;
93
        $start = $response->meta->start;
94
//        $end = $response->meta->end;
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
        $cur_time = $start;
96
        $tmp_data = [];
97
98
        foreach ($response->data as $data) {
99
            $tmp_data[] = [$cur_time] + array_map('intval', $data);
100
            $cur_time += $step;
101
        }
102
103
        return json_encode([
104
            'data'   => $tmp_data,
105
            'labels' => $this->headers,
106
        ]);
107
    }
108
109
    function csvSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
    {
111
        $response = $this->getOutput();
112
        $step = $response->meta->step;
113
        $start = $response->meta->start;
114
//        $end = $response->meta->end;
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
        $cur_time = $start;
116
        $output = 'Date, '.implode(',', $this->headers).PHP_EOL;
117
118
        foreach ($response->data as $data) {
119
            $output .= Carbon::createFromTimestamp($cur_time).',';
120
            $output .= implode(',', array_map('intval', $data)).PHP_EOL;
121
            $cur_time += $step;
122
        }
123
        return $output;
124
    }
125
}
126