-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
695 lines (614 loc) · 26.4 KB
/
Copy pathindex.php
File metadata and controls
695 lines (614 loc) · 26.4 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Dashboard — main page for Integration Hub.
*
* @package local_integrationhub
* @copyright 2026 Integration Hub Contributors
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
use local_integrationhub\service\registry as service_registry;
use local_integrationhub\service\circuit_breaker;
require_login();
$context = context_system::instance();
require_capability('local/integrationhub:view', $context);
$canmanage = has_capability('local/integrationhub:manage', $context);
// Handle actions.
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
$serviceid = optional_param('serviceid', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_INT);
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/local/integrationhub/index.php'));
$PAGE->set_title(get_string('pluginname', 'local_integrationhub'));
$PAGE->set_heading(get_string('pluginname', 'local_integrationhub'));
$PAGE->set_pagelayout('admin');
// Process form submission.
if ($canmanage && $action === 'save' && confirm_sesskey()) {
$data = new stdClass();
$data->name = required_param('name', PARAM_TEXT);
$data->type = optional_param('type', 'rest', PARAM_ALPHA);
$data->base_url = required_param('base_url', PARAM_TEXT);
$data->auth_type = required_param('auth_type', PARAM_ALPHA);
$data->auth_token = optional_param('auth_token', '', PARAM_TEXT);
$data->timeout = optional_param('timeout', 5, PARAM_INT);
$data->max_retries = optional_param('max_retries', 3, PARAM_INT);
$data->retry_backoff = optional_param('retry_backoff', 1, PARAM_INT);
$data->cb_failure_threshold = optional_param('cb_failure_threshold', 5, PARAM_INT);
$data->cb_cooldown = optional_param('cb_cooldown', 30, PARAM_INT);
$data->response_queue = optional_param('response_queue', '', PARAM_ALPHANUMEXT);
// Server-side URL reconstruction for AMQP.
// This ensures that even if JS fails to update the hidden base_url field.
// We construct the correct URL from the individual fields.
if ($data->type === 'amqp') {
$amqphost = optional_param('amqp_host', 'localhost', PARAM_HOST);
$amqpport = optional_param('amqp_port', 5672, PARAM_INT);
$amqpuser = optional_param('amqp_user', 'guest', PARAM_TEXT);
$amqppass = optional_param('amqp_pass', 'guest', PARAM_TEXT);
$amqpvhost = optional_param('amqp_vhost', '/', PARAM_TEXT); // Allow slash.
$amqpexchange = optional_param('ih-amqp_exchange', '', PARAM_TEXT); // Note ID prefix in form.
}
if ($serviceid > 0) {
service_registry::update_service($serviceid, $data);
\core\notification::success(get_string('serviceupdated', 'local_integrationhub', $data->name));
} else {
service_registry::create_service($data);
\core\notification::success(get_string('servicecreated', 'local_integrationhub', $data->name));
}
redirect($PAGE->url);
}
// Process delete.
if ($canmanage && $action === 'delete' && $serviceid > 0 && confirm_sesskey()) {
if ($confirm) {
$service = service_registry::get_service_by_id($serviceid);
service_registry::delete_service($serviceid);
\core\notification::success(get_string('servicedeleted', 'local_integrationhub', $service->name));
redirect($PAGE->url);
}
}
// Process circuit reset.
if ($canmanage && $action === 'resetcircuit' && $serviceid > 0 && confirm_sesskey()) {
$service = service_registry::get_service_by_id($serviceid);
$cb = circuit_breaker::from_service($service);
$cb->reset();
\core\notification::success(get_string('circuitreset', 'local_integrationhub', $service->name));
redirect($PAGE->url);
}
// Process reset all circuits.
if ($canmanage && $action === 'resetall' && confirm_sesskey()) {
$services = service_registry::get_all_services();
foreach ($services as $svc) {
$cb = circuit_breaker::from_service($svc);
$cb->reset();
}
\core\notification::success(get_string('allcircuitsreset', 'local_integrationhub'));
redirect($PAGE->url);
}
// Load data.
$services = service_registry::get_all_services();
// Calculate stats for each service.
foreach ($services as $service) {
$cb = circuit_breaker::from_service($service);
$service->circuit_state = $cb->get_state_label();
$cbstate = $cb->get_state();
$service->circuit_state_raw = strtolower($cbstate->state);
// If OPEN, append retry info.
if ($service->circuit_state_raw === 'open') {
$retrytime = $cbstate->timemodified + $service->cb_cooldown;
$remaining = $retrytime - time();
if ($remaining > 0) {
$service->circuit_state .= ' <br><small class="text-white-50">Retry in ' . $remaining . 's</small>';
}
}
// Get average latency from last 50 requests.
$service->avg_latency = $DB->get_field_sql(
"SELECT AVG(latency_ms) FROM (
SELECT latency_ms FROM {local_integrationhub_log}
WHERE serviceid = ? AND success = 1
ORDER BY timecreated DESC
LIMIT 50
) last50",
[$service->id]
);
// Get recent error count (last 24h).
$since = time() - 86400;
$service->recent_errors = $DB->count_records_select(
'local_integrationhub_log',
'serviceid = ? AND success = 0 AND timecreated > ?',
[$service->id, $since]
);
}
// Prepare edit data if editing.
$editservice = null;
if ($canmanage && $action === 'edit' && $serviceid > 0) {
$editservice = service_registry::get_service_by_id($serviceid);
}
// Charts data preparation.
// 1. Status Distribution (Global).
$statusstats = $DB->get_records_sql(
"SELECT success, COUNT(*) as count
FROM {local_integrationhub_log}
GROUP BY success"
);
$successcount = 0;
$failcount = 0;
foreach ($statusstats as $stat) {
if ($stat->success == 1) {
$successcount = (int)$stat->count;
} else {
$failcount = (int)$stat->count;
}
}
// 2. Latency Trend (Last 200 requests).
$logs = $DB->get_records('local_integrationhub_log', [], 'timecreated DESC', '*', 0, 200);
$chartlabels = [];
$chartdata = [];
// Process in reverse to get chronological order.
foreach (array_reverse($logs) as $log) {
if ($log->latency_ms === null) {
continue;
}
$chartlabels[] = date('H:i', $log->timecreated);
$chartdata[] = (int)$log->latency_ms;
}
// Prepare data for JS.
$chartdatajs = [
'success' => $successcount,
'fail' => $failcount,
'labels' => $chartlabels,
'latency' => $chartdata,
];
// Load Chart.js before the header so it is included while <head> is still being built.
// The AMD module (dashboard.js) guards its Chart usage with `typeof Chart !== 'undefined'`.
$PAGE->requires->js(new moodle_url('/local/integrationhub/assets/min/chart.umd.min.js'), true);
// Output.
echo $OUTPUT->header();
// Tabs navigation.
echo $OUTPUT->render_from_template('local_integrationhub/dashboard_tabs', [
'index_url' => (new moodle_url('/local/integrationhub/index.php'))->out(false),
'rules_url' => (new moodle_url('/local/integrationhub/rules.php'))->out(false),
'queue_url' => (new moodle_url('/local/integrationhub/queue.php'))->out(false),
'events_url' => (new moodle_url('/local/integrationhub/events.php'))->out(false),
'services_str' => get_string('services', 'local_integrationhub'),
'rules_str' => get_string('rules', 'local_integrationhub'),
'queue_str' => get_string('queue', 'local_integrationhub'),
'sent_events_str' => get_string('sent_events', 'local_integrationhub'),
]);
// Charts section — rendered via Mustache template (Output API).
echo $OUTPUT->render_from_template('local_integrationhub/dashboard_charts', [
'integrationstatus_str' => get_string('integrationstatus', 'local_integrationhub'),
'latencytrend_str' => get_string('latencytrend', 'local_integrationhub'),
]);
// Action buttons.
echo html_writer::start_div('row mb-4');
echo html_writer::start_div('col-12 d-flex', ['style' => 'gap: 12px;']);
if ($canmanage) {
echo html_writer::tag('button', get_string('addservice', 'local_integrationhub'), [
'class' => 'btn btn-primary',
'id' => 'ih-btn-add',
'type' => 'button',
]);
$resetallurl = new moodle_url($PAGE->url, ['action' => 'resetall', 'sesskey' => sesskey()]);
echo html_writer::link(
$resetallurl,
'<i class="fa fa-refresh"></i> ' . get_string('resetallcircuits', 'local_integrationhub'),
['class' => 'btn btn-outline-warning']
);
}
echo html_writer::link(
new moodle_url('/local/integrationhub/logs.php'),
'<i class="fa fa-list-alt"></i> ' . get_string('viewlogs', 'local_integrationhub'),
['class' => 'btn btn-outline-secondary']
);
echo html_writer::end_div(); // Close col-12.
echo html_writer::end_div(); // Close row.
// Force a visual break between buttons and the form/table.
// Removed aggressive spacer.
// Inline form (hidden by default, shown via JS or when editing).
$showform = ($canmanage && ($action === 'add' || $editservice));
$formclass = $showform ? '' : 'd-none';
echo html_writer::start_div("card mb-4 {$formclass}", ['id' => 'ih-service-form']);
echo html_writer::start_div('card-header');
echo html_writer::tag('h5', $editservice
? get_string('editservice', 'local_integrationhub')
: get_string('addservice', 'local_integrationhub'));
echo html_writer::end_div();
echo html_writer::start_div('card-body');
echo html_writer::start_tag('form', [
'method' => 'post',
'action' => $PAGE->url->out(false),
'id' => 'ih-form',
]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'action', 'value' => 'save']);
echo html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => 'serviceid',
'value' => $editservice ? $editservice->id : 0,
'id' => 'ih-serviceid',
]);
// Form fields.
$fields = [
['name', 'servicename', 'text', $editservice->name ?? '', true],
['base_url', 'baseurl', 'text', $editservice->base_url ?? '', true],
['auth_token', 'authtoken', 'password', $editservice->auth_token ?? '', false],
['response_queue', 'response_queue', 'text', $editservice->response_queue ?? '', false],
['timeout', 'timeout', 'number', $editservice->timeout ?? 5, false],
['max_retries', 'maxretries', 'number', $editservice->max_retries ?? 3, false],
['retry_backoff', 'retrybackoff', 'number', $editservice->retry_backoff ?? 1, false],
['cb_failure_threshold', 'cbfailurethreshold', 'number', $editservice->cb_failure_threshold ?? 5, false],
['cb_cooldown', 'cbcooldown', 'number', $editservice->cb_cooldown ?? 30, false],
];
echo '<div class="row">';
// Service type select.
echo '<div class="col-md-6 mb-3">';
echo html_writer::tag('label', get_string('col_type', 'local_integrationhub'), [
'for' => 'ih-type', 'class' => 'form-label', 'style' => 'display:block; margin-bottom:6px;',
]);
echo html_writer::start_tag('select', [
'name' => 'type', 'id' => 'ih-type', 'class' => 'form-select',
]);
$selectedrest = (($editservice->type ?? 'rest') === 'rest') ? 'selected' : '';
$selectedamqp = (($editservice->type ?? '') === 'amqp') ? 'selected' : '';
$selectedsoap = (($editservice->type ?? '') === 'soap') ? 'selected' : '';
echo "<option value='rest' {$selectedrest}>REST</option>";
echo "<option value='amqp' {$selectedamqp}>AMQP (RabbitMQ)</option>";
echo "<option value='soap' {$selectedsoap}>SOAP</option>";
echo html_writer::end_tag('select');
echo '</div>';
// Auth type select.
echo '<div class="col-md-6 mb-3">';
echo html_writer::tag('label', get_string('authtype', 'local_integrationhub'), [
'for' => 'ih-auth_type', 'class' => 'form-label', 'style' => 'display:block; margin-bottom:6px;',
]);
echo html_writer::start_tag('select', [
'name' => 'auth_type', 'id' => 'ih-auth_type', 'class' => 'form-select',
]);
$selectedbearer = (($editservice->auth_type ?? 'bearer') === 'bearer') ? 'selected' : '';
$selectedapikey = (($editservice->auth_type ?? '') === 'apikey') ? 'selected' : '';
echo "<option value='bearer' {$selectedbearer}>" .
get_string('authtype_bearer', 'local_integrationhub') . "</option>";
echo "<option value='apikey' {$selectedapikey}>" .
get_string('authtype_apikey', 'local_integrationhub') . "</option>";
echo html_writer::end_tag('select');
echo '</div>';
// Type-toggle UI logic is handled by the AMD module local_integrationhub/dashboard
// (updateUiForType / syncAmqpUrl). No inline <script> needed here.
// AMQP Connection Builder (Conditional Visibility via JS).
echo '<div id="ih-amqp-builder" class="col-12 d-none mb-3">';
echo '<div class="card bg-light border-info"><div class="card-body">';
echo '<h6 class="card-title text-info"><i class="fa fa-magic"></i> ' .
get_string('amqp_builder', 'local_integrationhub') . '</h6>';
echo '<div class="row">';
// Parse existing URL if editing AMQP.
$amqpparts = [
'host' => 'localhost', 'port' => 5672, 'user' => 'guest', 'pass' => 'guest', 'vhost' => '/',
'exchange' => '', 'routing_key' => '', 'queue_declare' => '', 'dlq' => '',
];
if (!empty($editservice) && ($editservice->type === 'amqp')) {
$parsed = parse_url($editservice->base_url);
$amqpparts['host'] = $parsed['host'] ?? 'localhost';
$amqpparts['port'] = $parsed['port'] ?? 5672;
$amqpparts['user'] = $parsed['user'] ?? 'guest';
$amqpparts['pass'] = $parsed['pass'] ?? 'guest';
// Decoded vhost for display.
$path = isset($parsed['path']) ? $parsed['path'] : '/';
if ($path !== '/' && strpos($path, '/') === 0) {
$path = substr($path, 1);
}
$amqpparts['vhost'] = urldecode($path);
if ($amqpparts['vhost'] === '') {
$amqpparts['vhost'] = '/';
}
if (isset($parsed['query'])) {
parse_str($parsed['query'], $query);
$amqpparts['exchange'] = $query['exchange'] ?? '';
$amqpparts['routing_key'] = $query['routing_key'] ?? '';
$amqpparts['queue_declare'] = $query['queue_declare'] ?? '';
$amqpparts['dlq'] = $query['dlq'] ?? '';
}
}
// Core Connection.
$amqpfields = [
['amqp_host', 'amqp_host', 'text', $amqpparts['host'], 'localhost', 'col-md-3'],
['amqp_port', 'amqp_port', 'number', $amqpparts['port'], '5672', 'col-md-2'],
['amqp_user', 'amqp_user', 'text', $amqpparts['user'], 'guest', 'col-md-2'],
// Password might be masked in future.
['amqp_pass', 'amqp_pass', 'password', $amqpparts['pass'], '', 'col-md-2'],
['amqp_vhost', 'amqp_vhost', 'text', $amqpparts['vhost'], '/', 'col-md-3'],
];
foreach ($amqpfields as $af) {
// Array unpack: id, langstr, type, val, placeholder, colclass.
$col = $af[5] ?? 'col-md-2';
echo '<div class="' . $col . ' mb-2">';
echo html_writer::tag('label', get_string($af[1], 'local_integrationhub'), [
'class' => 'small', 'style' => 'display:block; margin-bottom:2px;',
]);
// Add name attribute so PHP receives it.
echo html_writer::empty_tag('input', [
'type' => $af[2],
'name' => $af[0],
'id' => "ih-{$af[0]}",
'value' => $af[3],
'class' => 'form-control form-control-sm ih-amqp-sync',
'placeholder' => $af[4],
]);
echo '</div>';
}
echo '</div>'; // Row (Connection).
// Advanced: Queue & DLQ.
echo '<div class="row mt-2 border-top pt-2">';
// Exchange.
echo '<div class="col-md-3">';
echo html_writer::tag('label', get_string('amqp_exchange', 'local_integrationhub'), ['class' => 'small fw-bold']);
echo html_writer::empty_tag('input', [
'type' => 'text',
'name' => 'ih-amqp_exchange', // Manual name.
'id' => 'ih-amqp_exchange',
'class' => 'form-control form-control-sm ih-amqp-sync',
'placeholder' => '(Default)',
'value' => $amqpparts['exchange'],
]);
echo '</div>';
// Routing Key.
echo '<div class="col-md-3">';
echo html_writer::tag('label', get_string('amqp_routing_key_default', 'local_integrationhub'), ['class' => 'small fw-bold']);
echo html_writer::tag('i', '', [
'class' => 'fa fa-question-circle text-muted ms-1',
'title' => get_string('amqp_routing_key_help', 'local_integrationhub'),
'data-toggle' => 'tooltip',
]);
echo html_writer::empty_tag('input', [
'type' => 'text',
'name' => 'ih-amqp_routing_key', // Manual name.
'id' => 'ih-amqp_routing_key',
'class' => 'form-control form-control-sm ih-amqp-sync',
'placeholder' => 'my.routing.key',
'value' => $amqpparts['routing_key'],
]);
echo '</div>';
// Queue Declare.
echo '<div class="col-md-3">';
echo html_writer::tag('label', get_string('amqp_queue_declare', 'local_integrationhub'), ['class' => 'small fw-bold']);
echo html_writer::tag('i', '', [
'class' => 'fa fa-question-circle text-muted ms-1',
'title' => get_string('amqp_queue_help', 'local_integrationhub'),
'data-toggle' => 'tooltip',
]);
echo html_writer::empty_tag('input', [
'type' => 'text',
'name' => 'ih-amqp_queue_declare', // Manual name.
'id' => 'ih-amqp_queue_declare',
'class' => 'form-control form-control-sm ih-amqp-sync',
'placeholder' => 'my_queue',
'value' => $amqpparts['queue_declare'],
]);
echo '</div>';
// DLQ.
echo '<div class="col-md-3">';
echo html_writer::tag('label', get_string('amqp_dlq', 'local_integrationhub'), ['class' => 'small fw-bold']);
echo html_writer::empty_tag('input', [
'type' => 'text',
'name' => 'ih-amqp_dlq', // Manual name.
'id' => 'ih-amqp_dlq',
'class' => 'form-control form-control-sm ih-amqp-sync',
'placeholder' => 'my_dlq',
'value' => $amqpparts['dlq'],
]);
echo '</div>';
echo '</div>'; // Row (Queues).
echo '</div></div>';
echo '</div>';
foreach ($fields as $field) {
[$fname, $stringkey, $type, $value, $required] = $field;
$divclass = 'col-md-6 mb-3';
// Hide base_url container if AMQP (handled by JS via ID).
if ($fname === 'base_url') {
$divclass .= ' ih-base-url-container'; // Marker class.
}
echo '<div class="' . $divclass . '">';
echo html_writer::tag('label', get_string($stringkey, 'local_integrationhub'), [
'for' => "ih-{$fname}", 'class' => 'form-label', 'style' => 'display:block; margin-bottom:6px;',
]);
$attrs = [
'type' => $type,
'name' => $fname,
'id' => "ih-{$fname}",
'value' => $value,
'class' => 'form-control',
];
if ($required) {
$attrs['required'] = 'required';
}
if ($type === 'number') {
$attrs['min'] = '0';
}
echo html_writer::empty_tag('input', $attrs);
if ($fname === 'base_url') {
echo html_writer::tag('div', get_string('base_url_help', 'local_integrationhub'), [
'class' => 'form-text text-muted', 'id' => 'ih-base_url-help',
]);
}
if ($fname === 'response_queue') {
echo html_writer::tag('div', get_string('response_queue_help', 'local_integrationhub'), [
'class' => 'form-text text-muted',
]);
}
echo '</div>';
}
echo '</div>'; // Row.
// Form buttons.
echo html_writer::start_div('d-flex');
// Added me-2 for spacing.
echo html_writer::tag('button', get_string('saveservice', 'local_integrationhub'), [
'type' => 'submit', 'class' => 'btn btn-success me-2',
]);
echo html_writer::tag('button', get_string('cancel', 'local_integrationhub'), [
'type' => 'button', 'class' => 'btn btn-secondary', 'id' => 'ih-btn-cancel',
]);
echo html_writer::end_div();
echo html_writer::end_tag('form');
echo html_writer::end_div(); // Card-body.
echo html_writer::end_div(); // Card #ih-service-form.
// Another spacer if form is hidden/shown, just to be safe.
// Spacer removed.
// Services table.
echo html_writer::tag('h4', get_string('services', 'local_integrationhub'), [
'class' => 'mb-3', 'style' => 'clear: both;',
]);
if (empty($services)) {
echo html_writer::div(get_string('noservices', 'local_integrationhub'), 'alert alert-info');
} else {
echo html_writer::start_tag('div', ['class' => 'table-responsive']);
// Force text-dark to avoid theme white-text issues.
echo html_writer::start_tag('table', [
'class' => 'table table-striped table-hover',
'id' => 'ih-services-table',
'style' => 'color: #212529 !important;',
]);
// Header.
echo '<thead class="table-dark"><tr>';
$headers = [
'col_name', 'col_type', 'col_baseurl', 'col_authtype',
'col_circuit', 'col_latency', 'col_errors', 'col_enabled',
];
if ($canmanage) {
$headers[] = 'col_actions';
}
foreach ($headers as $h) {
echo html_writer::tag('th', get_string($h, 'local_integrationhub'));
}
echo '</tr></thead>';
// Body.
echo '<tbody>';
foreach ($services as $svc) {
echo '<tr>';
echo html_writer::tag('td', s($svc->name));
$typelabel = strtoupper($svc->type ?? 'rest');
$typeclass = 'badge bg-secondary';
if (($svc->type ?? 'rest') === 'amqp') {
$typeclass = 'badge bg-info text-dark';
} else if (($svc->type ?? 'rest') === 'soap') {
$typeclass = 'badge bg-warning text-dark';
}
echo html_writer::tag('td', html_writer::tag('span', $typelabel, ['class' => $typeclass]));
echo html_writer::tag('td', html_writer::tag('code', s($svc->base_url)));
// Auth type badge.
$authbadge = ($svc->auth_type === 'bearer') ? 'badge bg-primary' : 'badge bg-info';
echo html_writer::tag('td', html_writer::tag('span', strtoupper(s($svc->auth_type)), ['class' => $authbadge]));
// Circuit state badge.
$cbclass = 'badge bg-success';
if ($svc->circuit_state_raw === 'open') {
$cbclass = 'badge bg-danger';
} else if ($svc->circuit_state_raw === 'halfopen') {
$cbclass = 'badge bg-warning text-dark';
}
echo html_writer::tag('td', html_writer::tag('span', $svc->circuit_state, ['class' => $cbclass]));
// Latency.
$latency = $svc->avg_latency ? round($svc->avg_latency) . ' ms' : '—';
echo html_writer::tag('td', $latency);
// Recent errors.
$errclass = $svc->recent_errors > 0 ? 'text-danger fw-bold' : 'text-muted';
echo html_writer::tag('td', html_writer::tag('span', $svc->recent_errors, ['class' => $errclass]));
// Enabled status.
$statusclass = $svc->enabled ? 'badge bg-success' : 'badge bg-secondary';
$statuslabel = $svc->enabled
? get_string('status_active', 'local_integrationhub')
: get_string('status_disabled', 'local_integrationhub');
echo html_writer::tag('td', html_writer::tag('span', $statuslabel, ['class' => $statusclass]));
// Actions.
if ($canmanage) {
echo '<td class="text-nowrap">';
// Logs for this service.
$logsurl = new moodle_url('/local/integrationhub/logs.php', ['serviceid' => $svc->id]);
echo html_writer::link(
$logsurl,
'<i class="fa fa-bar-chart"></i> ',
['class' => 'btn btn-sm btn-outline-info me-1', 'title' => get_string('viewlogs', 'local_integrationhub')]
);
// Edit button.
$editurl = new moodle_url($PAGE->url, ['action' => 'edit', 'serviceid' => $svc->id]);
echo html_writer::link(
$editurl,
'<i class="fa fa-pencil"></i> ',
['class' => 'btn btn-sm btn-outline-primary me-1', 'title' => get_string('editservice', 'local_integrationhub')]
);
// Delete button.
$deleteurl = new moodle_url($PAGE->url, [
'action' => 'delete', 'serviceid' => $svc->id,
'confirm' => 1, 'sesskey' => sesskey(),
]);
echo html_writer::link(
$deleteurl,
'<i class="fa fa-trash"></i> ',
[
'class' => 'btn btn-sm btn-outline-danger me-1',
'title' => get_string('deleteservice', 'local_integrationhub'),
'onclick' => "return confirm('" .
addslashes_js(get_string('deleteconfirm', 'local_integrationhub', $svc->name)) . "');",
]
);
// Reset circuit (only if not closed).
if ($svc->circuit_state_raw !== 'closed') {
$reseturl = new moodle_url($PAGE->url, [
'action' => 'resetcircuit', 'serviceid' => $svc->id, 'sesskey' => sesskey(),
]);
echo html_writer::link(
$reseturl,
'<i class="fa fa-refresh"></i> ',
[
'class' => 'btn btn-sm btn-outline-warning',
'title' => get_string('resetcircuit', 'local_integrationhub'),
]
);
}
echo '</td>';
}
echo '</tr>';
}
echo '</tbody></table></div>';
}
// Delete confirmation dialog (if pending).
if ($action === 'delete' && $serviceid > 0 && !$confirm && $canmanage) {
try {
$service = service_registry::get_service_by_id($serviceid);
$confirmurl = new moodle_url($PAGE->url, [
'action' => 'delete', 'serviceid' => $serviceid,
'confirm' => 1, 'sesskey' => sesskey(),
]);
$cancelurl = $PAGE->url;
echo $OUTPUT->confirm(
get_string('deleteconfirm', 'local_integrationhub', $service->name),
$confirmurl,
$cancelurl
);
} catch (\Exception $e) {
// Service not found, just redirect.
redirect($PAGE->url);
}
}
// Call AMD Module.
$PAGE->requires->js_call_amd('local_integrationhub/dashboard', 'init', [
$chartdatajs,
[
'success' => get_string('success', 'local_integrationhub'),
'failure' => get_string('failure', 'local_integrationhub'),
'avglatency' => get_string('avglatency', 'local_integrationhub'),
'url_help_rest' => get_string('url_help_rest', 'local_integrationhub'),
'url_help_amqp' => get_string('url_help_amqp', 'local_integrationhub'),
],
]);
echo $OUTPUT->footer();