From 6e28dc05fdde1bee4e5be66dc4286926fbd00c0b Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Thu, 12 Dec 2024 19:48:21 +0100
Subject: [PATCH] feat: queue info

---
 .idea/workspace.xml |  4 +++-
 manager.go          |  6 +++++
 queue.go            | 54 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index cff611a..b6599b1 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -6,7 +6,8 @@
   <component name="ChangeListManager">
     <list default="true" id="9979eb22-471e-4f2f-b624-fd3edb5e8c6e" name="Changes" comment="">
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/worker.go" beforeDir="false" afterPath="$PROJECT_DIR$/worker.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/manager.go" beforeDir="false" afterPath="$PROJECT_DIR$/manager.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/queue.go" beforeDir="false" afterPath="$PROJECT_DIR$/queue.go" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -256,6 +257,7 @@
       <workItem from="1734001335693" duration="5602000" />
       <workItem from="1734012566872" duration="4517000" />
       <workItem from="1734022155342" duration="424000" />
+      <workItem from="1734024671338" duration="491000" />
     </task>
     <servers />
   </component>
diff --git a/manager.go b/manager.go
index ee6f1c6..351726b 100644
--- a/manager.go
+++ b/manager.go
@@ -57,6 +57,12 @@ func NewManager() *Manager {
 
 }
 
+func (m *Manager) GetQueueStats() QueueStats {
+	m.mu.Lock()
+	defer m.mu.Unlock()
+	return m.queue.Stats()
+}
+
 // GetEventBus returns the event bus
 func (m *Manager) GetEventBus() *EventBus {
 	m.mu.Lock()
diff --git a/queue.go b/queue.go
index 7bf6a35..76cd7d0 100644
--- a/queue.go
+++ b/queue.go
@@ -191,3 +191,57 @@ func removeJobID(deps []JobID, id JobID) []JobID {
 	}
 	return deps
 }
+
+type QueueStats struct {
+	ReadyQueueLength         int           `json:"ReadyQueueLength"`
+	JobMapLength             int           `json:"JobMapLength"`
+	PendingDeps              int           `json:"PendingDeps"`
+	ProcessedJobsCount       int           `json:"ProcessedJobsCount"`
+	MaxAgeReachedCount       int           `json:"MaxAgeReachedCount"`
+	PendingDependenciesCount int           `json:"PendingDependenciesCount"`
+	OldestProcessedJobAge    time.Duration `json:"OldestProcessedJobAge"`
+}
+
+// Stats computes and returns the statistics of the job queue
+func (q *Queue) Stats() QueueStats {
+	q.mu.Lock()
+	defer q.mu.Unlock()
+
+	// Calculate the oldest processed job age
+	var oldestProcessedJobAge time.Duration
+	if len(q.processedJobs) > 0 {
+		oldestAge := time.Now().Sub(q.processedJobs[0].ProcessedTime)
+		for _, job := range q.processedJobs {
+			jobAge := time.Now().Sub(job.ProcessedTime)
+			if jobAge > oldestAge {
+				oldestAge = jobAge
+			}
+		}
+		oldestProcessedJobAge = oldestAge
+	}
+
+	// Count how many jobs have reached MaxAge
+	maxAgeReachedCount := 0
+	currentTime := time.Now()
+	for _, jobInfo := range q.processedJobs {
+		if currentTime.Sub(jobInfo.ProcessedTime) > MaxAge {
+			maxAgeReachedCount++
+		}
+	}
+
+	// Count all pending dependencies
+	var totalPendingDeps = 0
+	for _, deps := range q.pendingDependencies {
+		totalPendingDeps += len(deps)
+	}
+
+	return QueueStats{
+		ReadyQueueLength:         len(q.readyQueue),
+		JobMapLength:             len(q.jobMap),
+		PendingDeps:              len(q.pendingDependencies),
+		ProcessedJobsCount:       len(q.processedJobs),
+		MaxAgeReachedCount:       maxAgeReachedCount,
+		PendingDependenciesCount: totalPendingDeps,
+		OldestProcessedJobAge:    oldestProcessedJobAge,
+	}
+}
-- 
GitLab