163 lines
9.1 KiB
Go
163 lines
9.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// CityHealth is a cheap, synchronous topology summary. Expensive content and
|
|
// link checks are intentionally left to scheduler jobs; navigation must not
|
|
// wait for them.
|
|
type CityHealth struct {
|
|
DomainUUID string `json:"domain_uuid"`
|
|
TopologyVersion int64 `json:"topology_version"`
|
|
Quartiers int `json:"quartiers"`
|
|
Streets int `json:"streets"`
|
|
Spots int `json:"spots"`
|
|
EmptySpots int `json:"empty_spots"`
|
|
UnconsolidatedPages int `json:"unconsolidated_pages"`
|
|
OrphanPages int `json:"orphan_pages"`
|
|
BrokenRelations int `json:"broken_relations"`
|
|
GeneratedAtMS int64 `json:"generated_at_ms"`
|
|
}
|
|
|
|
type TopologyEvent struct {
|
|
ID int64 `json:"id"`
|
|
DomainUUID string `json:"domain_uuid"`
|
|
TopologyVersion int64 `json:"topology_version"`
|
|
ActorPrincipalUUID string `json:"actor_principal_uuid"`
|
|
Operation string `json:"operation"`
|
|
TargetType string `json:"target_type"`
|
|
TargetUUID string `json:"target_uuid"`
|
|
BeforeJSON string `json:"before_json"`
|
|
AfterJSON string `json:"after_json"`
|
|
RequestID string `json:"request_id"`
|
|
CreatedAtMS int64 `json:"created_at_ms"`
|
|
}
|
|
|
|
type CityPathContext struct {
|
|
Quartier *CityNodeDetail `json:"quartier,omitempty"`
|
|
Street *CityNodeDetail `json:"street,omitempty"`
|
|
Spot *CityNodeDetail `json:"spot,omitempty"`
|
|
}
|
|
|
|
func (s *Store) GetCityHealth(domainUUID string) (_ CityHealth, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("city health", resultErr) }()
|
|
health := CityHealth{DomainUUID: domainUUID, TopologyVersion: 1, GeneratedAtMS: time.Now().UnixMilli()}
|
|
ctx, cancel := context.WithTimeout(context.Background(), cityReadTimeout)
|
|
defer cancel()
|
|
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
|
if err != nil {
|
|
return health, err
|
|
}
|
|
defer tx.Rollback()
|
|
if err := tx.QueryRowContext(ctx, `SELECT version FROM domain_topology_versions WHERE domain_uuid=`+s.sqlParam(1), domainUUID).Scan(&health.TopologyVersion); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return health, err
|
|
}
|
|
queries := []struct {
|
|
target *int
|
|
query string
|
|
args []any
|
|
}{
|
|
{&health.Quartiers, `SELECT COUNT(*) FROM zones WHERE domain_uuid=` + s.sqlParam(1) + ` AND kind='quartier' AND state!='archived'`, []any{domainUUID}},
|
|
{&health.Streets, `SELECT COUNT(*) FROM zones WHERE domain_uuid=` + s.sqlParam(1) + ` AND kind='street' AND state!='archived'`, []any{domainUUID}},
|
|
{&health.Spots, `SELECT COUNT(*) FROM spots WHERE domain_uuid=` + s.sqlParam(1) + ` AND state!='archived'`, []any{domainUUID}},
|
|
{&health.EmptySpots, `SELECT COUNT(*) FROM spots s WHERE s.domain_uuid=` + s.sqlParam(1) + ` AND s.state!='archived' AND NOT EXISTS(SELECT 1 FROM spot_pages sp JOIN paths p ON p.object_uuid=sp.page_uuid AND p.domain_uuid=s.domain_uuid WHERE sp.spot_uuid=s.uuid) AND NOT EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=s.domain_uuid AND ` + s.pathScopeSQL("s.path_prefix", "p.path") + `)`, []any{domainUUID}},
|
|
{&health.UnconsolidatedPages, `SELECT COUNT(DISTINCT p.object_uuid) FROM paths p JOIN zones st ON st.domain_uuid=p.domain_uuid AND st.kind='street' AND st.state!='archived' AND ` + s.pathScopeSQL("st.path_prefix", "p.path") + ` WHERE p.domain_uuid=` + s.sqlParam(1) + ` AND NOT EXISTS(SELECT 1 FROM spots s JOIN street_spots ss ON ss.spot_uuid=s.uuid AND ss.street_uuid=st.uuid WHERE s.domain_uuid=st.domain_uuid AND s.state!='archived' AND ` + s.pathScopeSQL("s.path_prefix", "p.path") + `)`, []any{domainUUID}},
|
|
{&health.OrphanPages, `SELECT COUNT(DISTINCT p.object_uuid) FROM paths p WHERE p.domain_uuid=` + s.sqlParam(1) + ` AND NOT ` + s.pathScopeSQL("'/u'", "p.path") + ` AND NOT EXISTS(SELECT 1 FROM spot_pages sp JOIN spots s ON s.uuid=sp.spot_uuid AND s.domain_uuid=p.domain_uuid AND s.state!='archived' WHERE sp.page_uuid=p.object_uuid) AND NOT EXISTS(SELECT 1 FROM spots s WHERE s.domain_uuid=p.domain_uuid AND s.state!='archived' AND ` + s.pathScopeSQL("s.path_prefix", "p.path") + `)`, []any{domainUUID}},
|
|
{&health.BrokenRelations, `SELECT COUNT(*) FROM spot_relations r JOIN spots source ON source.uuid=r.source_spot_uuid AND source.domain_uuid=` + s.sqlParam(1) + ` WHERE source.state!='archived' AND NOT EXISTS(SELECT 1 FROM spots target WHERE target.uuid=r.target_spot_uuid AND target.domain_uuid=` + s.sqlParam(2) + ` AND target.state!='archived')`, []any{domainUUID, domainUUID}},
|
|
}
|
|
for _, item := range queries {
|
|
if err := tx.QueryRowContext(ctx, item.query, item.args...).Scan(item.target); err != nil {
|
|
return health, err
|
|
}
|
|
}
|
|
return health, nil
|
|
}
|
|
|
|
func (s *Store) ListTopologyEvents(domainUUID string, afterVersion int64, limit int) (_ []TopologyEvent, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("city events", resultErr) }()
|
|
if limit < 1 {
|
|
limit = 50
|
|
}
|
|
if limit > 200 {
|
|
limit = 200
|
|
}
|
|
rows, err := s.DB.Query(`SELECT id,domain_uuid,topology_version,actor_principal_uuid,operation,target_type,target_uuid,before_json,after_json,request_id,created_at_ms FROM topology_events WHERE domain_uuid=`+s.sqlParam(1)+` AND topology_version>`+s.sqlParam(2)+` ORDER BY topology_version DESC,id DESC LIMIT `+s.sqlParam(3), domainUUID, afterVersion, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
events := []TopologyEvent{}
|
|
for rows.Next() {
|
|
var event TopologyEvent
|
|
if err := rows.Scan(&event.ID, &event.DomainUUID, &event.TopologyVersion, &event.ActorPrincipalUUID, &event.Operation, &event.TargetType, &event.TargetUUID, &event.BeforeJSON, &event.AfterJSON, &event.RequestID, &event.CreatedAtMS); err != nil {
|
|
return nil, err
|
|
}
|
|
events = append(events, event)
|
|
}
|
|
return events, rows.Err()
|
|
}
|
|
|
|
func (s *Store) ResolveCityLocalityPath(domainUUID, kind, id string) (string, error) {
|
|
return s.ResolveCityLocalityPathContext(context.Background(), domainUUID, kind, id)
|
|
}
|
|
|
|
func (s *Store) ResolveCityLocalityPathContext(ctx context.Context, domainUUID, kind, id string) (string, error) {
|
|
if kind == "domain" || kind == "" {
|
|
return "/", nil
|
|
}
|
|
node, err := s.CityNodeContext(ctx, domainUUID, kind, id)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return node.PathPrefix, nil
|
|
}
|
|
|
|
func (s *Store) ResolveCityPathContext(domainUUID, path string) (CityPathContext, error) {
|
|
return s.ResolveCityPathContextWithContext(context.Background(), domainUUID, path)
|
|
}
|
|
|
|
func (s *Store) ResolveCityPathContextWithContext(parent context.Context, domainUUID, path string) (_ CityPathContext, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("city path context", resultErr) }()
|
|
path = normalizePath(path)
|
|
result := CityPathContext{}
|
|
ctx, cancel := context.WithTimeout(parent, cityReadTimeout)
|
|
defer cancel()
|
|
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
defer tx.Rollback()
|
|
var q CityNodeDetail
|
|
err = tx.QueryRowContext(ctx, `SELECT uuid,domain_uuid,kind,COALESCE(parent_uuid,''),name,title,description,state,path_prefix,sort_key,version FROM zones CROSS JOIN (SELECT `+s.sqlParam(1)+` AS candidate) input WHERE domain_uuid=`+s.sqlParam(2)+` AND kind='quartier' AND state!='archived' AND `+s.pathScopeSQL("path_prefix", "input.candidate")+` ORDER BY length(path_prefix) DESC,uuid LIMIT 1`, path, domainUUID).Scan(&q.UUID, &q.DomainUUID, &q.Kind, &q.ParentUUID, &q.Name, &q.Title, &q.Description, &q.State, &q.PathPrefix, &q.SortKey, &q.Version)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return result, err
|
|
}
|
|
if err == sql.ErrNoRows {
|
|
return result, nil
|
|
}
|
|
result.Quartier = &q
|
|
var street CityNodeDetail
|
|
err = tx.QueryRowContext(ctx, `SELECT uuid,domain_uuid,kind,COALESCE(parent_uuid,''),name,title,description,state,path_prefix,sort_key,version FROM zones CROSS JOIN (SELECT `+s.sqlParam(1)+` AS candidate) input WHERE domain_uuid=`+s.sqlParam(2)+` AND kind='street' AND parent_uuid=`+s.sqlParam(3)+` AND state!='archived' AND `+s.pathScopeSQL("path_prefix", "input.candidate")+` ORDER BY length(path_prefix) DESC,uuid LIMIT 1`, path, domainUUID, q.UUID).Scan(&street.UUID, &street.DomainUUID, &street.Kind, &street.ParentUUID, &street.Name, &street.Title, &street.Description, &street.State, &street.PathPrefix, &street.SortKey, &street.Version)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return result, err
|
|
}
|
|
if err == sql.ErrNoRows {
|
|
return result, nil
|
|
}
|
|
result.Street = &street
|
|
var spot CityNodeDetail
|
|
err = tx.QueryRowContext(ctx, `SELECT s.uuid,s.domain_uuid,'spot','',s.name,s.title,s.summary,s.state,s.path_prefix,ss.sort_key,s.version FROM spots s JOIN street_spots ss ON ss.spot_uuid=s.uuid CROSS JOIN (SELECT `+s.sqlParam(1)+` AS candidate) input WHERE s.domain_uuid=`+s.sqlParam(2)+` AND ss.street_uuid=`+s.sqlParam(3)+` AND s.state!='archived' AND `+s.pathScopeSQL("s.path_prefix", "input.candidate")+` ORDER BY length(s.path_prefix) DESC,s.uuid LIMIT 1`, path, domainUUID, street.UUID).Scan(&spot.UUID, &spot.DomainUUID, &spot.Kind, &spot.ParentUUID, &spot.Name, &spot.Title, &spot.Description, &spot.State, &spot.PathPrefix, &spot.SortKey, &spot.Version)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return result, err
|
|
}
|
|
if err == nil {
|
|
spot.ParentUUID = street.UUID
|
|
result.Spot = &spot
|
|
}
|
|
return result, nil
|
|
}
|