Fix bug in SearchBar Handler

Introduce Message boxes
Incomplete and needs cleanup
This commit is contained in:
Jan Eitzinger 2023-06-22 16:26:09 +02:00
parent bcadb1adda
commit 82b8e8c284
4 changed files with 85 additions and 20 deletions

View File

@ -316,7 +316,7 @@ func main() {
// Send a searchId and then reply with a redirect to a user, or directly send query to job table for jobid and project. // 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) {
routerConfig.HandleSearchBar(rw, r, api) routerConfig.HandleSearchBar(rw, r)
}) })
// Mount all /monitoring/... and /api/... routes. // Mount all /monitoring/... and /api/... routes.

View File

@ -12,7 +12,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/ClusterCockpit/cc-backend/internal/api"
"github.com/ClusterCockpit/cc-backend/internal/auth" "github.com/ClusterCockpit/cc-backend/internal/auth"
"github.com/ClusterCockpit/cc-backend/internal/graph/model" "github.com/ClusterCockpit/cc-backend/internal/graph/model"
"github.com/ClusterCockpit/cc-backend/internal/repository" "github.com/ClusterCockpit/cc-backend/internal/repository"
@ -270,8 +269,9 @@ func SetupRoutes(router *mux.Router, version string, hash string, buildTime stri
} }
} }
func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi) { func HandleSearchBar(rw http.ResponseWriter, r *http.Request) {
if search := r.URL.Query().Get("searchId"); search != "" { if search := r.URL.Query().Get("searchId"); search != "" {
repo := repository.GetJobRepository()
user := auth.GetUser(r.Context()) user := auth.GetUser(r.Context())
splitSearch := strings.Split(search, ":") splitSearch := strings.Split(search, ":")
@ -287,10 +287,11 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi)
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound) http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound)
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Users: Redirect to Tablequery web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"})
// web.RenderMessage(rw, "error", "Missing access rights!")
} }
case "name": case "name":
usernames, _ := api.JobRepository.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name") usernames, _ := repo.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name")
if len(usernames) != 0 { if len(usernames) != 0 {
joinedNames := strings.Join(usernames, "&user=") joinedNames := strings.Join(usernames, "&user=")
http.Redirect(rw, r, "/monitoring/users/?user="+joinedNames, http.StatusFound) http.Redirect(rw, r, "/monitoring/users/?user="+joinedNames, http.StatusFound)
@ -298,23 +299,27 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi)
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect) http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect)
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Users: Redirect to Tablequery web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Missing Access Rights"})
// web.RenderMessage(rw, "error", "Missing access rights!")
} }
} }
default: default:
log.Warnf("Searchbar type parameter '%s' unknown", strings.Trim(splitSearch[0], " ")) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " "))})
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery // web.RenderMessage(rw, "error", fmt.Sprintf("Unknown search term %s", strings.Trim(splitSearch[0], " ")))
} }
} else if len(splitSearch) == 1 { } else if len(splitSearch) == 1 {
username, project, jobname, _ := api.JobRepository.FindUserOrProjectOrJobname(user, strings.Trim(search, " ")) username, project, jobname, err := repo.FindUserOrProjectOrJobname(user, strings.Trim(search, " "))
// err := fmt.Errorf("Blabla")
/* Causes 'http: superfluous response.WriteHeader call' causing SSL error and frontend crash: Cause unknown*/ /* Causes 'http: superfluous response.WriteHeader call' causing SSL error and frontend crash: Cause unknown*/
// if err != nil { if err != nil {
web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "No search result"})
return
// web.RenderMessage(rw, "info", "Search with no result")
// log.Errorf("Error while searchbar best guess: %v", err.Error()) // log.Errorf("Error while searchbar best guess: %v", err.Error())
// http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery }
//}
if username != "" { if username != "" {
http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusFound) // User: Redirect to user page http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusFound) // User: Redirect to user page
@ -327,11 +332,11 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi)
} }
} else { } else {
log.Warnf("Searchbar query parameters malformed: %v", search) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Searchbar query parameters malformed"})
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery // web.RenderMessage(rw, "warn", "Searchbar query parameters malformed")
} }
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Warn", Info: "Empty search"})
// web.RenderMessage(rw, "warn", "Empty search")
} }
} }

View File

@ -0,0 +1,21 @@
{{define "navigation"}}
<header>
<nav class="navbar navbar-expand-lg navbar-light fixed-top bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="/">
<img style="height: 30px;" alt="ClusterCockpit Logo" src="/img/logo.png" class="d-inline-block align-top">
</a>
</div>
</nav>
</header>
{{end}}
{{define "content"}}
<div class="row">
<div class="col">
<div class="alert alert-info" role="alert">
{{.Info}}
</div>
</div>
</div>
{{end}}

View File

@ -96,8 +96,7 @@ type Page struct {
func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page *Page) { func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page *Page) {
t, ok := templates[file] t, ok := templates[file]
if !ok { if !ok {
log.Fatalf("WEB/WEB > template '%s' not found", file) log.Errorf("WEB/WEB > template '%s' not found", file)
panic("template not found")
} }
if page.Clusters == nil { if page.Clusters == nil {
@ -106,7 +105,47 @@ func RenderTemplate(rw http.ResponseWriter, r *http.Request, file string, page *
} }
} }
log.Infof("Page config : %v\n", page.Config) log.Debugf("Page config : %v\n", page.Config)
if err := t.Execute(rw, page); err != nil {
log.Errorf("Template error: %s", err.Error())
}
}
type Message struct {
Title string
Type string
Message string
Icon string
}
func RenderMessage(rw http.ResponseWriter, msgType string, msg string) {
var page Message
log.Info("render message template")
switch msgType {
case "success":
page.Title = "Success"
page.Type = "alert-success"
case "info":
page.Title = "Info"
page.Type = "alert-info"
case "warn":
page.Title = "Warning"
page.Type = "alert-warning"
case "error":
page.Title = "Error"
page.Type = "alert-danger"
default:
page.Title = "Message"
page.Type = "alert-secondary"
}
t, ok := templates["message.tmpl"]
if !ok {
log.Error("WEB/WEB > template message.tmpl not found")
}
page.Message = msg
rw.Header().Add("Content-Type", "text/html; charset=utf-8")
if err := t.Execute(rw, page); err != nil { if err := t.Execute(rw, page); err != nil {
log.Errorf("Template error: %s", err.Error()) log.Errorf("Template error: %s", err.Error())
} }