github.com/m-lab/locate@v0.17.6/siteinfo/siteinfo.go (about)

     1  package siteinfo
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  
     7  	"github.com/m-lab/go/host"
     8  	v2 "github.com/m-lab/locate/api/v2"
     9  )
    10  
    11  // Machines returns a map of machines that Locate knows about. The map values
    12  // are a combination of a machine's heartbeat registration information and
    13  // health informatiom from both heartbeat and Prometheus.
    14  func Machines(msgs map[string]v2.HeartbeatMessage, v url.Values) (map[string]v2.HeartbeatMessage, error) {
    15  	machines := make(map[string]v2.HeartbeatMessage)
    16  
    17  	org := v.Get("org")
    18  	exp := v.Get("exp")
    19  
    20  	if org != "" || exp != "" {
    21  		for k, v := range msgs {
    22  			parts, err := host.Parse(k)
    23  			if err != nil {
    24  				returnError := fmt.Errorf("failed to parse hostname: %s", k)
    25  				return nil, returnError
    26  			}
    27  			if org != "" && exp == "" {
    28  				if org == parts.Org {
    29  					machines[k] = v
    30  				}
    31  			} else if org == "" && exp != "" {
    32  				if exp == parts.Service {
    33  					machines[k] = v
    34  				}
    35  			} else {
    36  				if org == parts.Org && exp == parts.Service {
    37  					machines[k] = v
    38  				}
    39  			}
    40  		}
    41  	} else {
    42  		machines = msgs
    43  	}
    44  
    45  	return machines, nil
    46  
    47  }