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 \Carbon\Carbon;
use App\Models\Task;
use App\Models\TaskHistory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@ -80,26 +79,27 @@ class ApiController extends Controller
if (! is_null($task)) {
// First, we get the first date of the stats
// 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
$history = $task
->history()
->orderBy('created_at', 'desc')
->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()
;
// Then we start building an array for the entire month
$stats = [];
$tmpdate = Carbon::now()->subDays($days);
do {
$stats[$date->toDateString()] = [
$stats[$tmpdate->toDateString()] = [
'up' => 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
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([
'task' => $task,
'stats' => $stats,
'history' => $history
'task' => $task,
'stats' => $stats,
'history' => $history,
'notifications' => $notifications
]);
}
}
@ -153,7 +163,7 @@ class ApiController extends Controller
$task->active = $active;
if ($task->save()) {
return $this->getTaskDetails($id);
return response()->json($task);
}
else {
throw new ApiException('Cannot disable this task');

View file

@ -26,10 +26,6 @@ class Task extends Model
return $this->belongsTo('App\Models\Group');
}
public function notifications() {
return $this->hasMany('App\Models\Notification');
}
public function contacts() {
return $this->belongsToMany('App\Models\Contact');
}
@ -37,4 +33,8 @@ class Task extends Model
public function history() {
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"
>
<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> -->
</h1>
@ -17,6 +17,8 @@
<option value="15">15 days</option>
<option value="30">30 days</option>
</select>
<!-- Chart block -->
<div id="chart" class="round">
<h3>Uptime: past {{ chart.days }} days</h3>
<div class="block-content">
@ -24,9 +26,10 @@
</div>
</div>
<!-- History backlog -->
<div class="round">
<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>
<table id="tasks_tbl">
<thead>
@ -61,6 +64,39 @@
</div>
<p v-else><center>No history to display here</center></p>
</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>
</template>
@ -74,6 +110,7 @@
id: null
},
history: null,
notifications: null,
refresh: null,
chart: {
@ -125,8 +162,9 @@
days: this.chart.days
})
.then(response => {
this.task = response.data.task
this.history = response.data.history
this.task = response.data.task
this.history = response.data.history
this.notifications = response.data.notifications
this.refreshGraph(response.data.stats)
})