···7878 // DID operations
7979 UpsertDID(ctx context.Context, did string, bundleNum int) error
8080 GetDIDRecord(ctx context.Context, did string) (*DIDRecord, error)
8181+ GetGlobalDIDInfo(ctx context.Context, did string) (*GlobalDIDInfo, error)
8182 AddBundleDIDs(ctx context.Context, bundleNum int, dids []string) error
8283 GetTotalDIDCount(ctx context.Context) (int64, error)
8384
+83
internal/storage/postgres.go
···16741674 return &record, nil
16751675}
1676167616771677+// GetGlobalDIDInfo retrieves consolidated DID info from 'dids' and 'pds_repos'
16781678+func (p *PostgresDB) GetGlobalDIDInfo(ctx context.Context, did string) (*GlobalDIDInfo, error) {
16791679+ // This query now includes a CTE to find primary endpoints and filters
16801680+ // the 'hosting_on' aggregation to only include repos from those endpoints.
16811681+ query := `
16821682+ WITH primary_endpoints AS (
16831683+ -- First, get the ID of every "primary" PDS.
16841684+ -- A primary PDS is the one with the earliest 'discovered_at' timestamp
16851685+ -- for a given 'server_did'.
16861686+ SELECT DISTINCT ON (COALESCE(server_did, id::text))
16871687+ id
16881688+ FROM endpoints
16891689+ WHERE endpoint_type = 'pds'
16901690+ ORDER BY COALESCE(server_did, id::text), discovered_at ASC
16911691+ )
16921692+ SELECT
16931693+ d.did,
16941694+ d.bundle_numbers,
16951695+ d.created_at,
16961696+ COALESCE(
16971697+ jsonb_agg(
16981698+ jsonb_build_object(
16991699+ 'id', pr.id,
17001700+ 'endpoint_id', pr.endpoint_id,
17011701+ 'endpoint', e.endpoint,
17021702+ 'did', pr.did,
17031703+ 'head', pr.head,
17041704+ 'rev', pr.rev,
17051705+ 'active', pr.active,
17061706+ 'status', pr.status,
17071707+ 'first_seen', pr.first_seen AT TIME ZONE 'UTC',
17081708+ 'last_seen', pr.last_seen AT TIME ZONE 'UTC',
17091709+ 'updated_at', pr.updated_at AT TIME ZONE 'UTC'
17101710+ )
17111711+ ORDER BY pr.last_seen DESC
17121712+ ) FILTER (
17131713+ -- This filter clause ensures we only aggregate repos
17141714+ -- where the endpoint_id is in our list of primary endpoints.
17151715+ WHERE pr.id IS NOT NULL AND pe.id IS NOT NULL
17161716+ ),
17171717+ '[]'::jsonb
17181718+ ) AS hosting_on
17191719+ FROM
17201720+ dids d
17211721+ LEFT JOIN
17221722+ pds_repos pr ON d.did = pr.did
17231723+ LEFT JOIN
17241724+ endpoints e ON pr.endpoint_id = e.id
17251725+ LEFT JOIN
17261726+ -- We join the primary_endpoints CTE. 'pe.id' will be NON-NULL
17271727+ -- only if the repo's endpoint_id (pr.endpoint_id) is a primary one.
17281728+ primary_endpoints pe ON pr.endpoint_id = pe.id
17291729+ WHERE
17301730+ d.did = $1
17311731+ GROUP BY
17321732+ d.did, d.bundle_numbers, d.created_at
17331733+ `
17341734+17351735+ var info GlobalDIDInfo
17361736+ var bundleNumbersJSON []byte
17371737+ var hostingOnJSON []byte
17381738+17391739+ err := p.db.QueryRowContext(ctx, query, did).Scan(
17401740+ &info.DID,
17411741+ &bundleNumbersJSON,
17421742+ &info.CreatedAt,
17431743+ &hostingOnJSON,
17441744+ )
17451745+ if err != nil {
17461746+ return nil, err // This will correctly be sql.ErrNoRows if not in 'dids'
17471747+ }
17481748+17491749+ if err := json.Unmarshal(bundleNumbersJSON, &info.BundleNumbers); err != nil {
17501750+ return nil, fmt.Errorf("failed to unmarshal bundle_numbers: %w", err)
17511751+ }
17521752+17531753+ if err := json.Unmarshal(hostingOnJSON, &info.HostingOn); err != nil {
17541754+ return nil, fmt.Errorf("failed to unmarshal hosting_on: %w", err)
17551755+ }
17561756+17571757+ return &info, nil
17581758+}
17591759+16771760func (p *PostgresDB) AddBundleDIDs(ctx context.Context, bundleNum int, dids []string) error {
16781761 if len(dids) == 0 {
16791762 return nil
+6
internal/storage/types.go
···256256type PDSRepo struct {
257257 ID int64 `json:"id"`
258258 EndpointID int64 `json:"endpoint_id"`
259259+ Endpoint string `json:"endpoint,omitempty"`
259260 DID string `json:"did"`
260261 Head string `json:"head,omitempty"`
261262 Rev string `json:"rev,omitempty"`
···273274 Active bool
274275 Status string
275276}
277277+278278+type GlobalDIDInfo struct {
279279+ DIDRecord
280280+ HostingOn []*PDSRepo `json:"hosting_on"`
281281+}