Skip to content

Commit c15d205

Browse files
committed
feat(model): bulk loader for probe-observed egress country codes
Mirrors GetAllProviderEgressHealthCounts. The provider-count loop runs over the whole provider population, so this has to be one query, not one per provider. Absent means unobserved, so the two-value lookup fails closed.
1 parent 8fcc255 commit c15d205

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

model/provider_egress_location_model.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,43 @@ func GetProviderEgressLocation(ctx context.Context, clientId server.Id) *Provide
278278
return e
279279
}
280280

281+
// GetAllProviderEgressCountryCodes reads every provider's latest observed
282+
// egress country in one query, for callers that need to check many providers
283+
// in a single pass. Mirrors GetAllProviderEgressHealthCounts: the counting
284+
// loop in UpdateClientLocations runs over the whole provider population, so a
285+
// per-provider query there would be one round trip per provider.
286+
//
287+
// Codes are lowercased so callers can compare directly against
288+
// location.country_code without normalising at each site.
289+
//
290+
// A provider with no observed location is ABSENT from the map rather than
291+
// present with an empty string. Callers use the two-value lookup and treat
292+
// absence as "not verified", which fails closed.
293+
func GetAllProviderEgressCountryCodes(ctx context.Context) map[server.Id]string {
294+
countryCodes := map[server.Id]string{}
295+
296+
server.Db(ctx, func(conn server.PgConn) {
297+
result, err := conn.Query(
298+
ctx,
299+
`
300+
SELECT client_id, country_code
301+
FROM provider_egress_location
302+
WHERE country_code IS NOT NULL AND country_code != ''
303+
`,
304+
)
305+
server.WithPgResult(result, err, func() {
306+
for result.Next() {
307+
var clientId server.Id
308+
var countryCode string
309+
server.Raise(result.Scan(&clientId, &countryCode))
310+
countryCodes[clientId] = strings.ToLower(countryCode)
311+
}
312+
})
313+
})
314+
315+
return countryCodes
316+
}
317+
281318
// GetFreshProviderEgressLocation is GetProviderEgressLocation, filtered to
282319
// entries probed within maxAge. The cutoff is computed in Go and bound as a
283320
// parameter: observed_at is a naive timestamp holding utc, and comparing it

model/provider_egress_location_model_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,30 @@ func TestProviderEgressLocationVerdictDefaults(t *testing.T) {
745745
connect.AssertEqual(t, stored.Assurance, ProviderEgressAssuranceDirect)
746746
})
747747
}
748+
749+
func TestGetAllProviderEgressCountryCodes(t *testing.T) {
750+
server.DefaultTestEnv().Run(t, func(t testing.TB) {
751+
ctx := context.Background()
752+
753+
observed := server.NewId()
754+
unobserved := server.NewId()
755+
756+
SetProviderEgressLocation(ctx, &ProviderEgressLocation{
757+
ClientId: observed,
758+
CountryCode: "GB",
759+
Verdict: "verified",
760+
ObservedAt: server.NowUtc(),
761+
})
762+
763+
codes := GetAllProviderEgressCountryCodes(ctx)
764+
765+
// observed providers come back lowercased, so callers can compare
766+
// against location.country_code without normalising at each site
767+
assert.Equal(t, codes[observed], "gb")
768+
769+
// a provider with no observed location is ABSENT, not "".
770+
// Callers rely on the two-value lookup to fail closed.
771+
_, ok := codes[unobserved]
772+
assert.Equal(t, ok, false)
773+
})
774+
}

0 commit comments

Comments
 (0)