Rework search to show jobId matches in table, add projectId search

This commit is contained in:
Christoph Kluge 2022-12-14 10:02:22 +01:00
parent ac8548a950
commit c9ee8b552d
3 changed files with 32 additions and 20 deletions

View File

@ -295,14 +295,13 @@ func main() {
} }
secured.Handle("/query", graphQLEndpoint) 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) { secured.HandleFunc("/search", func(rw http.ResponseWriter, r *http.Request) {
if search := r.URL.Query().Get("searchId"); search != "" { if search := r.URL.Query().Get("searchId"); search != "" {
job, username, err := api.JobRepository.FindJobOrUser(r.Context(), search)
if err == repository.ErrNotFound { _, username, project, err := api.JobRepository.FindJobOrUserOrProject(r.Context(), search)
http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(search), http.StatusTemporaryRedirect) // Directly to table! -> Running Jobs probably
return if err != nil {
} else if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
@ -310,10 +309,14 @@ func main() {
if username != "" { if username != "" {
http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusTemporaryRedirect) http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusTemporaryRedirect)
return return
} else if (project != "") {
http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+project, http.StatusTemporaryRedirect) // Directly to table!
return
} else { } 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 return
} }
} else { } else {
http.Error(rw, "'searchId' query parameter missing", http.StatusBadRequest) http.Error(rw, "'searchId' query parameter missing", http.StatusBadRequest)
} }

View File

@ -358,17 +358,15 @@ func (r *JobRepository) Archive(
return nil 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. // 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 is "" instead in order to check what machted. // 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. // If nothing matches the search, `ErrNotFound` is returned.
// TO BE IMPROVED; SHOW ALL MATCHES (eg: multiple clusters with matching jobId) + Search by JobNAME // TO BE IMPROVED; 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!]
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) user := auth.GetUser(ctx)
if id, err := strconv.Atoi(searchterm); err == nil { if id, err := strconv.Atoi(searchterm); err == nil {
qb := sq.Select("job.id").From("job").Where("job.job_id = ?", id) 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) err := qb.RunWith(r.stmtCache).QueryRow().Scan(&job)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return 0, "", err return 0, "", "", err
} else if err == nil { } 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). Where("job.user = ?", searchterm).
RunWith(r.stmtCache).QueryRow().Scan(&username) RunWith(r.stmtCache).QueryRow().Scan(&username)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
return 0, "", err return 0, "", "", err
} else if err == nil { } 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) { func (r *JobRepository) Partitions(cluster string) ([]string, error) {

View File

@ -55,7 +55,7 @@
<div class="d-flex"> <div class="d-flex">
<form method="GET" action="/search"> <form method="GET" action="/search">
<InputGroup> <InputGroup>
<Input type="text" placeholder={isAdmin ? "Search jobId / username" : "Search jobId"} name="searchId"/> <Input type="text" placeholder={isAdmin ? "Search jobId/username/projectId" : "Search jobId"} name="searchId"/>
<Button outline type="submit"><Icon name="search"/></Button> <Button outline type="submit"><Icon name="search"/></Button>
</InputGroup> </InputGroup>
</form> </form>