Get back to compiling, but using new jet for publicreport

This was an epically long change, and a terrible idea, but it compiles.
This was essentially a cascade that came about because I can't blend jet
and bob in the same transaction. In for a penny, I guess...
This commit is contained in:
Eli Ribble 2026-05-07 10:39:17 +00:00
parent a95e44cf42
commit fcd95f1a25
No known key found for this signature in database
65 changed files with 3129 additions and 3457 deletions

View file

@ -3,20 +3,14 @@ package geocode
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
"github.com/Gleipnir-Technology/bob/dialect/psql/im"
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
//bobtypes "github.com/Gleipnir-Technology/bob/types"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/h3utils"
"github.com/Gleipnir-Technology/nidus-sync/db"
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
querypublic "github.com/Gleipnir-Technology/nidus-sync/db/query/public"
platformaddress "github.com/Gleipnir-Technology/nidus-sync/platform/address"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
"github.com/rs/zerolog/log"
"github.com/stephenafamo/scan"
//"github.com/rs/zerolog/log"
)
type _rowWithID struct {
@ -24,157 +18,33 @@ type _rowWithID struct {
}
// Ensure the provided address exists. If it doesn't add it to the database.
func EnsureAddress(ctx context.Context, txn bob.Executor, a types.Address) (*models.Address, error) {
address, err := models.Addresses.Query(
models.SelectWhere.Addresses.Gid.EQ(a.GID),
).One(ctx, txn)
if err == nil {
return address, nil
}
id, err := insertAddress(ctx, txn, a)
func EnsureAddress(ctx context.Context, txn db.Ex, a types.Address) (types.Address, error) {
existing, err := querypublic.AddressFromGID(ctx, txn, a.GID)
if err != nil {
return nil, fmt.Errorf("insert address: %w", err)
return types.Address{}, fmt.Errorf("query address from gid: %w", err)
}
return &models.Address{
Country: a.Country,
Created: time.Now(),
Gid: a.GID,
H3cell: "",
ID: *id,
Locality: a.Locality,
Location: "",
PostalCode: a.PostalCode,
Street: a.Street,
Unit: a.Unit,
Region: a.Region,
Number: a.Number,
}, nil
if existing != nil {
return types.AddressFromModel(*existing), nil
}
addr, err := platformaddress.InsertAddress(ctx, txn, a)
if err != nil {
return types.Address{}, fmt.Errorf("insert address: %w", err)
}
return addr, nil
}
func ensureAddressFromFeature(ctx context.Context, txn bob.Executor, feature stadia.GeocodeFeature) (int32, error) {
func ensureAddressFromFeature(ctx context.Context, txn db.Ex, feature stadia.GeocodeFeature) (types.Address, error) {
var result types.Address
if feature.Geometry.Type != "Point" {
return 0, fmt.Errorf("Can't hanlde stadia geometry %s", feature.Geometry.Type)
return result, fmt.Errorf("Can't hanlde stadia geometry %s", feature.Geometry.Type)
}
lat := feature.Geometry.Coordinates[1]
lng := feature.Geometry.Coordinates[0]
cell, err := h3utils.GetCell(lng, lat, 15)
existing, err := querypublic.AddressFromGID(ctx, txn, feature.Properties.GID)
if err != nil {
return 0, fmt.Errorf("failed to convert lat %f lng %f to h3 cell", lat, lng)
return types.Address{}, fmt.Errorf("query address from gid: %w", err)
}
a, err := models.Addresses.Query(
models.SelectWhere.Addresses.Gid.EQ(feature.Properties.GID),
).One(ctx, txn)
if err != nil && err.Error() != "sql: no rows in result set" {
return 0, fmt.Errorf("query address: %w", err)
if existing != nil {
return types.AddressFromModel(*existing), nil
}
if err == nil {
return a.ID, nil
}
query := addressQuery()
query.Apply(
im.Values(
psql.Arg(feature.CountryCode()),
psql.Arg(time.Now()),
psql.Arg(feature.Properties.GID),
psql.Arg(cell.String()),
psql.Raw("DEFAULT"),
psql.Arg(feature.Locality()),
psql.F("ST_Point", lng, lat, 4326),
psql.Arg(feature.Number()),
psql.Arg(feature.PostalCode()),
psql.Arg(feature.Region()),
psql.Arg(feature.Street()),
psql.Raw("''"),
),
im.OnConflict("gid").DoNothing(),
)
row, err := bob.One(ctx, txn, query, scan.StructMapper[_rowWithID]())
log.Info().Int32("id", row.ID).Msg("inserted address")
if err != nil {
return 0, fmt.Errorf("insert: %w", err)
}
return row.ID, nil
}
func insertAddress(ctx context.Context, txn bob.Executor, address types.Address) (*int32, error) {
lng := address.Location.Longitude
lat := address.Location.Latitude
cell, err := h3utils.GetCell(lng, lat, 15)
if err != nil {
return nil, fmt.Errorf("failed to convert lat %f lng %f to h3 cell", lat, lng)
}
query := addressQuery()
query.Apply(
im.Values(
psql.Arg(address.Country),
psql.Arg(time.Now()),
psql.Arg(address.GID),
psql.Arg(cell),
psql.Raw("DEFAULT"),
psql.Arg(address.Locality),
psql.F("ST_Point", address.Location.Longitude, address.Location.Latitude, 4326),
psql.Arg(address.Number),
psql.Arg(address.PostalCode),
psql.Arg(address.Region),
psql.Arg(address.Street),
psql.Raw("''"),
),
)
row, err := bob.One(ctx, txn, query, scan.StructMapper[_rowWithID]())
if err != nil {
return nil, fmt.Errorf("insert: %w", err)
}
return &row.ID, nil
}
func insertAddresses(ctx context.Context, txn bob.Executor, features []stadia.GeocodeFeature) ([]types.Address, error) {
query := addressQuery()
gids := make([]string, len(features))
for i, feature := range features {
gids[i] = feature.Properties.GID
lng := feature.Geometry.Coordinates[0]
lat := feature.Geometry.Coordinates[1]
cell, err := h3utils.GetCell(lng, lat, 15)
if err != nil {
return nil, fmt.Errorf("failed to convert lat %f lng %f to h3 cell", lat, lng)
}
query.Apply(
im.Values(
psql.Arg(feature.CountryCode()),
psql.Arg(time.Now()),
psql.Arg(feature.Properties.GID),
psql.Arg(cell.String()),
psql.Raw("DEFAULT"),
psql.Arg(feature.Locality()),
psql.F("ST_Point", lng, lat, 4326),
psql.Arg(feature.Number()),
psql.Arg(feature.PostalCode()),
psql.Arg(feature.Region()),
psql.Arg(feature.Street()),
psql.Raw("''"),
),
im.OnConflict("gid").DoNothing(),
)
}
_, err := bob.All(ctx, txn, query, scan.StructMapper[_rowWithID]())
if err != nil {
return nil, fmt.Errorf("insert: %w", err)
}
addresses, err := models.Addresses.Query(
sm.Where(
models.Addresses.Columns.Gid.EQ(psql.Any(gids)),
),
).All(ctx, txn)
if err != nil {
return nil, fmt.Errorf("query by gid: %w", err)
}
results := make([]types.Address, len(addresses))
for i, address := range addresses {
results[i] = types.AddressFromModel(address)
}
return results, nil
}
func addressQuery() bob.BaseQuery[*dialect.InsertQuery] {
return psql.Insert(
im.Into("address", "country", "created", "gid", "h3cell", "id", "locality", "location", "number_", "postal_code", "region", "street", "unit"),
im.Returning("id"),
)
return platformaddress.InsertAddressFeature(ctx, txn, feature)
}

View file

@ -30,7 +30,7 @@ func ByGID(ctx context.Context, gid string) (*GeocodeResult, error) {
if err != nil {
return nil, fmt.Errorf("latlngtocell: %w", err)
}
id, err := ensureAddressFromFeature(ctx, db.PGInstance.BobDB, feature)
addr, err := ensureAddressFromFeature(ctx, db.PGInstance.PGXPool, feature)
if err != nil {
return nil, fmt.Errorf("insert address: %w", err)
}
@ -38,7 +38,7 @@ func ByGID(ctx context.Context, gid string) (*GeocodeResult, error) {
Address: types.Address{
Country: feature.Properties.Context.ISO3166A3,
GID: feature.Properties.GID,
ID: &id,
ID: addr.ID,
Locality: feature.Properties.Context.WhosOnFirst.Locality.Name,
Location: &location,
Number: feature.Properties.AddressComponents.Number,

View file

@ -17,6 +17,7 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/stadia/table"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/h3utils"
platformaddress "github.com/Gleipnir-Technology/nidus-sync/platform/address"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
//"github.com/aarondl/opt/omit"
@ -86,7 +87,7 @@ func GeocodeRaw(ctx context.Context, org *models.Organization, address string) (
if err != nil {
return nil, fmt.Errorf("client raw geocode failure on %s: %w", address, err)
}
addresses, err := insertAddresses(ctx, db.PGInstance.BobDB, resp.Features)
addresses, err := platformaddress.InsertAddresses(ctx, db.PGInstance.PGXPool, resp.Features)
if err != nil {
return nil, fmt.Errorf("insert addresses: %w", err)
}
@ -106,7 +107,7 @@ func GeocodeStructured(ctx context.Context, org *models.Organization, a types.Ad
if err != nil {
return nil, fmt.Errorf("client structured geocode failure on %s: %w", a.String(), err)
}
addresses, err := insertAddresses(ctx, db.PGInstance.BobDB, resp.Features)
addresses, err := platformaddress.InsertAddresses(ctx, db.PGInstance.PGXPool, resp.Features)
if err != nil {
return nil, fmt.Errorf("insert addresses: %w", err)
}
@ -139,7 +140,7 @@ func ReverseGeocode(ctx context.Context, location types.Location) (*GeocodeResul
if err != nil {
return nil, fmt.Errorf("client reverse geocode failure on %s: %w", location.String(), err)
}
addresses, err := insertAddresses(ctx, db.PGInstance.BobDB, resp.Features)
addresses, err := platformaddress.InsertAddresses(ctx, db.PGInstance.PGXPool, resp.Features)
if err != nil {
return nil, fmt.Errorf("insert addresses: %w", err)
}
@ -155,7 +156,7 @@ func ReverseGeocodeClosest(ctx context.Context, location types.Location) (*Geoco
if err != nil {
return nil, fmt.Errorf("client reverse geocode failure on %s: %w", location.String(), err)
}
addresses, err := insertAddresses(ctx, db.PGInstance.BobDB, resp.Features)
addresses, err := platformaddress.InsertAddresses(ctx, db.PGInstance.PGXPool, resp.Features)
if err != nil {
return nil, fmt.Errorf("insert addresses: %w", err)
}