From ad789c73bddc94bc5a4dc5233a3aae2a24fcfa50 Mon Sep 17 00:00:00 2001 From: Hadi <112569860+anotherhadi@users.noreply.github.com> Date: Sat, 4 Oct 2025 19:19:19 +0200 Subject: [PATCH] Cancel queued search Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com> --- back/api/api.go | 38 +++++++++++++++++ back/search/search.go | 4 +- .../components/index/search/history.svelte | 41 ++++++++++++++++--- front/src/lib/types.ts | 2 +- 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/back/api/api.go b/back/api/api.go index 792c14b..d7d92f7 100644 --- a/back/api/api.go +++ b/back/api/api.go @@ -118,6 +118,44 @@ func routes(s *server.Server, cache *map[string]*search.Result, searchQueue chan c.JSON(http.StatusOK, r) }) + s.Router.POST("/search/cancel/:id", func(c *gin.Context) { + id := c.Param("id") + + s.Mu.Lock() + r, exists := (*cache)[id] + s.Mu.Unlock() + + if !exists { + c.JSON(http.StatusNotFound, gin.H{"Error": "Search not found"}) + return + } + + if r.Status == "queued" { + newQueue := make(chan string, cap(searchQueue)) + for { + select { + case queuedId := <-searchQueue: + if queuedId != id { + newQueue <- queuedId + } + default: + goto Done + } + } + Done: + searchQueue = newQueue + + s.Mu.Lock() + r.Status = "cancelled" + s.Mu.Unlock() + + c.JSON(http.StatusOK, gin.H{"Status": "Cancelled"}) + return + } + + c.JSON(http.StatusBadRequest, gin.H{"Error": "Cannot cancel running or finished search"}) + }) + s.Router.GET("/dataleak/sample", func(c *gin.Context) { path := c.Query("path") if path == "" { diff --git a/back/search/search.go b/back/search/search.go index 7163a6e..c3e9738 100644 --- a/back/search/search.go +++ b/back/search/search.go @@ -27,7 +27,7 @@ type Query struct { type Result struct { Id string Date time.Time - Status string // "queued", "pending", "completed", "error" + Status string // "queued", "pending", "completed", "error", "cancelled" Query Query ResultsCount int // Total number of results found across all services @@ -44,6 +44,8 @@ func Search(s *server.Server, q Query, r *Result, mu *sync.RWMutex) { r.ResultsCount = 0 mu.Unlock() + time.Sleep(20 * time.Second) // To ensure the status update is sent to the client before starting the search + wg.Add(3) go func() { if !q.Datawells { diff --git a/front/src/lib/components/index/search/history.svelte b/front/src/lib/components/index/search/history.svelte index 3437eca..3097a32 100644 --- a/front/src/lib/components/index/search/history.svelte +++ b/front/src/lib/components/index/search/history.svelte @@ -1,8 +1,11 @@