Skip to content

Commit

Permalink
Merge pull request #3 from fujiwara/service
Browse files Browse the repository at this point in the history
Accept service name as node name
  • Loading branch information
fujiwara committed Jan 13, 2017
2 parents c0c20df + cb234ca commit f9f24b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions assets/scripts/dashboard.js
Expand Up @@ -19,7 +19,7 @@ var StatusSelector = React.createClass({
<button type="button" value="warning" className="btn btn-default navbar-btn alert-warning" onClick={this.handleChange}>Warning</button>
<button type="button" value="danger" className="btn btn-default navbar-btn alert-danger" onClick={this.handleChange}>Danger</button>
<button type="button" value="info" className="btn btn-default navbar-btn alert-info" onClick={this.handleChange}>Info</button>
<input type="text" id="nodeFilter" className="form-control" placeholder="nodename" onKeyUp={this.handleNodeGrep}/>
<input type="text" id="nodeFilter" className="form-control" placeholder="node | service" onKeyUp={this.handleNodeGrep}/>
<input type="text" id="keyFilter" className="form-control" placeholder="key" onKeyUp={this.handleKeyGrep}/>
</form>
</div>
Expand Down Expand Up @@ -208,7 +208,7 @@ var Dashboard = React.createClass({
<table className="table table-bordered">
<thead>
<tr>
<th>node</th>
<th>node | service</th>
<th>address</th>
<th>key</th>
<th className="item_timestamp_col">timestamp</th>
Expand Down
47 changes: 41 additions & 6 deletions dashboard.go
Expand Up @@ -24,6 +24,7 @@ var (
Version string
ExtAssetDir string
Nodes []Node
Services map[string][]string
mutex sync.RWMutex
)

Expand Down Expand Up @@ -126,7 +127,8 @@ func main() {
log.Println("trigger:", trigger)
go watchForTrigger(trigger)
}
go updateNodeList()
go updateNodes()
go updateServices()

log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}
Expand Down Expand Up @@ -205,7 +207,7 @@ func kvApiProxy(w http.ResponseWriter, r *http.Request) {
items := make([]Item, 0, len(kvps))
for _, kv := range kvps {
item := kv.NewItem()
if itemInNodes(&item) {
if itemInCatalog(&item) {
items = append(items, item)
}
}
Expand Down Expand Up @@ -236,7 +238,7 @@ func watchForTrigger(command string) {
currentItem := make(map[string]Item)
for _, kv := range kvps {
item := kv.NewItem()
if !itemInNodes(&item) {
if !itemInCatalog(&item) {
continue
}

Expand All @@ -248,7 +250,7 @@ func watchForTrigger(command string) {
}
for _, kv := range kvps {
item := kv.NewItem()
if !itemInNodes(&item) {
if !itemInCatalog(&item) {
continue
}
if _, exist := currentItem[item.Category]; !exist {
Expand Down Expand Up @@ -336,7 +338,7 @@ func invokePipe(command string, src io.Reader) error {
return cmdErr
}

func updateNodeList() {
func updateNodes() {
var index int64
for {
resp, newIndex, err := callConsulAPI(
Expand All @@ -358,7 +360,28 @@ func updateNodeList() {
}
}

func itemInNodes(item *Item) bool {
func updateServices() {
var index int64
for {
resp, newIndex, err := callConsulAPI(
"/v1/catalog/services?index=" + strconv.FormatInt(index, 10) + "&wait=55s",
)
if err != nil {
log.Println("[error]", err)
time.Sleep(10 * time.Second)
continue
}
index = newIndex
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
mutex.Lock()
dec.Decode(&Services)
mutex.Unlock()
time.Sleep(1 * time.Second)
}
}

func itemInCatalog(item *Item) bool {
mutex.RLock()
defer mutex.RUnlock()
for _, node := range Nodes {
Expand All @@ -367,6 +390,18 @@ func itemInNodes(item *Item) bool {
return true
}
}
for name, tags := range Services {
if item.Node == name {
item.Address = "service"
return true
}
for _, tag := range tags {
if item.Node == fmt.Sprintf("%s.%s", tag, name) {
item.Address = "service"
return true
}
}
}
return false
}

Expand Down

0 comments on commit f9f24b0

Please sign in to comment.