-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathRunController.php
More file actions
338 lines (299 loc) · 11.3 KB
/
Copy pathRunController.php
File metadata and controls
338 lines (299 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace IXP\Http\Controllers\RipeAtlas;
/*
* Copyright (C) 2009 - 2021 Internet Neutral Exchange Association Company Limited By Guarantee.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
use DB, Former, Redirect, Route, Validator;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\{
RedirectResponse,
Request
};
use Illuminate\View\View;
use IXP\Jobs\RipeAtlas\{
CompleteRequests as CompleteRequestsJob,
CreateMeasurements as CreateMeasurementsJob,
RunMeasurements as RunMeasurementsJob
};
use IXP\Models\{
Aggregators\CustomerAggregator,
AtlasRun,
Router,
Vlan};
use IXP\Utils\Http\Controllers\Frontend\EloquentController as Eloquent2Frontend;
use IXP\Utils\View\Alert\{
Alert,
Container as AlertContainer
};
/**
* Run Controller
* @author Barry O'Donovan <barry@islandbridgenetworks.ie>
* @author Yann Robin <yann@islandbridgenetworks.ie>
* @category IXP
* @package IXP\Http\Controllers\RipeAtlas
* @copyright Copyright (C) 2009 - 2021 Internet Neutral Exchange Association Company Limited By Guarantee
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL V2.0
*/
class RunController extends Eloquent2Frontend
{
/**
* The object being created/edited
*
* @var AtlasRun
*/
protected $object = null;
/**
* The URL prefix to use.
*
* Automatically determined based on the controller name if not set.
*
* @var string
*/
protected static $route_prefix = "ripe-atlas/runs";
/**
* Do we disable to edit?
*
* @var boolean
*/
public static $disable_edit = true;
/**
* This function sets up the frontend controller
*
* @return void
*/
#[\Override]
public function feInit()
{
$this->feParams = (object)[
'model' => AtlasRun::class,
'pagetitle' => 'Ripe Atlas :: Runs',
'titleSingular' => 'Run',
'nameSingular' => 'atlas run',
'listOrderBy' => 'created_at',
'listOrderByDir' => 'DESC',
'disableEdit' => self::$disable_edit,
'viewFolderName' => 'ripe-atlas/run',
'listColumns' => [
'vlan_name' => [
'title' => 'Vlan',
'type' => self::$FE_COL_TYPES[ 'HAS_ONE' ],
'controller' => 'vlan',
'action' => 'view',
'idField' => 'vlan_id',
],
'protocol' => [
'title' => 'Protocol',
'type' => self::$FE_COL_TYPES[ 'CONST' ],
'const' => Router::$PROTOCOLS,
],
'nb_run' => 'Total',
'created_at' => 'Created At',
'scheduled_at' => 'Scheduled At',
'started_at' => 'Started At',
'completed_at' => 'Completed At',
],
];
// display the same information in the view as the list
$this->feParams->viewColumns = $this->feParams->listColumns;
}
/**
* Function which can be over-ridden to add additional routes
*
* If you don't want to use the defaults as well as some additionals, override
* `routes()` instead.
*
* @param string $route_prefix
*
* @return void
*/
#[\Override]
protected static function additionalRoutes( string $route_prefix ): void
{
Route::group( [ 'prefix' => $route_prefix ], function() use ( $route_prefix ) {
Route::get( 'add-step-2', [static::class, 'addStep2'] )->name( $route_prefix . '@add-step-2' );
Route::put( 'run/complete/{atlasrun}', [static::class, 'completeRun'] )->name( $route_prefix . '@complete-run' );
});
}
/**
* Provide array of rows for the list action and view action
*
* @param int|null $id The `id` of the row to load for `view` action`. `null` if `listAction`
*
* @return array
*/
#[\Override]
protected function listGetData( ?int $id = null ): array
{
$feParams = $this->feParams;
return AtlasRun::select( [
'atlas_runs.*',
'vlan.name as vlan_name',
DB::raw('COUNT( am.run_id ) as nb_am'),
DB::raw('COUNT( am.atlas_create ) as nb_am_created'),
DB::raw('COUNT( am.atlas_start ) as nb_am_started'),
DB::raw('COUNT( am.atlas_stop ) as nb_am_stopped')
])
->leftJoin( 'vlan', 'atlas_runs.vlan_id','vlan.id' )
->leftJoin( 'atlas_measurements AS am' , 'atlas_runs.id', 'am.run_id' )
->when( $id, function( Builder $q, $id ) {
return $q->where( 'atlas_runs.id', $id );
} )
->when( $feParams->listOrderBy , function( Builder $q, $orderby ) use ( $feParams ) {
return $q->orderBy( $orderby, $feParams->listOrderByDir ?? 'ASC');
})->groupBy( 'atlas_runs.id' )->get()->toArray();
}
/**
* Display the form to create an object
*
* @return (\Illuminate\Database\Eloquent\Collection|null|true)[]
*
* @psalm-return array{object: null, vlans: \Illuminate\Database\Eloquent\Collection<int, Vlan>, preAddForm: true}
*/
#[\Override]
protected function createPrepareForm(): array
{
Former::populate( [
'protocol' => request()->old( 'protocol', Router::PROTOCOL_IPV4 ),
'scheduled_at' => request()->old( 'scheduled_at', (string)AtlasRun::SCHEDULED_AT_NOW ),
'scheduled_date' => request()->old( 'scheduled_date' ),
'scheduled_time' => request()->old( 'scheduled_time' ),
] );
return [
'object' => null,
'vlans' => Vlan::publicOnly()->get(),
'preAddForm' => true
];
}
/**
* Second step to create an Atlas run
*
* @param Request $r
*
* @return View
*/
public function addStep2( Request $r ): View
{
$this->checkForm( $r );
$this->feParams->titleSingular = "Step 2";
$this->addEditSetup();
$this->data[ 'params' ][ 'isAdd' ] = true;
$this->data[ 'params' ][ 'preAddForm' ] = false;
$this->data[ 'params' ][ 'object' ] = null;
$this->data[ 'params' ][ 'custs' ] = CustomerAggregator::WithProbesForProtocol( $r->protocol, $r->vlan_id );
$this->data[ 'params' ][ 'vlans' ] = Vlan::publicOnly()->get();
$this->data[ 'params' ][ 'protocol' ] = $r->protocol;
$this->data[ 'params' ][ 'vlanid' ] = $r->vlan_id;
$this->data[ 'params' ][ 'scheduled_at' ] = $r->scheduled_at;
$this->data[ 'params' ][ 'scheduled_date' ] = $r->scheduled_date;
$this->data[ 'params' ][ 'scheduled_time' ] = $r->scheduled_time;
return $this->display( 'edit' );
}
/**
* Function to do the actual validation and storing of the submitted object.
*
* @param Request $r
*
* @return RedirectResponse|true
*/
#[\Override]
public function doStore( Request $r ): bool|RedirectResponse
{
if( !$r->selected_custs || !count( $r->selected_custs ) ) {
AlertContainer::push( "You need to select at least one " . config( "ixp_fe.lang.customer.one" ) . ".", Alert::DANGER );
return Redirect::back()->withInput();
}
$this->checkForm( $r );
$this->object = AtlasRun::create( [
'protocol' => $r->protocol,
'scheduled_at' => $r->scheduled_at === AtlasRun::SCHEDULED_AT_NOW ? now() : new Carbon( $r->scheduled_date . $r->scheduled_time ),
'vlan_id' => $r->vlan_id
] );
CreateMeasurementsJob::dispatchSync( $this->object, $r->selected_custs );
if( (int)$r->scheduled_at === AtlasRun::SCHEDULED_AT_NOW ) {
$this->object->atlasMeasurements()->each( function( $am ) {
RunMeasurementsJob::dispatchAfterResponse( $am );
} );
$this->object->update( [ 'started_at' => now() ] );
AlertContainer::push( 'The command atlas run is processing.', Alert::SUCCESS );
}
return true;
}
/**
* Run the Atlas complete job
*
* @param AtlasRun $atlasrun
*
* @return RedirectResponse
*/
public function completeRun( AtlasRun $atlasrun ): RedirectResponse
{
if( $atlasrun->completed_at ) {
AlertContainer::push( 'The command complete atlas run have already executed.', Alert::DANGER );
} elseif( CompleteRequestsJob::dispatchSync( $atlasrun ) ) {
AlertContainer::push( 'The command complete atlas run executed with success.', Alert::SUCCESS );
} else {
AlertContainer::push( 'The command complete atlas run cannot be executed, some atlas measurements are not ended.', Alert::DANGER );
}
return redirect( route( self::$route_prefix . "@list" ) );
}
/**
* Delete all atlas measurements and atlas result before deleting the atlas run
*
* @inheritdoc
*
* @return bool Return false to stop / cancel the deletion
*/
#[\Override]
protected function preDelete(): bool
{
if( !$this->object->completed_at ) {
AlertContainer::push( 'Impossible to delete, the atlas run must be completed or stopped to do this action.', Alert::DANGER );
return false;
}
// Delete All Atlas results and Atlas measurements before deleting the Atlas run
// Delete on cascade for the atlas result linked to the atlas measurements
$this->object->atlasMeasurements()->delete();
return true;
}
/**
* Check if the form is valid
*
* @param Request $r
*/
#[\Override]
public function checkForm( Request $r ): void
{
$rules = [
'vlan_id' => 'required|integer|exists:Vlan,id',
'protocol' => 'required|integer|in:' . implode( ',', array_keys( Router::$PROTOCOLS ) ),
'scheduled_at' => 'required|integer|in:' . implode( ',', array_keys( AtlasRun::$SCHEDULED_AT ) ),
];
if( $r->scheduled_at === AtlasRun::SCHEDULED_AT_DATETIME ) {
$validateScheduled =
[
'scheduled_date' => 'required|date',
'scheduled_time' => 'required|date_format:H:i',
];
$rules = array_merge( $rules, $validateScheduled );
}
$r->validate( $rules );
}
}