fix(outages): parse portal address at the boundary; strip district suffix
The outages portal appends the district in parentheses, e.g. "Медкадры улица, 1 (Заельцовский район)". That suffix hid the house number from the parser, so every portal address failed house matching and /v1/outages always returned empty. - address: NormalizeText drops a trailing "(...)" group (root fix, benefits every source) - outages: introduce HouseUnit; the ArcGIS adapter parses the (cleaned) address at the boundary and stores the structured unit; MatchingHouse matches on the unit (with a parse fallback for sources that only provide text) - config: portal.url is now optional (core runs without the events module) - httpapi: always return "outages": [] (no more omitempty); report "detail" when the events module is not configured - tests: regression coverage for the portal district format
This commit is contained in:
@@ -15,13 +15,26 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.totmin.ru/en2zmax/addrkit/internal/domain/address"
|
||||
"git.totmin.ru/en2zmax/addrkit/internal/domain/outages"
|
||||
)
|
||||
|
||||
// reDistrictSuffix matches the administrative suffix the portal appends to its
|
||||
// display address, e.g. "Медкадры улица, 1 (Заельцовский район)". It must be
|
||||
// removed before the address is parsed, otherwise the trailing district text
|
||||
// hides the house number. The district is available separately as a field.
|
||||
var reDistrictSuffix = regexp.MustCompile(`\s*\([^)]*\)\s*$`)
|
||||
|
||||
// cleanAddress strips the portal's district suffix and surrounding spaces.
|
||||
func cleanAddress(s string) string {
|
||||
return strings.TrimSpace(reDistrictSuffix.ReplaceAllString(s, ""))
|
||||
}
|
||||
|
||||
// Client is a bounded HTTP client for the portal.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
@@ -127,8 +140,9 @@ func parseResponse(body []byte) ([]outages.Outage, error) {
|
||||
|
||||
out := make([]outages.Outage, 0, len(pr.Features))
|
||||
for _, feat := range pr.Features {
|
||||
addr := cleanAddress(addressOf(feat.Attributes, feat.Value))
|
||||
o := outages.Outage{
|
||||
Address: addressOf(feat.Attributes, feat.Value),
|
||||
Address: addr,
|
||||
Type: lookup(maps, "type_id", feat.Attributes),
|
||||
System: lookup(maps, "system_id", feat.Attributes),
|
||||
Time: lookup(maps, "time_category", feat.Attributes),
|
||||
@@ -136,6 +150,9 @@ func parseResponse(body []byte) ([]outages.Outage, error) {
|
||||
EndDate: stringOf(feat.Attributes["end_date"]),
|
||||
}
|
||||
o.Action = actionFor(o.System)
|
||||
// Parse the address at the boundary so the domain matches on structure
|
||||
// and never depends on this portal's text format.
|
||||
o.Unit = outages.HouseUnitOf(address.Parse(addr))
|
||||
out = append(out, o)
|
||||
}
|
||||
return out, nil
|
||||
|
||||
@@ -66,3 +66,29 @@ func TestQueryRetriesThenFails(t *testing.T) {
|
||||
t.Errorf("calls = %d, want 3 (1 + 2 retries)", calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQueryStripsDistrictSuffix is a regression test: the portal appends the
|
||||
// district in parentheses, which used to hide the house number from the parser.
|
||||
func TestQueryStripsDistrictSuffix(t *testing.T) {
|
||||
const env = `{"features":[{"value":"Медкадры улица, 1 (Заельцовский район)","attributes":{"address":"Медкадры улица, 1 (Заельцовский район)"}}]}`
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(env))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
c := New(ts.URL, time.Second, 0)
|
||||
rows, err := c.Query(context.Background(), "Медкадры улица 1")
|
||||
if err != nil {
|
||||
t.Fatalf("Query: %v", err)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("rows = %d, want 1", len(rows))
|
||||
}
|
||||
if rows[0].Address != "Медкадры улица, 1" {
|
||||
t.Errorf("district suffix not stripped: %q", rows[0].Address)
|
||||
}
|
||||
if rows[0].Unit.House != "1" {
|
||||
t.Errorf("house not parsed at the boundary: %+v", rows[0].Unit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,9 +79,10 @@ type outagesResponse struct {
|
||||
Status address.Status `json:"status"`
|
||||
Address string `json:"address,omitempty"`
|
||||
District string `json:"district,omitempty"`
|
||||
Outages []outages.Outage `json:"outages,omitempty"`
|
||||
Outages []outages.Outage `json:"outages"`
|
||||
AsOf string `json:"as_of,omitempty"`
|
||||
DataVersion string `json:"data_version,omitempty"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) handleOutages(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -97,6 +98,7 @@ func (s *Server) handleOutages(w http.ResponseWriter, r *http.Request) {
|
||||
Address: res.Address,
|
||||
District: res.District,
|
||||
DataVersion: res.DataVersion,
|
||||
Outages: []outages.Outage{},
|
||||
}
|
||||
if res.Status != address.StatusFound {
|
||||
// No canonical address => nothing to query. Return the resolver's
|
||||
@@ -104,6 +106,13 @@ func (s *Server) handleOutages(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
return
|
||||
}
|
||||
if s.outages == nil {
|
||||
// The events module is optional and off when portal.url is empty.
|
||||
out.Status = address.StatusInfra
|
||||
out.Detail = "outages source is not configured"
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
return
|
||||
}
|
||||
|
||||
raw, err := s.outages.Query(ctx, res.Address)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user