Overhaul publicreport storage layer, create unified tables

This is a huge change. I was getting really sick of the split between
nuisance/water tables when more than half of the data they store is
common. I finally bit off the big work of switching it all.

This creates a single unified table, publicreport.report and copies the
existing report data into it. It also ports existing data from the
original tables into the new table.

Along with all of this I also overhauled the system for handling
asynchronous work to use a LISTEN/NOTIFY connection from the database
and a single cache table to avoid ever losing work.
This commit is contained in:
Eli Ribble 2026-03-18 15:36:20 +00:00
parent 2538638c9d
commit 1e071d5ce5
No known key found for this signature in database
109 changed files with 22903 additions and 11713 deletions

View file

@ -3,7 +3,6 @@ package publicreport
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
@ -18,34 +17,25 @@ import (
)
type Water struct {
AccessComments string `db:"access_comments" json:"access_comments"`
AccessGate bool `db:"access_gate" json:"access_gate"`
AccessFence bool `db:"access_fence" json:"access_fence"`
AccessLocked bool `db:"access_locked" json:"access_locked"`
AccessDog bool `db:"access_dog" json:"access_dog"`
AccessOther bool `db:"access_other" json:"access_other"`
Address types.Address `db:"address" json:"address"`
AddressRaw string `db:"address_raw" json:"address_raw"`
Comments string `db:"comments" json:"comments"`
Created time.Time `db:"created" json:"created"`
HasAdult bool `db:"has_adult" json:"has_adult"`
HasBackyardPermission bool `db:"has_backyard_permission" json:"has_backyard_permission"`
HasLarvae bool `db:"has_larvae" json:"has_larvae"`
HasPupae bool `db:"has_pupae" json:"has_pupae"`
ID int32 `db:"id" json:"-"`
Images []types.Image `db:"-" json:"images"`
IsReporterConfidential bool `db:"is_reporter_confidential" json:"is_reporter_confidential"`
IsReporterOwner bool `db:"is_reporter_owner" json:"is_reporter_owner"`
Location types.Location `db:"location" json:"location"`
Owner types.Contact `db:"owner" json:"owner"`
PublicID string `db:"public_id" json:"public_id"`
Reporter types.Contact `db:"reporter" json:"reporter"`
ReporterContactConsent *bool `db:"reporter_contact_consent" json:"reporter_contact_consent"`
Status string `db:"status" json:"status"`
AccessComments string `db:"access_comments" json:"access_comments"`
AccessGate bool `db:"access_gate" json:"access_gate"`
AccessFence bool `db:"access_fence" json:"access_fence"`
AccessLocked bool `db:"access_locked" json:"access_locked"`
AccessDog bool `db:"access_dog" json:"access_dog"`
AccessOther bool `db:"access_other" json:"access_other"`
Comments string `db:"comments" json:"comments"`
HasAdult bool `db:"has_adult" json:"has_adult"`
HasBackyardPermission bool `db:"has_backyard_permission" json:"has_backyard_permission"`
HasLarvae bool `db:"has_larvae" json:"has_larvae"`
HasPupae bool `db:"has_pupae" json:"has_pupae"`
IsReporterConfidential bool `db:"is_reporter_confidential" json:"is_reporter_confidential"`
IsReporterOwner bool `db:"is_reporter_owner" json:"is_reporter_owner"`
Owner types.Contact `db:"owner" json:"owner"`
ReportID int32 `db:"report_id" json:"-"`
}
func WaterReportForOrganization(ctx context.Context, org_id int32) ([]Water, error) {
reports, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
func watersByReportID(ctx context.Context, report_ids []int32) (map[int32]*Water, error) {
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"access_comments",
"access_gate",
@ -53,72 +43,45 @@ func WaterReportForOrganization(ctx context.Context, org_id int32) ([]Water, err
"access_locked",
"access_dog",
"access_other",
"access_gate AS address_raw",
"address_country AS \"address.country\"",
"address_locality AS \"address.locality\"",
"address_number AS \"address.number\"",
"address_postal_code AS \"address.postal_code\"",
"address_region AS \"address.region\"",
"address_street AS \"address.street\"",
"comments",
"created",
"has_adult",
"has_backyard_permission",
"has_larvae",
"has_pupae",
"id",
"is_reporter_confidential",
"is_reporter_owner",
"ST_Y(location::geometry::geometry(point, 4326)) AS \"location.latitude\"",
"ST_X(location::geometry::geometry(point, 4326)) AS \"location.longitude\"",
"owner_email AS \"owner.email\"",
"owner_name AS \"owner.name\"",
"owner_phone AS \"owner.phone\"",
"public_id",
"reporter_email AS \"reporter.email\"",
"reporter_name AS \"reporter.name\"",
"reporter_phone AS \"reporter.phone\"",
"reporter_contact_consent",
"status",
"report_id",
),
sm.From("publicreport.water"),
sm.Where(psql.Quote("publicreport", "water", "organization_id").EQ(psql.Arg(org_id))),
sm.Where(psql.Quote("publicreport", "water", "reviewed").IsNull()),
sm.Where(psql.Quote("report_id").EQ(
psql.Any(report_ids),
)),
), scan.StructMapper[Water]())
if err != nil {
return nil, fmt.Errorf("get reports: %w", err)
return nil, fmt.Errorf("query water: %w", err)
}
report_ids := make([]int32, len(reports))
for i, report := range reports {
report_ids[i] = report.ID
}
images_by_id, err := loadImagesForReportWater(ctx, org_id, report_ids)
if err != nil {
return nil, fmt.Errorf("images for report: %w", err)
}
for i := range reports {
images, ok := images_by_id[reports[i].ID]
if ok {
reports[i].Images = images
} else {
reports[i].Images = []types.Image{}
results := make(map[int32]*Water, len(rows))
for _, row := range rows {
results[row.ReportID] = &Water{
AccessComments: row.AccessComments,
AccessGate: row.AccessGate,
AccessFence: row.AccessFence,
AccessLocked: row.AccessLocked,
AccessDog: row.AccessDog,
AccessOther: row.AccessOther,
Comments: row.Comments,
HasAdult: row.HasAdult,
HasBackyardPermission: row.HasBackyardPermission,
HasLarvae: row.HasLarvae,
HasPupae: row.HasPupae,
IsReporterConfidential: row.IsReporterConfidential,
IsReporterOwner: row.IsReporterOwner,
Owner: row.Owner,
ReportID: row.ReportID,
}
}
return reports, nil
}
func WaterReportForOrganizationCount(ctx context.Context, org_id int32) (uint, error) {
type _Row struct {
Count uint `db:"count"`
}
row, err := bob.One(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"COUNT(*) AS count",
),
sm.From("publicreport.water"),
sm.Where(psql.Quote("publicreport", "water", "organization_id").EQ(psql.Arg(org_id))),
), scan.StructMapper[_Row]())
if err != nil {
return 0, fmt.Errorf("query count: %w", err)
}
return row.Count, nil
return results, nil
}