Issues (2963)

app/Http/Controllers/Table/OutagesController.php (4 issues)

1
<?php
2
/**
3
 * OutagesController.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 <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2020 Thomas Berberich
23
 * @author     Thomas Berberich <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Table;
27
28
use App\Models\DeviceOutage;
29
use Carbon\Carbon;
30
use LibreNMS\Config;
31
use LibreNMS\Util\Url;
32
33
class OutagesController extends TableController
34
{
35
    public function rules()
36
    {
37
        return [
38
            'device' => 'nullable|int',
39
            'to' => 'nullable|date',
40
            'from' => 'nullable|date',
41
        ];
42
    }
43
44
    protected function filterFields($request)
45
    {
46
        return [
47
            'device_id' => 'device',
48
        ];
49
    }
50
51
    protected function sortFields($request)
52
    {
53
        return ['going_down', 'up_again', 'device_id'];
54
    }
55
56
    /**
57
     * Defines the base query for this resource
58
     *
59
     * @param  \Illuminate\Http\Request  $request
60
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
61
     */
62
    public function baseQuery($request)
63
    {
64
        return DeviceOutage::hasAccess($request->user())
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Device...ion(...) { /* ... */ }) also could return the type App\Models\DeviceOutage which is incompatible with the documented return type Illuminate\Database\Eloq...\Database\Query\Builder.
Loading history...
65
            ->with('device')
66
            ->when($request->from, function ($query) use ($request) {
67
                $query->where('going_down', '>=', strtotime($request->from));
68
            })
69
            ->when($request->to, function ($query) use ($request) {
70
                $query->where('going_down', '<=', strtotime($request->to));
71
            });
72
    }
73
74
    /**
75
     * @param  DeviceOutage  $outage
76
     */
77
    public function formatItem($outage)
78
    {
79
        $start = $this->formatDatetime($outage->going_down);
0 ignored issues
show
Bug Best Practice introduced by
The property going_down does not exist on App\Models\DeviceOutage. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
        $end = $outage->up_again ? $this->formatDatetime($outage->up_again) : '-';
81
        $duration = ($outage->up_again ?: time()) - $outage->going_down;
82
83
        return [
84
            'status' => $this->statusLabel($outage),
85
            'going_down' => $start,
86
            'up_again' => $end,
87
            'device_id' => $outage->device ? Url::deviceLink($outage->device, $outage->device->shortDisplayName()) : null,
0 ignored issues
show
Bug Best Practice introduced by
The property device does not exist on App\Models\DeviceOutage. Since you implemented __get, consider adding a @property annotation.
Loading history...
88
            'duration' => $this->formatTime($duration),
89
        ];
90
    }
91
92
    private function formatTime($duration)
93
    {
94
        $day_seconds = 86400;
95
96
        $duration_days = (int) ($duration / $day_seconds);
97
        $duration_time = $duration % $day_seconds;
0 ignored issues
show
The assignment to $duration_time is dead and can be removed.
Loading history...
98
99
        $output = "<span style='display:inline;'>";
100
        if ($duration_days) {
101
            $output .= $duration_days . 'd ';
102
        }
103
        $output .= (new Carbon($duration))->format(Config::get('dateformat.time'));
104
        $output .= '</span>';
105
106
        return $output;
107
    }
108
109
    private function formatDatetime($timestamp)
110
    {
111
        if (! $timestamp) {
112
            $timestamp = 0;
113
        }
114
115
        $output = "<span style='display:inline;'>";
116
        $output .= (new Carbon($timestamp))->format(Config::get('dateformat.compact'));
117
        $output .= '</span>';
118
119
        return $output;
120
    }
121
122
    private function statusLabel($outage)
123
    {
124
        if (empty($outage->up_again)) {
125
            $label = 'label-danger';
126
        } else {
127
            $label = 'label-success';
128
        }
129
130
        $output = "<span class='alert-status " . $label . "'></span>";
131
132
        return $output;
133
    }
134
}
135