ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m48s
ci / test (pull_request) Successful in 12m42s
ci / build (pull_request) Canceled after 30s
ci / image (pull_request) Canceled after 0s
Regions get an optional location. Platform admins set it for platform regions and org admins for their own, by clicking the map on the regions page or entering the coordinates; PATCH /org/regions/:id and /admin/regions/:id set or remove it. The monitor form shows the located regions on a map from the self-hosted ZB-Server Maps: a click on a marker ticks or unticks the region, and the checkbox list stays the source of truth. MapLibre loads only on pages with a map, and the map stays hidden if the map server cannot be reached. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
478 lines
11 KiB
Go
478 lines
11 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: agents.sql
|
|
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const createAgent = `-- name: CreateAgent :one
|
|
INSERT INTO agents (org_id, kind, name, region_id, token_hash, prefix, allow_private)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING id, org_id, kind, name, token_hash, prefix, last_seen_at, release, created_at, region_id, allow_private
|
|
`
|
|
|
|
type CreateAgentParams struct {
|
|
OrgID *uuid.UUID
|
|
Kind string
|
|
Name string
|
|
RegionID *uuid.UUID
|
|
TokenHash []byte
|
|
Prefix string
|
|
AllowPrivate bool
|
|
}
|
|
|
|
func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent, error) {
|
|
row := q.db.QueryRow(ctx, createAgent,
|
|
arg.OrgID,
|
|
arg.Kind,
|
|
arg.Name,
|
|
arg.RegionID,
|
|
arg.TokenHash,
|
|
arg.Prefix,
|
|
arg.AllowPrivate,
|
|
)
|
|
var i Agent
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.TokenHash,
|
|
&i.Prefix,
|
|
&i.LastSeenAt,
|
|
&i.Release,
|
|
&i.CreatedAt,
|
|
&i.RegionID,
|
|
&i.AllowPrivate,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createRegion = `-- name: CreateRegion :one
|
|
INSERT INTO regions (org_id, key, name, latitude, longitude) VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING key, name, created_at, id, org_id, latitude, longitude
|
|
`
|
|
|
|
type CreateRegionParams struct {
|
|
OrgID *uuid.UUID
|
|
Key string
|
|
Name string
|
|
Latitude *float64
|
|
Longitude *float64
|
|
}
|
|
|
|
func (q *Queries) CreateRegion(ctx context.Context, arg CreateRegionParams) (Region, error) {
|
|
row := q.db.QueryRow(ctx, createRegion,
|
|
arg.OrgID,
|
|
arg.Key,
|
|
arg.Name,
|
|
arg.Latitude,
|
|
arg.Longitude,
|
|
)
|
|
var i Region
|
|
err := row.Scan(
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.CreatedAt,
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Latitude,
|
|
&i.Longitude,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteAgent = `-- name: DeleteAgent :execrows
|
|
DELETE FROM agents WHERE id = $1 AND org_id IS NOT DISTINCT FROM $2::uuid
|
|
`
|
|
|
|
type DeleteAgentParams struct {
|
|
ID uuid.UUID
|
|
OrgID *uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) DeleteAgent(ctx context.Context, arg DeleteAgentParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteAgent, arg.ID, arg.OrgID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteRegion = `-- name: DeleteRegion :execrows
|
|
DELETE FROM regions WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteRegion(ctx context.Context, id uuid.UUID) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteRegion, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const ensurePlatformRegion = `-- name: EnsurePlatformRegion :one
|
|
INSERT INTO regions (org_id, key, name)
|
|
VALUES (NULL, $1, coalesce(nullif($2::text, ''), $1))
|
|
ON CONFLICT ON CONSTRAINT regions_owner_key_key
|
|
DO UPDATE SET name = CASE WHEN $2::text = '' THEN regions.name ELSE EXCLUDED.name END
|
|
RETURNING key, name, created_at, id, org_id, latitude, longitude
|
|
`
|
|
|
|
type EnsurePlatformRegionParams struct {
|
|
Key string
|
|
Name string
|
|
}
|
|
|
|
// An empty name names a new region after its key and keeps an existing name.
|
|
func (q *Queries) EnsurePlatformRegion(ctx context.Context, arg EnsurePlatformRegionParams) (Region, error) {
|
|
row := q.db.QueryRow(ctx, ensurePlatformRegion, arg.Key, arg.Name)
|
|
var i Region
|
|
err := row.Scan(
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.CreatedAt,
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Latitude,
|
|
&i.Longitude,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getAgentByHash = `-- name: GetAgentByHash :one
|
|
SELECT a.id, a.org_id, a.kind, a.name, a.token_hash, a.prefix, a.last_seen_at, a.release, a.created_at, a.region_id, a.allow_private, r.key AS region_key, r.name AS region_name
|
|
FROM agents a
|
|
LEFT JOIN regions r ON r.id = a.region_id
|
|
WHERE a.token_hash = $1
|
|
`
|
|
|
|
type GetAgentByHashRow struct {
|
|
ID uuid.UUID
|
|
OrgID *uuid.UUID
|
|
Kind string
|
|
Name string
|
|
TokenHash []byte
|
|
Prefix string
|
|
LastSeenAt *time.Time
|
|
Release *string
|
|
CreatedAt time.Time
|
|
RegionID *uuid.UUID
|
|
AllowPrivate bool
|
|
RegionKey *string
|
|
RegionName *string
|
|
}
|
|
|
|
// System scope.
|
|
func (q *Queries) GetAgentByHash(ctx context.Context, tokenHash []byte) (GetAgentByHashRow, error) {
|
|
row := q.db.QueryRow(ctx, getAgentByHash, tokenHash)
|
|
var i GetAgentByHashRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.TokenHash,
|
|
&i.Prefix,
|
|
&i.LastSeenAt,
|
|
&i.Release,
|
|
&i.CreatedAt,
|
|
&i.RegionID,
|
|
&i.AllowPrivate,
|
|
&i.RegionKey,
|
|
&i.RegionName,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getRegion = `-- name: GetRegion :one
|
|
SELECT key, name, created_at, id, org_id, latitude, longitude FROM regions WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetRegion(ctx context.Context, id uuid.UUID) (Region, error) {
|
|
row := q.db.QueryRow(ctx, getRegion, id)
|
|
var i Region
|
|
err := row.Scan(
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.CreatedAt,
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Latitude,
|
|
&i.Longitude,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listAgents = `-- name: ListAgents :many
|
|
SELECT a.id, a.org_id, a.kind, a.name, a.token_hash, a.prefix, a.last_seen_at, a.release, a.created_at, a.region_id, a.allow_private, r.key AS region_key, r.name AS region_name
|
|
FROM agents a
|
|
LEFT JOIN regions r ON r.id = a.region_id
|
|
WHERE a.org_id IS NOT DISTINCT FROM $1::uuid
|
|
ORDER BY a.kind, r.name, a.name
|
|
`
|
|
|
|
type ListAgentsRow struct {
|
|
ID uuid.UUID
|
|
OrgID *uuid.UUID
|
|
Kind string
|
|
Name string
|
|
TokenHash []byte
|
|
Prefix string
|
|
LastSeenAt *time.Time
|
|
Release *string
|
|
CreatedAt time.Time
|
|
RegionID *uuid.UUID
|
|
AllowPrivate bool
|
|
RegionKey *string
|
|
RegionName *string
|
|
}
|
|
|
|
// Agents owned by org, or platform agents when org is NULL.
|
|
func (q *Queries) ListAgents(ctx context.Context, orgID *uuid.UUID) ([]ListAgentsRow, error) {
|
|
rows, err := q.db.Query(ctx, listAgents, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListAgentsRow{}
|
|
for rows.Next() {
|
|
var i ListAgentsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.TokenHash,
|
|
&i.Prefix,
|
|
&i.LastSeenAt,
|
|
&i.Release,
|
|
&i.CreatedAt,
|
|
&i.RegionID,
|
|
&i.AllowPrivate,
|
|
&i.RegionKey,
|
|
&i.RegionName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listPlatformRegions = `-- name: ListPlatformRegions :many
|
|
SELECT key, name, created_at, id, org_id, latitude, longitude FROM regions WHERE org_id IS NULL ORDER BY name, key
|
|
`
|
|
|
|
func (q *Queries) ListPlatformRegions(ctx context.Context) ([]Region, error) {
|
|
rows, err := q.db.Query(ctx, listPlatformRegions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Region{}
|
|
for rows.Next() {
|
|
var i Region
|
|
if err := rows.Scan(
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.CreatedAt,
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Latitude,
|
|
&i.Longitude,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listVisibleRegions = `-- name: ListVisibleRegions :many
|
|
SELECT key, name, created_at, id, org_id, latitude, longitude FROM regions ORDER BY org_id IS NULL, name, key
|
|
`
|
|
|
|
// Row-level security limits this to platform regions and the scope's org;
|
|
// the org's own regions come first.
|
|
func (q *Queries) ListVisibleRegions(ctx context.Context) ([]Region, error) {
|
|
rows, err := q.db.Query(ctx, listVisibleRegions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Region{}
|
|
for rows.Next() {
|
|
var i Region
|
|
if err := rows.Scan(
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.CreatedAt,
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Latitude,
|
|
&i.Longitude,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const orgOwnerEmails = `-- name: OrgOwnerEmails :many
|
|
SELECT u.email FROM memberships m JOIN users u ON u.id = m.user_id
|
|
WHERE m.org_id = $1 AND m.is_owner AND u.disabled_at IS NULL
|
|
`
|
|
|
|
// System scope.
|
|
func (q *Queries) OrgOwnerEmails(ctx context.Context, orgID uuid.UUID) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, orgOwnerEmails, orgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var email string
|
|
if err := rows.Scan(&email); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, email)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const orgServesOwnPages = `-- name: OrgServesOwnPages :one
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM agents
|
|
WHERE org_id = $1 AND kind = 'statuspage' AND last_seen_at IS NOT NULL
|
|
)
|
|
`
|
|
|
|
// An organization serves its status pages itself once one of its own
|
|
// status page servers has connected.
|
|
func (q *Queries) OrgServesOwnPages(ctx context.Context, orgID *uuid.UUID) (bool, error) {
|
|
row := q.db.QueryRow(ctx, orgServesOwnPages, orgID)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const regionsToWatch = `-- name: RegionsToWatch :many
|
|
SELECT r.id, r.name, r.org_id, o.name AS org_name
|
|
FROM regions r
|
|
LEFT JOIN orgs o ON o.id = r.org_id
|
|
WHERE EXISTS (SELECT 1 FROM agents a WHERE a.kind = 'checker' AND a.region_id = r.id)
|
|
AND EXISTS (SELECT 1 FROM monitor_regions mr JOIN monitors m ON m.id = mr.monitor_id
|
|
WHERE mr.region_id = r.id AND m.enabled)
|
|
ORDER BY r.name
|
|
`
|
|
|
|
type RegionsToWatchRow struct {
|
|
ID uuid.UUID
|
|
Name string
|
|
OrgID *uuid.UUID
|
|
OrgName *string
|
|
}
|
|
|
|
// System scope: regions that have checkers and enabled monitors, so a
|
|
// region without a live checker leaves monitors unchecked.
|
|
func (q *Queries) RegionsToWatch(ctx context.Context) ([]RegionsToWatchRow, error) {
|
|
rows, err := q.db.Query(ctx, regionsToWatch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []RegionsToWatchRow{}
|
|
for rows.Next() {
|
|
var i RegionsToWatchRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.OrgID,
|
|
&i.OrgName,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const setAgentAllowPrivate = `-- name: SetAgentAllowPrivate :execrows
|
|
UPDATE agents SET allow_private = $1
|
|
WHERE id = $2 AND kind = 'checker' AND org_id = $3
|
|
`
|
|
|
|
type SetAgentAllowPrivateParams struct {
|
|
AllowPrivate bool
|
|
ID uuid.UUID
|
|
OrgID *uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) SetAgentAllowPrivate(ctx context.Context, arg SetAgentAllowPrivateParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setAgentAllowPrivate, arg.AllowPrivate, arg.ID, arg.OrgID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const touchAgent = `-- name: TouchAgent :exec
|
|
UPDATE agents SET last_seen_at = now(), release = $2
|
|
WHERE id = $1 AND (last_seen_at IS NULL OR last_seen_at < now() - interval '30 seconds' OR release IS DISTINCT FROM $2)
|
|
`
|
|
|
|
type TouchAgentParams struct {
|
|
ID uuid.UUID
|
|
Release *string
|
|
}
|
|
|
|
func (q *Queries) TouchAgent(ctx context.Context, arg TouchAgentParams) error {
|
|
_, err := q.db.Exec(ctx, touchAgent, arg.ID, arg.Release)
|
|
return err
|
|
}
|
|
|
|
const updateRegionLocation = `-- name: UpdateRegionLocation :execrows
|
|
UPDATE regions SET latitude = $1, longitude = $2
|
|
WHERE id = $3
|
|
`
|
|
|
|
type UpdateRegionLocationParams struct {
|
|
Latitude *float64
|
|
Longitude *float64
|
|
ID uuid.UUID
|
|
}
|
|
|
|
// Null coordinates remove the location.
|
|
func (q *Queries) UpdateRegionLocation(ctx context.Context, arg UpdateRegionLocationParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateRegionLocation, arg.Latitude, arg.Longitude, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|