package web import ( "context" "log" "net/http" "time" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "code.osinet.fr/fgm/sqs_demo/back/services/redriver" ) // classifyPolicies distributes queue policies across five bins: // - wild: policies describing a DLQ with an allowAll policy // - closed: policies describing a DLQ with an denyAll policy // - byQueue: policies describing a DLQ with an byQueue policy // - sources: policies describing a queue with a DLQ // - notSources: policies describing a queue without a DLQ // // Note that dead-letter queues may also by source queues, for multi-level processing, and vice-versa. // // For easier reading, DLQs are not listed as part of the non-source list. func classifyPolicies(policies map[string]*redriver.QueueRedrivePolicies) (byQueue, wild, closed, sources map[string]redriver.QueueRedrivePolicies, notSources []string) { DLQs := make(map[string]redriver.QueueRedrivePolicies) sources = make(map[string]redriver.QueueRedrivePolicies) wild = make(map[string]redriver.QueueRedrivePolicies) closed = make(map[string]redriver.QueueRedrivePolicies) byQueue = make(map[string]redriver.QueueRedrivePolicies) nsTemp := make([]string, 0, len(notSources)) for qURL, qrp := range policies { if qrp.QueueInfoAttributesRedriveAllowPolicy != nil { DLQs[qURL] = *qrp } if qrp.QueueInfoAttributesRedrivePolicy != nil { sources[qURL] = *qrp } else { nsTemp = append(nsTemp, qURL) } } for _, qURL := range nsTemp { if _, ok := DLQs[qURL]; !ok { notSources = append(notSources, qURL) } } for qURL, qrp := range DLQs { rap := qrp.QueueInfoAttributesRedriveAllowPolicy switch rap.RedrivePermission { case "allowAll": wild[qURL] = qrp case "denyAll": closed[qURL] = qrp case "byQueue": byQueue[qURL] = qrp } } return } func policiesByURL(c *gin.Context, qURLs []string, rd redriver.Redriver, ctx context.Context) map[string]*redriver.QueueRedrivePolicies { qMap := make(map[string]*redriver.QueueRedrivePolicies, len(qURLs)) for _, qURL := range qURLs { name, err := redriver.NameFromURL(qURL) if err != nil { log.Println(http.StatusInternalServerError) c.HTML(http.StatusInternalServerError, "500", nil) } qrp, err := rd.GetRedrivePolicies(ctx, name) if err != nil || qrp == nil { log.Println(err) c.HTML(http.StatusInternalServerError, "500", nil) } qMap[qURL] = qrp } return qMap } type link struct{ URL, Text string } // queueRow provides templates with a representation for all kinds of table cells type queueCell struct { Message string Link *link Links []link } type QueueRow [2]queueCell func makeHomeHandler(rd redriver.Redriver) gin.HandlerFunc { return func(c *gin.Context) { ctx := c.Request.Context() sess := sessions.Default(c) flashes := sess.Flashes() defer func() { _ = sess.Save() }() t0 := time.Now() qURLs, err := rd.ListQueues(ctx, "") latency := time.Since(t0) if err != nil { log.Printf("failed listing queues: %v", err) c.HTML(http.StatusInternalServerError, "500", nil) return } policies := policiesByURL(c, qURLs, rd, ctx) byqueue, wild, closed, sources, notSources := classifyPolicies(policies) // log.Printf("DLQs: %#v\nSRCs: %#v\nNoSRCs: %#v\n", maps.Keys(DLQs), maps.Keys(SRCs), NoSRCs) l := len(byqueue) if len(wild) > 0 { l++ } if len(closed) > 0 { l++ } if len(notSources) > 0 { l++ } rows := make([]QueueRow, 0, l) // Sources appears within a byQueue row rows, err = prepareSingleLeft(wild, rows, "All source queues allowed") if err != nil { log.Printf("failed converting queue URL %q: %v", qURLs, err) c.HTML(http.StatusInternalServerError, "500", nil) return } rows, err = prepareSingleLeft(closed, rows, "No source queue allowed") if err != nil { log.Printf("failed converting queue URL %q: %v", qURLs, err) c.HTML(http.StatusInternalServerError, "500", nil) return } rows, err = prepareSingleRight(notSources, rows, "No associated DLQ") if err != nil { log.Printf("failed converting queue URL %q: %v", qURLs, err) c.HTML(http.StatusInternalServerError, "500", nil) return } // for qURLs := range closed { // // } log.Println(sources) c.HTML(http.StatusOK, "home", gin.H{ "flashes": flashes, "latency": latency, "rows": rows, }) } } func prepareSingleLeft(list map[string]redriver.QueueRedrivePolicies, rows []QueueRow, msg string) ([]QueueRow, error) { if len(list) > 0 { row := QueueRow{ {Message: msg}, {Links: make([]link, 0, len(list))}, } for qURL := range list { name, err := redriver.NameFromURL(qURL) if err != nil { return nil, err } row[1].Links = append(row[1].Links, link{URL: "/queue/" + name, Text: name}) } rows = append(rows, row) } return rows, nil } func prepareSingleRight(list []string, rows []QueueRow, msg string) ([]QueueRow, error) { if len(list) > 0 { row := QueueRow{ {Links: make([]link, 0, len(list))}, {Message: msg}, } for _, qURL := range list { name, err := redriver.NameFromURL(qURL) if err != nil { return nil, err } row[0].Links = append(row[0].Links, link{URL: "/queue/" + name, Text: name}) } rows = append(rows, row) } return rows, nil }