From c9ee8b552d7d80d21d2a23c032459dab7ff6f2e5 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Wed, 14 Dec 2022 10:02:22 +0100 Subject: [PATCH] Rework search to show jobId matches in table, add projectId search --- cmd/cc-backend/main.go | 17 ++++++++++------- internal/repository/job.go | 33 +++++++++++++++++++++------------ web/frontend/src/Header.svelte | 2 +- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cmd/cc-backend/main.go b/cmd/cc-backend/main.go index 8d39a25..bc6a265 100644 --- a/cmd/cc-backend/main.go +++ b/cmd/cc-backend/main.go @@ -295,14 +295,13 @@ func main() { } secured.Handle("/query", graphQLEndpoint) - // Send a searchId and then reply with a redirect to a user or job. // IMPROVE HERE + // Send a searchId and then reply with a redirect to a user, or directly send query to job table for jobid and project. secured.HandleFunc("/search", func(rw http.ResponseWriter, r *http.Request) { if search := r.URL.Query().Get("searchId"); search != "" { - job, username, err := api.JobRepository.FindJobOrUser(r.Context(), search) - if err == repository.ErrNotFound { - http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(search), http.StatusTemporaryRedirect) // Directly to table! -> Running Jobs probably - return - } else if err != nil { + + _, username, project, err := api.JobRepository.FindJobOrUserOrProject(r.Context(), search) + + if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } @@ -310,10 +309,14 @@ func main() { if username != "" { http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusTemporaryRedirect) return + } else if (project != "") { + http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+project, http.StatusTemporaryRedirect) // Directly to table! + return } else { - http.Redirect(rw, r, fmt.Sprintf("/monitoring/job/%d", job), http.StatusTemporaryRedirect) + http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(search), http.StatusTemporaryRedirect) // Directly to table! return } + } else { http.Error(rw, "'searchId' query parameter missing", http.StatusBadRequest) } diff --git a/internal/repository/job.go b/internal/repository/job.go index 73b3517..7902ce0 100644 --- a/internal/repository/job.go +++ b/internal/repository/job.go @@ -358,17 +358,15 @@ func (r *JobRepository) Archive( return nil } -var ErrNotFound = errors.New("no such job or user") +var ErrNotFound = errors.New("no such job, project or user") -// FindJobOrUser returns a job database ID or a username if a job or user matches the search term. -// As 0 is a valid job id, check if username is "" instead in order to check what machted. +// FindJobOrUserOrProject returns a job database ID or a username or a projectId if a job or user or project matches the search term. +// As 0 is a valid job id, check if username/projectId is "" instead in order to check what matched. // If nothing matches the search, `ErrNotFound` is returned. -// TO BE IMPROVED; SHOW ALL MATCHES (eg: multiple clusters with matching jobId) + Search by JobNAME -// Plan: Parent-Method: Solves Searchtag or, if no searchtag, tries to find by hierarical searchId -// Plan: Nested methods for jobid, jobname (?), username, project, tag [dependent on roles of user, do not forget api!] +// TO BE IMPROVED; Search by JobNAME -func (r *JobRepository) FindJobOrUser(ctx context.Context, searchterm string) (job int64, username string, err error) { +func (r *JobRepository) FindJobOrUserOrProject(ctx context.Context, searchterm string) (job int64, username string, project string, err error) { user := auth.GetUser(ctx) if id, err := strconv.Atoi(searchterm); err == nil { qb := sq.Select("job.id").From("job").Where("job.job_id = ?", id) @@ -378,9 +376,9 @@ func (r *JobRepository) FindJobOrUser(ctx context.Context, searchterm string) (j err := qb.RunWith(r.stmtCache).QueryRow().Scan(&job) if err != nil && err != sql.ErrNoRows { - return 0, "", err + return 0, "", "", err } else if err == nil { - return job, "", nil + return job, "", "", nil } } @@ -389,13 +387,24 @@ func (r *JobRepository) FindJobOrUser(ctx context.Context, searchterm string) (j Where("job.user = ?", searchterm). RunWith(r.stmtCache).QueryRow().Scan(&username) if err != nil && err != sql.ErrNoRows { - return 0, "", err + return 0, "", "", err } else if err == nil { - return 0, username, nil + return 0, username, "", nil } } - return 0, "", ErrNotFound + if user == nil || user.HasRole(auth.RoleAdmin) || user.HasRole(auth.RoleSupport) { + err := sq.Select("job.project").Distinct().From("job"). + Where("job.project = ?", searchterm). + RunWith(r.stmtCache).QueryRow().Scan(&project) + if err != nil && err != sql.ErrNoRows { + return 0, "", "", err + } else if err == nil { + return 0, "", project, nil + } + } + + return 0, "", "", ErrNotFound } func (r *JobRepository) Partitions(cluster string) ([]string, error) { diff --git a/web/frontend/src/Header.svelte b/web/frontend/src/Header.svelte index 95675f4..b541b2d 100644 --- a/web/frontend/src/Header.svelte +++ b/web/frontend/src/Header.svelte @@ -55,7 +55,7 @@
- +