Files
uptimesebi/internal/store/agent_instances.sql.go
T

131 lines
3.0 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: agent_instances.sql
package store
import (
"context"
"time"
"github.com/google/uuid"
)
const agentInstancesByID = `-- name: AgentInstancesByID :many
SELECT i.id, i.agent_id, i.release, i.started_at, i.last_seen_at, i.org_id,
a.name AS agent_name, a.kind, r.id AS region_id, coalesce(r.name, '')::text AS region_name
FROM agent_instances i
JOIN agents a ON a.id = i.agent_id
LEFT JOIN regions r ON r.id = a.region_id
WHERE i.id = ANY ($1::uuid[])
`
type AgentInstancesByIDRow struct {
ID uuid.UUID
AgentID uuid.UUID
Release string
StartedAt time.Time
LastSeenAt time.Time
OrgID *uuid.UUID
AgentName string
Kind string
RegionID *uuid.UUID
RegionName string
}
func (q *Queries) AgentInstancesByID(ctx context.Context, ids []uuid.UUID) ([]AgentInstancesByIDRow, error) {
rows, err := q.db.Query(ctx, agentInstancesByID, ids)
if err != nil {
return nil, err
}
defer rows.Close()
items := []AgentInstancesByIDRow{}
for rows.Next() {
var i AgentInstancesByIDRow
if err := rows.Scan(
&i.ID,
&i.AgentID,
&i.Release,
&i.StartedAt,
&i.LastSeenAt,
&i.OrgID,
&i.AgentName,
&i.Kind,
&i.RegionID,
&i.RegionName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const siblingInstances = `-- name: SiblingInstances :many
SELECT id, release, started_at, last_seen_at FROM agent_instances
WHERE agent_id = $1 AND last_seen_at > now() - interval '1 day'
ORDER BY last_seen_at DESC
`
type SiblingInstancesRow struct {
ID uuid.UUID
Release string
StartedAt time.Time
LastSeenAt time.Time
}
// Other instances of the same agent seen in the last day.
func (q *Queries) SiblingInstances(ctx context.Context, agentID uuid.UUID) ([]SiblingInstancesRow, error) {
rows, err := q.db.Query(ctx, siblingInstances, agentID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []SiblingInstancesRow{}
for rows.Next() {
var i SiblingInstancesRow
if err := rows.Scan(
&i.ID,
&i.Release,
&i.StartedAt,
&i.LastSeenAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const touchAgentInstance = `-- name: TouchAgentInstance :exec
INSERT INTO agent_instances (id, agent_id, org_id, release)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO UPDATE SET release = EXCLUDED.release, last_seen_at = now()
WHERE agent_instances.last_seen_at < now() - interval '1 minute' OR agent_instances.release <> EXCLUDED.release
`
type TouchAgentInstanceParams struct {
ID uuid.UUID
AgentID uuid.UUID
OrgID *uuid.UUID
Release string
}
// Writes at most once a minute per instance; agents poll far more often.
func (q *Queries) TouchAgentInstance(ctx context.Context, arg TouchAgentInstanceParams) error {
_, err := q.db.Exec(ctx, touchAgentInstance,
arg.ID,
arg.AgentID,
arg.OrgID,
arg.Release,
)
return err
}