Skip to content

Commit

Permalink
Added icon to WorkItem detail page showing state of NSQ worker node.
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Mar 12, 2024
1 parent 77eac3e commit 5c848ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
19 changes: 16 additions & 3 deletions network/nsq_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -59,6 +59,19 @@ func (data *NSQStatsData) GetChannel(topicName, channelName string) *nsqd.Channe
return nil
}

func (data *NSQStatsData) ClientIsRunning(hostname string) bool {
for _, topic := range data.Topics {
for _, channel := range topic.Channels {
for _, client := range channel.Clients {
if client.Hostname == hostname {
return true
}
}
}
}
return false
}

// NewNSQClient returns a new NSQ client that will connect to the NSQ
// server and the specified url. The URL is typically available through
// Config.NsqdHttpAddress, and usually ends with :4151. This is
Expand Down Expand Up @@ -98,7 +111,7 @@ func (client *NSQClient) EnqueueString(topic string, data string) error {

// nsqd sends a simple OK. We have to read the response body,
// or the connection will hang open forever.
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()

// NSQ response body is short. "OK" on success,
Expand All @@ -123,7 +136,7 @@ func (client *NSQClient) get(_url string) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("error connecting to nsq at %s: %v", client.URL, err)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("error reading nsq response body: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion views/work_items/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h2>{{ .item.Name }}</h2>
<dt class="text-label text-xs is-grey-dark">User</dt>
<dd class="text-table">{{ .item.User }}</dd>
<dt class="text-label text-xs is-grey-dark">Node</dt>
<dd class="text-table">{{ defaultString .item.Node "N/A" }}</dd>
<dd class="text-table">{{ defaultString .item.Node "N/A" }} {{ escapeHTML .clientStatusIcon }} </dd>
<dt class="text-label text-xs is-grey-dark">PID</dt>
<dd class="text-table">{{ .item.PID }}</dd>
<dt class="text-label text-xs is-grey-dark">APTrust Approver</dt>
Expand Down
19 changes: 19 additions & 0 deletions web/webui/work_items_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func WorkItemShow(c *gin.Context) {
}
req.TemplateData["item"] = item

// This has to be initialized to a string value, or the HTML template won't render.
req.TemplateData["clientStatusIcon"] = ""

// Show requeue options to Admin, if item has not completed.
// Only sys admin should have this permission.
userCanRequeue := req.CurrentUser.HasPermission(constants.WorkItemRequeue, item.InstitutionID)
Expand All @@ -48,6 +51,22 @@ func WorkItemShow(c *gin.Context) {
return
}
req.TemplateData["form"] = form

// Create an icon to display next to the node name indicating
// whether that node is currently running. Options are "?" for
// unknown, check for running, "!" for not running.
if item.Node != "" {
icon := `<i class="material-icons" aria-hidden="true">question_mark</i>`
nsqStats, err := common.Context().NSQClient.GetStats()
if err == nil && nsqStats != nil {
if nsqStats.ClientIsRunning(item.Node) {
icon = `<i class="material-icons" aria-hidden="true" style="color:#35826b">check_circle_outline</i>`
} else {
icon = `<i class="material-icons" aria-hidden="true" style="color:#be1f45">error_outline</i>`
}
}
req.TemplateData["clientStatusIcon"] = icon
}
}

// Let user fix missing object id, if necessary
Expand Down

0 comments on commit 5c848ba

Please sign in to comment.