Rework publicreport addressing

This adds the ability to link a proper address in the database to the
report and harmonizes the field names with the address table. It also
migrates away from mapbox entirely.

And I fixed the "pool" naming for the publicreports, which are supposed
to be the more generic 'water'.
This commit is contained in:
Eli Ribble 2026-03-09 18:02:22 +00:00
parent 884634a2d7
commit e932c2c473
No known key found for this signature in database
60 changed files with 4511 additions and 5072 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/geocode"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
//"github.com/Gleipnir-Technology/nidus-sync/h3utils"
//"github.com/Gleipnir-Technology/nidus-sync/platform/geom"
//"github.com/Gleipnir-Technology/nidus-sync/platform/text"
@ -53,8 +54,8 @@ func JobCommit(ctx context.Context, file_id int32) error {
return fmt.Errorf("Failed to get all rows of file %d: %w", file_id, err)
}
for _, row := range rows {
a := geocode.Address{
Country: enums.CountrytypeUsa,
a := types.Address{
Country: "usa",
Locality: row.AddressLocality,
Number: row.AddressNumber,
PostalCode: row.AddressPostalCode,
@ -62,7 +63,7 @@ func JobCommit(ctx context.Context, file_id int32) error {
Street: row.AddressStreet,
Unit: "",
}
address, err := geocode.EnsureAddress(ctx, txn, org, a)
address, err := geocode.EnsureAddressWithGeocode(ctx, txn, org, a)
if err != nil {
//return fmt.Errorf("ensure address: %w", err)
if address == nil {

View file

@ -19,6 +19,7 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/platform/geocode"
"github.com/Gleipnir-Technology/nidus-sync/platform/geom"
"github.com/Gleipnir-Technology/nidus-sync/platform/text"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
"github.com/Gleipnir-Technology/nidus-sync/userfile"
"github.com/aarondl/opt/omit"
@ -132,7 +133,7 @@ type jobGeocode struct {
func geocodePool(ctx context.Context, txn bob.Tx, client *stadia.StadiaMaps, job *jobGeocode) error {
pool := job.pool
a := geocode.Address{
a := types.Address{
Number: pool.AddressNumber,
Locality: pool.AddressLocality,
PostalCode: pool.AddressPostalCode,

View file

@ -10,35 +10,22 @@ import (
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/im"
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/h3utils"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
"github.com/stephenafamo/scan"
//"github.com/rs/zerolog/log"
"github.com/uber/h3-go/v4"
)
type Address struct {
Country enums.Countrytype
Locality string
Number string
PostalCode string
Region string
Street string
Unit string
}
type GeocodeResult struct {
Address Address
Address types.Address
Cell h3.Cell
Longitude float64
Latitude float64
}
func (a Address) String() string {
return fmt.Sprintf("%s %s, %s, %s, %s, %s", a.Number, a.Street, a.Locality, a.Region, a.PostalCode, a.Country)
}
var client *stadia.StadiaMaps
func InitializeStadia(key string) {
@ -47,9 +34,9 @@ func InitializeStadia(key string) {
// Either get an address that matches, or create a new address. Either way, return an address
// This will make a call to a structured geocode service, so it's slow.
func EnsureAddress(ctx context.Context, txn bob.Tx, org *models.Organization, a Address) (*models.Address, error) {
func EnsureAddressWithGeocode(ctx context.Context, txn bob.Tx, org *models.Organization, a types.Address) (*models.Address, error) {
address, err := models.Addresses.Query(
models.SelectWhere.Addresses.Country.EQ(a.Country),
models.SelectWhere.Addresses.Country.EQ(a.CountryEnum()),
models.SelectWhere.Addresses.Locality.EQ(a.Locality),
models.SelectWhere.Addresses.Number.EQ(a.Number),
models.SelectWhere.Addresses.PostalCode.EQ(a.PostalCode),
@ -92,7 +79,7 @@ func EnsureAddress(ctx context.Context, txn bob.Tx, org *models.Organization, a
}
return &models.Address{
Country: geo.Address.Country,
Country: geo.Address.CountryEnum(),
Created: created,
H3cell: "",
ID: row.ID,
@ -106,9 +93,9 @@ func EnsureAddress(ctx context.Context, txn bob.Tx, org *models.Organization, a
}, nil
}
func Geocode(ctx context.Context, org *models.Organization, a Address) (GeocodeResult, error) {
func Geocode(ctx context.Context, org *models.Organization, a types.Address) (GeocodeResult, error) {
street := fmt.Sprintf("%s %s", a.Number, a.Street)
country_s := a.Country.String()
country_s := a.Country
/*
sublog := log.With().
Str("street", street).
@ -148,15 +135,10 @@ func Geocode(ctx context.Context, org *models.Organization, a Address) (GeocodeR
if err != nil {
return GeocodeResult{}, fmt.Errorf("failed to convert lat %f lng %f to h3 cell", longitude, latitude)
}
var country enums.Countrytype
country_s = strings.ToLower(feature.Properties.CountryA)
err = country.Scan(country_s)
if err != nil {
return GeocodeResult{}, fmt.Errorf("failed to scan country '%s': %w", country_s, err)
}
return GeocodeResult{
Address: Address{
Country: country,
Address: types.Address{
Country: country_s,
Locality: feature.Properties.Locality,
Number: feature.Properties.HouseNumber,
PostalCode: feature.Properties.PostalCode,

View file

@ -33,23 +33,28 @@ func loadImagesForReportNuisance(ctx context.Context, org_id int32, report_ids [
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"i.storage_uuid AS uuid",
"ST_X(location) AS location.longitude",
"ST_Y(location) AS location.latitude",
"MAX(e.value) FILTER (WHERE e.name = 'Make') AS exif_make",
"MAX(e.value) FILTER (WHERE e.name = 'Model') AS exif_model",
"MAX(e.value) FILTER (WHERE e.name = 'DateTime') AS exif_datetime",
"COALESCE(ST_X(location), 0) AS \"location.longitude\"",
"COALESCE(ST_Y(location), 0) AS \"location.latitude\"",
"COALESCE(MAX(e.value) FILTER (WHERE e.name = 'Make'), '') AS exif_make",
"COALESCE(MAX(e.value) FILTER (WHERE e.name = 'Model'), '') AS exif_model",
"COALESCE(MAX(e.value) FILTER (WHERE e.name = 'DateTime'), '') AS exif_datetime",
"ni.nuisance_id AS nuisance_id",
),
sm.From("publicreport.image").As("i"),
sm.LeftJoin("publicreport.image_exif").As("e").OnEQ(
psql.Quote("r", "id"),
psql.Quote("i", "id"),
psql.Quote("e", "image_id"),
),
sm.InnerJoin("publicreport.nuisance_image").As("ni").OnEQ(
psql.Quote("ni", "image_id"),
psql.Quote("i", "id"),
),
sm.Where(psql.Quote("ni", "nuisance_id").In(psql.Arg(report_ids))),
sm.Where(psql.Quote("ni", "nuisance_id").EQ(psql.Any(report_ids))),
sm.GroupBy(
//psql.Quote("i", "id"),
//psql.Quote("ni", "nuisance_id"),
psql.Raw("i.id, ni.nuisance_id"),
),
), scan.StructMapper[types.Image]())
if err != nil {
return nil, fmt.Errorf("get images: %w", err)

View file

@ -18,45 +18,40 @@ import (
)
type Nuisance struct {
AdditionalInfo string `db:"additional_info"`
Address types.Address `db:"address"`
AddressAsGiven string `db:"address_as_given"`
Created time.Time `db:"created"`
Duration string `db:"duration"`
ID int32 `db:"id"`
Images []types.Image
IsLocationBackyard bool `db:"is_location_backyard"`
IsLocationFrontyard bool `db:"is_location_frontyard"`
IsLocationGarden bool `db:"is_location_garden"`
IsLocationOther bool `db:"is_location_other"`
IsLocationPool bool `db:"is_location_pool"`
Location types.Location `db:"location"`
PublicID string `db:"public_id"`
Reporter Reporter `db:"reporter"`
SourceContainer bool `db:"source_container"`
SourceDescription string `db:"source_description"`
SourceGutter bool `db:"source_gutter"`
SourceStagnant bool `db:"source_stagnant"`
TODDay bool `db:"tod_day"`
TODEarly bool `db:"tod_early"`
TODEvening bool `db:"tod_evening"`
TODNight bool `db:"tod_night"`
}
type Reporter struct {
Email *string `db:"reporter_email"`
Name *string `db:"reporter_name"`
Phone *string `db:"reporter_phone"`
AdditionalInfo string `db:"additional_info" json:"additional_info"`
Address types.Address `db:"address" json:"address"`
AddressRaw string `db:"address_raw" json:"address_raw"`
Created time.Time `db:"created" json:"created"`
Duration string `db:"duration" json:"duration"`
ID int32 `db:"id" json:"-"`
Images []types.Image `json:"images"`
IsLocationBackyard bool `db:"is_location_backyard" json:"is_location_backyard"`
IsLocationFrontyard bool `db:"is_location_frontyard" json:"is_location_frontyard"`
IsLocationGarden bool `db:"is_location_garden" json:"is_location_garden"`
IsLocationOther bool `db:"is_location_other" json:"is_location_other"`
IsLocationPool bool `db:"is_location_pool" json:"is_location_pool"`
Location types.Location `db:"location" json:"location"`
PublicID string `db:"public_id" json:"public_id"`
Reporter types.Contact `db:"reporter" json:"reporter"`
SourceContainer bool `db:"source_container" json:"source_container"`
SourceDescription string `db:"source_description" json:"source_description"`
SourceGutter bool `db:"source_gutter" json:"source_gutter"`
SourceStagnant bool `db:"source_stagnant" json:"source_stagnant"`
TODDay bool `db:"tod_day" json:"tod_day"`
TODEarly bool `db:"tod_early" json:"tod_early"`
TODEvening bool `db:"tod_evening" json:"tod_evening"`
TODNight bool `db:"tod_night" json:"tod_night"`
}
func NuisanceReportForOrganization(ctx context.Context, org_id int32) ([]Nuisance, error) {
reports, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"additional_info",
"address AS address_as_given",
"address_raw AS address_raw",
"address_country AS \"address.country\"",
"address_locality AS \"address.locality\"",
"address_number AS \"address.number\"",
"address_place AS \"address.place\"",
"address_postcode AS \"address.postcode\"",
"address_postal_code AS \"address.postal_code\"",
"address_region AS \"address.region\"",
"address_street AS \"address.street\"",
"created",

View file

@ -193,8 +193,8 @@ func findSomeReport(ctx context.Context, report_id string) (result SomeReport, e
switch row.FoundInTables[0] {
case "nuisance":
return newNuisance(ctx, report_id, int32(t))
case "pool":
return newPool(ctx, report_id, int32(t))
case "water":
return newWater(ctx, report_id, int32(t))
default:
log.Error().Err(e).Str("table_name", row.FoundInTables[0]).Msg("Unrecognized table")
return Nuisance{}, newErrorWithCode("internal-error", fmt.Sprintf("Unrecognized table '%s'", row.FoundInTables[0]))

View file

@ -21,50 +21,50 @@ import (
"github.com/stephenafamo/scan"
)
type Pool struct {
type Water struct {
id int32
publicReportID string
row *models.PublicreportPool
row *models.PublicreportWater
}
func (sr Pool) PublicReportID() string {
func (sr Water) PublicReportID() string {
return sr.publicReportID
}
func (sr Pool) addNotificationEmail(ctx context.Context, txn bob.Tx, email string) *ErrorWithCode {
setter := models.PublicreportNotifyEmailPoolSetter{
func (sr Water) addNotificationEmail(ctx context.Context, txn bob.Tx, email string) *ErrorWithCode {
setter := models.PublicreportNotifyEmailWaterSetter{
Created: omit.From(time.Now()),
Deleted: omitnull.FromPtr[time.Time](nil),
PoolID: omit.From(sr.id),
EmailAddress: omit.From(email),
WaterID: omit.From(sr.id),
}
_, err := models.PublicreportNotifyEmailPools.Insert(&setter).Exec(ctx, txn)
_, err := models.PublicreportNotifyEmailWaters.Insert(&setter).Exec(ctx, txn)
if err != nil {
log.Error().Err(err).Msg("Failed to save new notification email row")
return newInternalError(err, "Failed to save new notification email row")
}
return nil
}
func (sr Pool) addNotificationPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode {
setter := models.PublicreportNotifyPhonePoolSetter{
func (sr Water) addNotificationPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode {
setter := models.PublicreportNotifyPhoneWaterSetter{
Created: omit.From(time.Now()),
Deleted: omitnull.FromPtr[time.Time](nil),
PoolID: omit.From(sr.id),
PhoneE164: omit.From(text.PhoneString(phone)),
WaterID: omit.From(sr.id),
}
_, err := models.PublicreportNotifyPhonePools.Insert(&setter).Exec(ctx, txn)
_, err := models.PublicreportNotifyPhoneWaters.Insert(&setter).Exec(ctx, txn)
if err != nil {
log.Error().Err(err).Msg("Failed to save new notification phone row")
return newInternalError(err, "Failed to save new notification phone row")
}
return nil
}
func (sr Pool) districtID(ctx context.Context) *int32 {
func (sr Water) districtID(ctx context.Context) *int32 {
type _Row struct {
OrganizationID *int32
}
row, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select(
sm.From("publicreport.pool"),
sm.From("publicreport.water"),
sm.Columns("organization_id"),
sm.Where(psql.Quote("public_id").EQ(psql.Arg(sr.publicReportID))),
), scan.StructMapper[_Row]())
@ -74,44 +74,44 @@ func (sr Pool) districtID(ctx context.Context) *int32 {
}
return row.OrganizationID
}
func (sr Pool) reportID() int32 {
func (sr Water) reportID() int32 {
return sr.id
}
func (sr Pool) updateReporterConsent(ctx context.Context, txn bob.Tx, has_consent bool) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportPoolSetter{
func (sr Water) updateReporterConsent(ctx context.Context, txn bob.Tx, has_consent bool) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportWaterSetter{
ReporterContactConsent: omitnull.From(has_consent),
})
}
func (sr Pool) updateReporterEmail(ctx context.Context, txn bob.Tx, email string) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportPoolSetter{
func (sr Water) updateReporterEmail(ctx context.Context, txn bob.Tx, email string) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportWaterSetter{
ReporterEmail: omit.From(email),
})
}
func (sr Pool) updateReporterName(ctx context.Context, txn bob.Tx, name string) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportPoolSetter{
func (sr Water) updateReporterName(ctx context.Context, txn bob.Tx, name string) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportWaterSetter{
ReporterName: omit.From(name),
})
}
func (sr Pool) updateReportCol(ctx context.Context, txn bob.Tx, setter *models.PublicreportPoolSetter) *ErrorWithCode {
func (sr Water) updateReportCol(ctx context.Context, txn bob.Tx, setter *models.PublicreportWaterSetter) *ErrorWithCode {
err := sr.row.Update(ctx, txn, setter)
if err != nil {
log.Error().Err(err).Str("public_id", sr.publicReportID).Int32("report_id", sr.id).Msg("Failed to update report")
return newInternalError(err, "Failed to update pool report in the database")
return newInternalError(err, "Failed to update water report in the database")
}
return nil
}
func (sr Pool) updateReporterPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportPoolSetter{
func (sr Water) updateReporterPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode {
return sr.updateReportCol(ctx, txn, &models.PublicreportWaterSetter{
ReporterPhone: omit.From(text.PhoneString(phone)),
})
}
func newPool(ctx context.Context, public_id string, report_id int32) (Pool, *ErrorWithCode) {
row, err := models.FindPublicreportPool(ctx, db.PGInstance.BobDB, report_id)
func newWater(ctx context.Context, public_id string, report_id int32) (Water, *ErrorWithCode) {
row, err := models.FindPublicreportWater(ctx, db.PGInstance.BobDB, report_id)
if err != nil {
log.Error().Err(err).Msg("Failed to find pool report")
return Pool{}, newInternalError(err, "Failed to find pool report %d: %w", public_id, err)
log.Error().Err(err).Msg("Failed to find water report")
return Water{}, newInternalError(err, "Failed to find water report %d: %w", public_id, err)
}
return Pool{
return Water{
id: report_id,
publicReportID: public_id,
row: row,

View file

@ -1,5 +1,11 @@
package types
import (
"fmt"
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
)
type Address struct {
Country string `db:"country" json:"country"`
Locality string `db:"locality" json:"locality"`
@ -9,3 +15,10 @@ type Address struct {
Street string `db:"street" json:"street"`
Unit string `db:"unit" json:"unit"`
}
func (a Address) String() string {
return fmt.Sprintf("%s %s, %s, %s, %s, %s", a.Number, a.Street, a.Locality, a.Region, a.PostalCode, a.Country)
}
func (a Address) CountryEnum() enums.Countrytype {
return enums.CountrytypeUsa
}

21
platform/types/contact.go Normal file
View file

@ -0,0 +1,21 @@
package types
import (
"encoding/json"
)
type Contact struct {
Email *string `db:"email" json:"-"`
HasEmail bool `json:"has_email"`
HasPhone bool `json:"has_phone"`
Name *string `db:"name"`
Phone *string `db:"phone" json:"-"`
}
func (c *Contact) MarshalJSON() ([]byte, error) {
to_marshal := make(map[string]interface{}, 0)
to_marshal["name"] = c.Name
to_marshal["has_email"] = (c.Email != nil && *c.Email != "")
to_marshal["has_phone"] = (c.Phone != nil && *c.Phone != "")
return json.Marshal(to_marshal)
}