99use App \Support \WorkflowQueryTaskBroker ;
1010use Illuminate \Http \JsonResponse ;
1111use Illuminate \Http \Request ;
12+ use Workflow \V2 \Enums \TaskType ;
13+ use Workflow \V2 \Models \WorkflowTask ;
1214use Workflow \V2 \Support \StandaloneWorkerVisibility ;
1315
1416class TaskQueueController
@@ -35,7 +37,7 @@ public function index(Request $request): JsonResponse
3537 $ payload = [
3638 'namespace ' => $ snapshot ->namespace ,
3739 'task_queues ' => array_map (function ($ detail ) use ($ namespace ): array {
38- $ summary = $ detail ->toSummaryArray ();
40+ $ summary = $ this -> withRecentTaskFlow ( $ namespace , $ detail ->name , $ detail -> toSummaryArray () );
3941 $ summary ['pollers ' ] = $ detail ->pollers ();
4042 $ summary = $ this ->withAdmission ($ namespace , $ summary );
4143 unset($ summary ['pollers ' ]);
@@ -56,13 +58,20 @@ public function show(Request $request, string $taskQueue): JsonResponse
5658 $ namespace = (string ) $ request ->attributes ->get ('namespace ' );
5759
5860 return ControlPlaneProtocol::json (
59- $ this ->withAdmission ($ namespace , StandaloneWorkerVisibility:: queueDetail (
61+ $ this ->withAdmission (
6062 $ namespace ,
61- $ taskQueue ,
62- WorkerRegistration::class,
63- now (),
64- $ this ->workerStaleAfterSeconds (),
65- )->toArray ()),
63+ $ this ->withRecentTaskFlow (
64+ $ namespace ,
65+ $ taskQueue ,
66+ StandaloneWorkerVisibility::queueDetail (
67+ $ namespace ,
68+ $ taskQueue ,
69+ WorkerRegistration::class,
70+ now (),
71+ $ this ->workerStaleAfterSeconds (),
72+ )->toArray (),
73+ ),
74+ ),
6675 );
6776 }
6877
@@ -541,6 +550,42 @@ private function taskAdmissionStatus(int $activeWorkerCount, int $configuredSlot
541550 return $ leasedCount >= $ configuredSlots ? 'saturated ' : 'accepting ' ;
542551 }
543552
553+ /**
554+ * @param array<string, mixed> $payload
555+ * @return array<string, mixed>
556+ */
557+ private function withRecentTaskFlow (string $ namespace , string $ taskQueue , array $ payload ): array
558+ {
559+ $ stats = is_array ($ payload ['stats ' ] ?? null ) ? $ payload ['stats ' ] : [];
560+ $ stats = array_merge ($ stats , $ this ->recentTaskFlow ($ namespace , $ taskQueue ));
561+ $ payload ['stats ' ] = $ stats ;
562+
563+ return $ payload ;
564+ }
565+
566+ /**
567+ * @return array{tasks_added_last_minute: int, tasks_dispatched_last_minute: int}
568+ */
569+ private function recentTaskFlow (string $ namespace , string $ taskQueue ): array
570+ {
571+ $ windowStart = now ()->subMinute ();
572+
573+ $ query = WorkflowTask::query ()
574+ ->where ('namespace ' , $ namespace )
575+ ->where ('queue ' , $ taskQueue )
576+ ->whereIn ('task_type ' , [TaskType::Workflow->value , TaskType::Activity->value ]);
577+
578+ return [
579+ 'tasks_added_last_minute ' => (clone $ query )
580+ ->where ('created_at ' , '>= ' , $ windowStart )
581+ ->count (),
582+ 'tasks_dispatched_last_minute ' => (clone $ query )
583+ ->whereNotNull ('last_dispatched_at ' )
584+ ->where ('last_dispatched_at ' , '>= ' , $ windowStart )
585+ ->count (),
586+ ];
587+ }
588+
544589 private function workerStaleAfterSeconds (): int
545590 {
546591 $ configured = config ('server.workers.stale_after_seconds ' );
0 commit comments