mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-12-25 21:09:05 +01:00
Rework search to show jobId matches in table, add projectId search
This commit is contained in:
parent
ac8548a950
commit
c9ee8b552d
@ -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)
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -55,7 +55,7 @@
|
||||
<div class="d-flex">
|
||||
<form method="GET" action="/search">
|
||||
<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>
|
||||
</InputGroup>
|
||||
</form>
|
||||
|
Loading…
Reference in New Issue
Block a user