|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Waterline\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use Workflow\Models\StoredWorkflow; |
| 7 | +use Workflow\Models\StoredWorkflowException; |
| 8 | + |
| 9 | +class DashboardStatsController extends Controller |
| 10 | +{ |
| 11 | + public function index() { |
| 12 | + |
| 13 | + $flowsPastHour = StoredWorkflow::where('updated_at', '>=', now()->subHour()) |
| 14 | + ->count(); |
| 15 | + |
| 16 | + $exceptionsPastHour = StoredWorkflowException::where('created_at', '>=', now()->subHour()) |
| 17 | + ->count(); |
| 18 | + |
| 19 | + $failedFlowsPastWeek = StoredWorkflow::where('status', 'failed') |
| 20 | + ->where('updated_at', '>=', now()->subDays(7)) |
| 21 | + ->count(); |
| 22 | + |
| 23 | + $maxWaitTimeWorkflow = StoredWorkflow::where('status', 'pending') |
| 24 | + ->orderBy('updated_at') |
| 25 | + ->first(); |
| 26 | + |
| 27 | + $maxDurationWorkflow = StoredWorkflow::select('*') |
| 28 | + ->addSelect(DB::raw('TIMEDIFF(created_at, updated_at) as duration')) |
| 29 | + ->where('status', '!=', 'pending') |
| 30 | + ->orderBy('duration') |
| 31 | + ->first(); |
| 32 | + |
| 33 | + $maxExceptionsWorkflow = StoredWorkflow::withCount('exceptions') |
| 34 | + ->orderByDesc('exceptions_count') |
| 35 | + ->orderByDesc('updated_at') |
| 36 | + ->first(); |
| 37 | + |
| 38 | + return response()->json([ |
| 39 | + 'flows' => StoredWorkflow::count(), |
| 40 | + 'flows_per_minute' => $flowsPastHour / 60, |
| 41 | + 'flows_past_hour' => $flowsPastHour, |
| 42 | + 'exceptions_past_hour' => $exceptionsPastHour, |
| 43 | + 'failed_flows_past_week' => $failedFlowsPastWeek, |
| 44 | + 'max_wait_time_workflow' => $maxWaitTimeWorkflow, |
| 45 | + 'max_duration_workflow' => $maxDurationWorkflow, |
| 46 | + 'max_exceptions_workflow' => $maxExceptionsWorkflow, |
| 47 | + ]); |
| 48 | + } |
| 49 | +} |
0 commit comments