Adding notifications log

This commit is contained in:
Axel 2021-12-23 19:55:11 +01:00
parent afd273081b
commit 8d7bd04424
4 changed files with 68 additions and 20 deletions

View file

@ -5,7 +5,6 @@ namespace App\Http\Controllers;
use Exception; use Exception;
use \Carbon\Carbon; use \Carbon\Carbon;
use App\Models\Task; use App\Models\Task;
use App\Models\TaskHistory;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -80,26 +79,27 @@ class ApiController extends Controller
if (! is_null($task)) { if (! is_null($task)) {
// First, we get the first date of the stats // First, we get the first date of the stats
// In this case, one month ago // In this case, one month ago
$date = $last_days = Carbon::now()->subDays($days); $last_days = Carbon::now()->subDays($days);
// Then we get all history for the past month // Then we get all history for the past month
$history = $task $history = $task
->history() ->history()
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->where('created_at', '>', $last_days->toDateString()) ->where('created_at', '>', $last_days->toDateString())
->selectRaw('date(created_at) as date, created_at, status') ->selectRaw('id, date(created_at) as date, created_at, status')
->get() ->get()
; ;
// Then we start building an array for the entire month // Then we start building an array for the entire month
$stats = []; $stats = [];
$tmpdate = Carbon::now()->subDays($days);
do { do {
$stats[$date->toDateString()] = [ $stats[$tmpdate->toDateString()] = [
'up' => 0, 'up' => 0,
'down' => 0 'down' => 0
]; ];
$date = $date->addDay(); $tmpdate = $tmpdate->addDay();
} }
while ($date->lt(Carbon::now())); while ($tmpdate->lt(Carbon::now()));
// Then we populate the stats data // Then we populate the stats data
if (! is_null($history)) { if (! is_null($history)) {
@ -133,10 +133,20 @@ class ApiController extends Controller
} }
} }
// Getting the notifications sent
$notifications = $task
->notifications()
->with('contact')
->where('notifications.created_at', '>', $last_days->toDateString())
->orderBy('notifications.created_at', 'desc')
->get()
;
return response()->json([ return response()->json([
'task' => $task, 'task' => $task,
'stats' => $stats, 'stats' => $stats,
'history' => $history 'history' => $history,
'notifications' => $notifications
]); ]);
} }
} }
@ -153,7 +163,7 @@ class ApiController extends Controller
$task->active = $active; $task->active = $active;
if ($task->save()) { if ($task->save()) {
return $this->getTaskDetails($id); return response()->json($task);
} }
else { else {
throw new ApiException('Cannot disable this task'); throw new ApiException('Cannot disable this task');

View file

@ -26,10 +26,6 @@ class Task extends Model
return $this->belongsTo('App\Models\Group'); return $this->belongsTo('App\Models\Group');
} }
public function notifications() {
return $this->hasMany('App\Models\Notification');
}
public function contacts() { public function contacts() {
return $this->belongsToMany('App\Models\Contact'); return $this->belongsToMany('App\Models\Contact');
} }
@ -37,4 +33,8 @@ class Task extends Model
public function history() { public function history() {
return $this->hasMany('App\Models\TaskHistory'); return $this->hasMany('App\Models\TaskHistory');
} }
public function notifications() {
return $this->hasManyThrough('App\Models\Notification', 'App\Models\TaskHistory');
}
} }

File diff suppressed because one or more lines are too long

View file

@ -4,7 +4,7 @@
v-if="task.id != null" v-if="task.id != null"
> >
<h1> <h1>
Task #{{ task.id }} <span class="highlight">{{ task.type }}</span> for host <span class="highlight">{{ task.host }}</span>
<!-- <p class="context-menu"><img src="/img/menu.svg" width="40" /></p> --> <!-- <p class="context-menu"><img src="/img/menu.svg" width="40" /></p> -->
</h1> </h1>
@ -17,6 +17,8 @@
<option value="15">15 days</option> <option value="15">15 days</option>
<option value="30">30 days</option> <option value="30">30 days</option>
</select> </select>
<!-- Chart block -->
<div id="chart" class="round"> <div id="chart" class="round">
<h3>Uptime: past {{ chart.days }} days</h3> <h3>Uptime: past {{ chart.days }} days</h3>
<div class="block-content"> <div class="block-content">
@ -24,9 +26,10 @@
</div> </div>
</div> </div>
<!-- History backlog -->
<div class="round"> <div class="round">
<h3>Last {{ chart.days }} days history log</h3> <h3>Last {{ chart.days }} days history log</h3>
<div class="block-content" v-if="history"> <div class="block-content" v-if="history.length > 0">
<p><i>Showing only records where status has changed</i></p> <p><i>Showing only records where status has changed</i></p>
<table id="tasks_tbl"> <table id="tasks_tbl">
<thead> <thead>
@ -61,6 +64,39 @@
</div> </div>
<p v-else><center>No history to display here</center></p> <p v-else><center>No history to display here</center></p>
</div> </div>
<!-- Notifications block -->
<div class="round">
<h3>Last {{ chart.days }} days notifications log</h3>
<div class="block-content" v-if="notifications.length > 0">
<table id="tasks_tbl">
<thead>
<tr>
<th width="20%">Date</th>
<th width="20%">Time</th>
<th width="*">Firstname</th>
<th width="10%">Lastname</th>
<th width="10%">Email</th>
<th width="10%">Status</th>
</tr>
</thead>
<tbody>
<tr
v-for="n in notifications"
v-bind:key="n.id"
>
<td>{{ moment(n.created_at).format('YYYY-MM-DD') }}</td>
<td>{{ moment(n.created_at).format('HH:mm:ss') }}</td>
<td>{{ n.contact.firstname }}</td>
<td>{{ n.contact.surname }}</td>
<td>{{ n.contact.email }}</td>
<td>{{ n.status }}</td>
</tr>
</tbody>
</table>
</div>
<p v-else><center>No notification to display here</center></p>
</div>
</div> </div>
</div> </div>
</template> </template>
@ -74,6 +110,7 @@
id: null id: null
}, },
history: null, history: null,
notifications: null,
refresh: null, refresh: null,
chart: { chart: {
@ -125,8 +162,9 @@
days: this.chart.days days: this.chart.days
}) })
.then(response => { .then(response => {
this.task = response.data.task this.task = response.data.task
this.history = response.data.history this.history = response.data.history
this.notifications = response.data.notifications
this.refreshGraph(response.data.stats) this.refreshGraph(response.data.stats)
}) })