Skip to content

Commit

Permalink
Apply provided site probability to Heartbeat registration (#23)
Browse files Browse the repository at this point in the history
* Add Probability parameter
* Apply provided site probability to Heartbeat registration
* Add test cases to cover probability parameter
  • Loading branch information
stephen-soltesz authored Jul 10, 2024
1 parent 973edb7 commit d4f2f06
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: "1.20"
- run: go test -coverprofile=profile.cov ./...
- run: go test -v -coverprofile=profile.cov ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
Expand Down
15 changes: 15 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func (s *Server) Register(rw http.ResponseWriter, req *http.Request) {
}
param.Geo = record
param.Network = s.ASN.AnnotateIP(param.IPv4)
// Override site probability with user-provided parameter.
// TODO(soltesz): include M-Lab override option
param.Probability = getProbability(req)
r := register.CreateRegisterResponse(param)

// Register the hostname under the organization zone.
Expand Down Expand Up @@ -366,3 +369,15 @@ func getClientIP(req *http.Request) string {
hip, _, _ := net.SplitHostPort(req.RemoteAddr)
return hip
}

func getProbability(req *http.Request) float64 {
prob := req.URL.Query().Get("probability")
if prob == "" {
return 1.0
}
p, err := strconv.ParseFloat(prob, 64)
if err != nil {
return 1.0
}
return p
}
52 changes: 51 additions & 1 deletion handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,57 @@ func TestServer_Register(t *testing.T) {
}{
{
name: "success",
params: "?service=foo&organization=bar&iata=lga&ipv4=192.168.0.1",
params: "?service=foo&organization=bar&iata=lga&ipv4=192.168.0.1&probability=1.0",
Iata: &fakeIataFinder{
findRow: iata.Row{
IATA: "lga",
Latitude: -10,
Longitude: -10,
},
},
Maxmind: &fakeMaxmind{
// NOTE: this riduculous declaration is needed due to anonymous structs in the geoip2 package.
city: &geoip2.City{
Country: struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
}{
IsoCode: "US",
},
Subdivisions: []struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
}{
{IsoCode: "NY", Names: map[string]string{"en": "New York"}},
{IsoCode: "ZZ", Names: map[string]string{"en": "fake thing"}},
},
Location: struct {
AccuracyRadius uint16 `maxminddb:"accuracy_radius"`
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
MetroCode uint `maxminddb:"metro_code"`
TimeZone string `maxminddb:"time_zone"`
}{
Latitude: 41,
Longitude: -73,
},
},
},
ASN: &fakeAsn{
ann: &annotator.Network{
ASNumber: 12345,
},
},
DNS: &fakeDNS{},
wantName: "foo-lga12345-c0a80001.bar.sandbox.measurement-lab.org",
wantCode: http.StatusOK,
},
{
name: "success-probability-invalid",
params: "?service=foo&organization=bar&iata=lga&ipv4=192.168.0.1&probability=invalid",
Iata: &fakeIataFinder{
findRow: iata.Row{
IATA: "lga",
Expand Down
3 changes: 2 additions & 1 deletion internal/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Params struct {
Geo *geoip2.City
Metro iata.Row
Network *annotator.Network
Probability float64
}

// OrgZone generates the organization zone name based the organization and project.
Expand Down Expand Up @@ -101,7 +102,7 @@ func CreateRegisterResponse(p *Params) v0.RegisterResponse {
Machine: machine,
Metro: site[:3],
Project: p.Project,
Probability: 1,
Probability: p.Probability,
Site: site,
Type: "unknown", // should be overridden by node.
Uplink: "unknown", // should be overridden by node.
Expand Down
1 change: 1 addition & 0 deletions internal/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestCreateRegisterResponse(t *testing.T) {
Network: &annotator.Network{
ASNumber: 12345,
},
Probability: 1.0,
},
want: v0.RegisterResponse{
Registration: &v0.Registration{
Expand Down

0 comments on commit d4f2f06

Please sign in to comment.