mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 15:34:26 +00:00
760 lines
26 KiB
Go
760 lines
26 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: telemetry_requests.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const clearNoPasswordBlocked = `-- name: ClearNoPasswordBlocked :execrows
|
|
UPDATE polling_targets SET no_password_blocked = false WHERE target_public_key = $1 AND no_password_blocked
|
|
`
|
|
|
|
// Run under LockPollingTarget.
|
|
func (q *Queries) ClearNoPasswordBlocked(ctx context.Context, targetPublicKey []byte) (int64, error) {
|
|
result, err := q.db.Exec(ctx, clearNoPasswordBlocked, targetPublicKey)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const countUserRequests = `-- name: CountUserRequests :one
|
|
SELECT count(*)::int FROM telemetry_requests WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) CountUserRequests(ctx context.Context, userID uuid.UUID) (int32, error) {
|
|
row := q.db.QueryRow(ctx, countUserRequests, userID)
|
|
var column_1 int32
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const deleteOldServedVersions = `-- name: DeleteOldServedVersions :exec
|
|
DELETE FROM polling_served_versions WHERE served_at < $1
|
|
`
|
|
|
|
func (q *Queries) DeleteOldServedVersions(ctx context.Context, servedAt pgtype.Timestamptz) error {
|
|
_, err := q.db.Exec(ctx, deleteOldServedVersions, servedAt)
|
|
return err
|
|
}
|
|
|
|
const deletePollingAdminStop = `-- name: DeletePollingAdminStop :execrows
|
|
DELETE FROM polling_admin_stops WHERE target_public_key = $1 AND observer_public_key = $2
|
|
`
|
|
|
|
type DeletePollingAdminStopParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
}
|
|
|
|
func (q *Queries) DeletePollingAdminStop(ctx context.Context, arg DeletePollingAdminStopParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deletePollingAdminStop, arg.TargetPublicKey, arg.ObserverPublicKey)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteTelemetryRequest = `-- name: DeleteTelemetryRequest :exec
|
|
DELETE FROM telemetry_requests WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteTelemetryRequest(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteTelemetryRequest, id)
|
|
return err
|
|
}
|
|
|
|
const ensurePollingTarget = `-- name: EnsurePollingTarget :exec
|
|
INSERT INTO polling_targets (target_public_key) VALUES ($1) ON CONFLICT (target_public_key) DO NOTHING
|
|
`
|
|
|
|
func (q *Queries) EnsurePollingTarget(ctx context.Context, targetPublicKey []byte) error {
|
|
_, err := q.db.Exec(ctx, ensurePollingTarget, targetPublicKey)
|
|
return err
|
|
}
|
|
|
|
const getPollingTarget = `-- name: GetPollingTarget :one
|
|
SELECT active_request_id, ver, ver_changed_at, no_password_blocked FROM polling_targets WHERE target_public_key = $1
|
|
`
|
|
|
|
type GetPollingTargetRow struct {
|
|
ActiveRequestID pgtype.UUID `json:"active_request_id"`
|
|
Ver int64 `json:"ver"`
|
|
VerChangedAt pgtype.Timestamptz `json:"ver_changed_at"`
|
|
NoPasswordBlocked bool `json:"no_password_blocked"`
|
|
}
|
|
|
|
func (q *Queries) GetPollingTarget(ctx context.Context, targetPublicKey []byte) (GetPollingTargetRow, error) {
|
|
row := q.db.QueryRow(ctx, getPollingTarget, targetPublicKey)
|
|
var i GetPollingTargetRow
|
|
err := row.Scan(
|
|
&i.ActiveRequestID,
|
|
&i.Ver,
|
|
&i.VerChangedAt,
|
|
&i.NoPasswordBlocked,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertPollingAdminStop = `-- name: InsertPollingAdminStop :execrows
|
|
INSERT INTO polling_admin_stops (target_public_key, observer_public_key, reported_at) VALUES ($1, $2, $3)
|
|
ON CONFLICT (target_public_key, observer_public_key) DO NOTHING
|
|
`
|
|
|
|
type InsertPollingAdminStopParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
ReportedAt pgtype.Timestamptz `json:"reported_at"`
|
|
}
|
|
|
|
func (q *Queries) InsertPollingAdminStop(ctx context.Context, arg InsertPollingAdminStopParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, insertPollingAdminStop, arg.TargetPublicKey, arg.ObserverPublicKey, arg.ReportedAt)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertTelemetryRequest = `-- name: InsertTelemetryRequest :exec
|
|
INSERT INTO telemetry_requests (id, user_id, target_public_key, status, status_reason, password_ct, password_nonce,
|
|
password_failed, created_at, updated_at, enabled_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
`
|
|
|
|
type InsertTelemetryRequestParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
PasswordFailed bool `json:"password_failed"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
EnabledAt pgtype.Timestamptz `json:"enabled_at"`
|
|
}
|
|
|
|
func (q *Queries) InsertTelemetryRequest(ctx context.Context, arg InsertTelemetryRequestParams) error {
|
|
_, err := q.db.Exec(ctx, insertTelemetryRequest,
|
|
arg.ID,
|
|
arg.UserID,
|
|
arg.TargetPublicKey,
|
|
arg.Status,
|
|
arg.StatusReason,
|
|
arg.PasswordCt,
|
|
arg.PasswordNonce,
|
|
arg.PasswordFailed,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
arg.EnabledAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listNoPasswordBlocked = `-- name: ListNoPasswordBlocked :many
|
|
SELECT target_public_key FROM polling_targets WHERE no_password_blocked ORDER BY target_public_key
|
|
`
|
|
|
|
func (q *Queries) ListNoPasswordBlocked(ctx context.Context) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listNoPasswordBlocked)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var target_public_key []byte
|
|
if err := rows.Scan(&target_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, target_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listPollingAdminStops = `-- name: ListPollingAdminStops :many
|
|
SELECT target_public_key, observer_public_key, reported_at FROM polling_admin_stops ORDER BY reported_at DESC, target_public_key, observer_public_key
|
|
`
|
|
|
|
func (q *Queries) ListPollingAdminStops(ctx context.Context) ([]PollingAdminStop, error) {
|
|
rows, err := q.db.Query(ctx, listPollingAdminStops)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []PollingAdminStop{}
|
|
for rows.Next() {
|
|
var i PollingAdminStop
|
|
if err := rows.Scan(&i.TargetPublicKey, &i.ObserverPublicKey, &i.ReportedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listRequestedAssigneeReadings = `-- name: ListRequestedAssigneeReadings :many
|
|
SELECT a.observer_public_key, l.received_at
|
|
FROM polling_assignments a
|
|
CROSS JOIN LATERAL (
|
|
SELECT r.received_at FROM repeater_readings r
|
|
WHERE r.target_public_key = a.target_public_key AND r.observer_public_key = a.observer_public_key AND r.trusted
|
|
ORDER BY r.sampled_at DESC LIMIT 1
|
|
) l
|
|
WHERE a.target_public_key = $1 AND a.requested
|
|
`
|
|
|
|
type ListRequestedAssigneeReadingsRow struct {
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
ReceivedAt pgtype.Timestamptz `json:"received_at"`
|
|
}
|
|
|
|
// When each observer handed this target as a requested one stored its newest trusted reading of it.
|
|
func (q *Queries) ListRequestedAssigneeReadings(ctx context.Context, targetPublicKey []byte) ([]ListRequestedAssigneeReadingsRow, error) {
|
|
rows, err := q.db.Query(ctx, listRequestedAssigneeReadings, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListRequestedAssigneeReadingsRow{}
|
|
for rows.Next() {
|
|
var i ListRequestedAssigneeReadingsRow
|
|
if err := rows.Scan(&i.ObserverPublicKey, &i.ReceivedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listRequestedAssignees = `-- name: ListRequestedAssignees :many
|
|
SELECT observer_public_key, rank FROM polling_assignments WHERE target_public_key = $1 AND requested
|
|
`
|
|
|
|
type ListRequestedAssigneesRow struct {
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
Rank int16 `json:"rank"`
|
|
}
|
|
|
|
// The observers handed this target as a requested one, with their rank (0 polls).
|
|
func (q *Queries) ListRequestedAssignees(ctx context.Context, targetPublicKey []byte) ([]ListRequestedAssigneesRow, error) {
|
|
rows, err := q.db.Query(ctx, listRequestedAssignees, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListRequestedAssigneesRow{}
|
|
for rows.Next() {
|
|
var i ListRequestedAssigneesRow
|
|
if err := rows.Scan(&i.ObserverPublicKey, &i.Rank); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetAdminStops = `-- name: ListTargetAdminStops :many
|
|
SELECT observer_public_key FROM polling_admin_stops WHERE target_public_key = $1
|
|
`
|
|
|
|
func (q *Queries) ListTargetAdminStops(ctx context.Context, targetPublicKey []byte) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listTargetAdminStops, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var observer_public_key []byte
|
|
if err := rows.Scan(&observer_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, observer_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetOtherStates = `-- name: ListTargetOtherStates :many
|
|
SELECT observer_public_key, seen_at FROM polling_other_states WHERE target_public_key = $1
|
|
`
|
|
|
|
type ListTargetOtherStatesRow struct {
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
SeenAt pgtype.Timestamptz `json:"seen_at"`
|
|
}
|
|
|
|
func (q *Queries) ListTargetOtherStates(ctx context.Context, targetPublicKey []byte) ([]ListTargetOtherStatesRow, error) {
|
|
rows, err := q.db.Query(ctx, listTargetOtherStates, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListTargetOtherStatesRow{}
|
|
for rows.Next() {
|
|
var i ListTargetOtherStatesRow
|
|
if err := rows.Scan(&i.ObserverPublicKey, &i.SeenAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetRequests = `-- name: ListTargetRequests :many
|
|
SELECT id, user_id, status, status_reason, password_ct, password_nonce, password_failed, created_at, updated_at, enabled_at
|
|
FROM telemetry_requests WHERE target_public_key = $1 ORDER BY created_at, id
|
|
`
|
|
|
|
type ListTargetRequestsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
PasswordFailed bool `json:"password_failed"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
EnabledAt pgtype.Timestamptz `json:"enabled_at"`
|
|
}
|
|
|
|
func (q *Queries) ListTargetRequests(ctx context.Context, targetPublicKey []byte) ([]ListTargetRequestsRow, error) {
|
|
rows, err := q.db.Query(ctx, listTargetRequests, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListTargetRequestsRow{}
|
|
for rows.Next() {
|
|
var i ListTargetRequestsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.Status,
|
|
&i.StatusReason,
|
|
&i.PasswordCt,
|
|
&i.PasswordNonce,
|
|
&i.PasswordFailed,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.EnabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetServedVersions = `-- name: ListTargetServedVersions :many
|
|
SELECT observer_public_key, ver FROM polling_served_versions WHERE target_public_key = $1
|
|
`
|
|
|
|
type ListTargetServedVersionsRow struct {
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
Ver int64 `json:"ver"`
|
|
}
|
|
|
|
func (q *Queries) ListTargetServedVersions(ctx context.Context, targetPublicKey []byte) ([]ListTargetServedVersionsRow, error) {
|
|
rows, err := q.db.Query(ctx, listTargetServedVersions, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListTargetServedVersionsRow{}
|
|
for rows.Next() {
|
|
var i ListTargetServedVersionsRow
|
|
if err := rows.Scan(&i.ObserverPublicKey, &i.Ver); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listUserTelemetryRequests = `-- name: ListUserTelemetryRequests :many
|
|
SELECT r.id, r.target_public_key, r.status, r.status_reason, (r.password_ct IS NOT NULL)::boolean AS has_password,
|
|
r.created_at, r.updated_at,
|
|
n.id AS node_id, COALESCE(n.name, '')::text AS node_name,
|
|
COALESCE(p.observer_id, '00000000-0000-0000-0000-000000000000'::uuid) AS poller_id, COALESCE(p.name, '')::text AS poller_name,
|
|
p.claim_at::timestamptz AS poller_claim_at, COALESCE(p.claimed, false)::boolean AS poller_claimed,
|
|
COALESCE(p.state, '')::text AS poller_state,
|
|
(SELECT max(x.sampled_at) FROM repeater_readings x WHERE x.target_public_key = r.target_public_key AND x.trusted)::timestamptz AS latest_reading_at
|
|
FROM telemetry_requests r
|
|
LEFT JOIN nodes n ON n.public_key = r.target_public_key
|
|
LEFT JOIN LATERAL (
|
|
SELECT o.id AS observer_id, COALESCE(NULLIF(o.display_name, ''), left(encode(o.public_key, 'hex'), 8)) AS name,
|
|
cl.received_at AS claim_at, (c.observer_public_key IS NOT NULL) AS claimed, c.state
|
|
FROM polling_assignments a JOIN observers o ON o.public_key = a.observer_public_key
|
|
LEFT JOIN polling_claims cl ON cl.observer_public_key = a.observer_public_key AND cl.received_at > $1
|
|
LEFT JOIN polling_candidates c ON c.observer_public_key = cl.observer_public_key AND c.target_public_key = a.target_public_key
|
|
WHERE a.target_public_key = r.target_public_key AND a.rank = 0
|
|
LIMIT 1
|
|
) p ON true
|
|
WHERE r.user_id = $2
|
|
ORDER BY r.created_at, r.id
|
|
`
|
|
|
|
type ListUserTelemetryRequestsParams struct {
|
|
ReceivedAfter pgtype.Timestamptz `json:"received_after"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
}
|
|
|
|
type ListUserTelemetryRequestsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
HasPassword bool `json:"has_password"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
NodeID pgtype.UUID `json:"node_id"`
|
|
NodeName string `json:"node_name"`
|
|
PollerID uuid.UUID `json:"poller_id"`
|
|
PollerName string `json:"poller_name"`
|
|
PollerClaimAt pgtype.Timestamptz `json:"poller_claim_at"`
|
|
PollerClaimed bool `json:"poller_claimed"`
|
|
PollerState string `json:"poller_state"`
|
|
LatestReadingAt pgtype.Timestamptz `json:"latest_reading_at"`
|
|
}
|
|
|
|
// A user's requests, oldest first, with the node's current id and name, the observer that polls the repeater and
|
|
// the newest trusted reading. No password column is read; poller_id is the nil uuid when nobody polls it. The
|
|
// poller's own claim is live when received after received_after: poller_claim_at is null without one, and
|
|
// poller_claimed says whether that claim lists the repeater (poller_state is its state then).
|
|
func (q *Queries) ListUserTelemetryRequests(ctx context.Context, arg ListUserTelemetryRequestsParams) ([]ListUserTelemetryRequestsRow, error) {
|
|
rows, err := q.db.Query(ctx, listUserTelemetryRequests, arg.ReceivedAfter, arg.UserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListUserTelemetryRequestsRow{}
|
|
for rows.Next() {
|
|
var i ListUserTelemetryRequestsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TargetPublicKey,
|
|
&i.Status,
|
|
&i.StatusReason,
|
|
&i.HasPassword,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.NodeID,
|
|
&i.NodeName,
|
|
&i.PollerID,
|
|
&i.PollerName,
|
|
&i.PollerClaimAt,
|
|
&i.PollerClaimed,
|
|
&i.PollerState,
|
|
&i.LatestReadingAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockUserForRequests = `-- name: LockUserForRequests :one
|
|
SELECT id FROM users WHERE id = $1 FOR NO KEY UPDATE
|
|
`
|
|
|
|
// Serializes one user's request creation (the limit) without blocking the foreign-key checks on their rows.
|
|
func (q *Queries) LockUserForRequests(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
|
|
row := q.db.QueryRow(ctx, lockUserForRequests, id)
|
|
var id_2 uuid.UUID
|
|
err := row.Scan(&id_2)
|
|
return id_2, err
|
|
}
|
|
|
|
const offlineRequestTargets = `-- name: OfflineRequestTargets :many
|
|
SELECT r.target_public_key::bytea AS target_public_key
|
|
FROM telemetry_requests r
|
|
LEFT JOIN nodes n ON n.public_key = r.target_public_key
|
|
WHERE r.status IN ('waiting', 'active')
|
|
GROUP BY r.target_public_key, n.last_seen
|
|
HAVING GREATEST(max(r.enabled_at), COALESCE(n.last_seen, '-infinity'::timestamptz)) < $1::timestamptz
|
|
`
|
|
|
|
// Targets with a waiting or active request whose node no observer has heard since cutoff and whose newest such
|
|
// request was enabled before it. Requests with no node row count from the enabling alone.
|
|
func (q *Queries) OfflineRequestTargets(ctx context.Context, cutoff pgtype.Timestamptz) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, offlineRequestTargets, cutoff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var target_public_key []byte
|
|
if err := rows.Scan(&target_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, target_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const pollingAdminRequestCounts = `-- name: PollingAdminRequestCounts :many
|
|
SELECT r.target_public_key::bytea AS target_public_key, count(*)::int AS requests,
|
|
bool_or(t.active_request_id IS NOT NULL)::boolean AS requested
|
|
FROM telemetry_requests r
|
|
LEFT JOIN polling_targets t ON t.target_public_key = r.target_public_key
|
|
WHERE r.target_public_key = ANY($1::bytea[])
|
|
GROUP BY r.target_public_key
|
|
`
|
|
|
|
type PollingAdminRequestCountsRow struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Requests int32 `json:"requests"`
|
|
Requested bool `json:"requested"`
|
|
}
|
|
|
|
func (q *Queries) PollingAdminRequestCounts(ctx context.Context, targets [][]byte) ([]PollingAdminRequestCountsRow, error) {
|
|
rows, err := q.db.Query(ctx, pollingAdminRequestCounts, targets)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []PollingAdminRequestCountsRow{}
|
|
for rows.Next() {
|
|
var i PollingAdminRequestCountsRow
|
|
if err := rows.Scan(&i.TargetPublicKey, &i.Requests, &i.Requested); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const pollingTargetExists = `-- name: PollingTargetExists :one
|
|
SELECT EXISTS (SELECT 1 FROM polling_targets WHERE target_public_key = $1)
|
|
`
|
|
|
|
func (q *Queries) PollingTargetExists(ctx context.Context, targetPublicKey []byte) (bool, error) {
|
|
row := q.db.QueryRow(ctx, pollingTargetExists, targetPublicKey)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const recordServedVersion = `-- name: RecordServedVersion :exec
|
|
INSERT INTO polling_served_versions AS s (target_public_key, observer_public_key, ver, served_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (target_public_key, observer_public_key) DO UPDATE SET ver = EXCLUDED.ver, served_at = EXCLUDED.served_at
|
|
WHERE s.ver <> EXCLUDED.ver OR s.served_at < EXCLUDED.served_at - interval '1 hour'
|
|
`
|
|
|
|
type RecordServedVersionParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
Ver int64 `json:"ver"`
|
|
ServedAt pgtype.Timestamptz `json:"served_at"`
|
|
}
|
|
|
|
// Written on every owners answer that lists the target, so it only touches the row when the version changed or the
|
|
// last write is an hour old.
|
|
func (q *Queries) RecordServedVersion(ctx context.Context, arg RecordServedVersionParams) error {
|
|
_, err := q.db.Exec(ctx, recordServedVersion,
|
|
arg.TargetPublicKey,
|
|
arg.ObserverPublicKey,
|
|
arg.Ver,
|
|
arg.ServedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const requestNode = `-- name: RequestNode :one
|
|
SELECT public_key, node_type, COALESCE(name, '')::text AS name FROM nodes WHERE id = $1
|
|
`
|
|
|
|
type RequestNodeRow struct {
|
|
PublicKey []byte `json:"public_key"`
|
|
NodeType int16 `json:"node_type"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func (q *Queries) RequestNode(ctx context.Context, id uuid.UUID) (RequestNodeRow, error) {
|
|
row := q.db.QueryRow(ctx, requestNode, id)
|
|
var i RequestNodeRow
|
|
err := row.Scan(&i.PublicKey, &i.NodeType, &i.Name)
|
|
return i, err
|
|
}
|
|
|
|
const targetStillOffline = `-- name: TargetStillOffline :one
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM telemetry_requests r
|
|
LEFT JOIN nodes n ON n.public_key = r.target_public_key
|
|
WHERE r.status IN ('waiting', 'active') AND r.target_public_key = $1
|
|
GROUP BY r.target_public_key, n.last_seen
|
|
HAVING GREATEST(max(r.enabled_at), COALESCE(n.last_seen, '-infinity'::timestamptz)) < $2::timestamptz
|
|
)::boolean AS offline
|
|
`
|
|
|
|
type TargetStillOfflineParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Cutoff pgtype.Timestamptz `json:"cutoff"`
|
|
}
|
|
|
|
// OfflineRequestTargets for one target, asked again under the target's lock before its requests are disabled.
|
|
func (q *Queries) TargetStillOffline(ctx context.Context, arg TargetStillOfflineParams) (bool, error) {
|
|
row := q.db.QueryRow(ctx, targetStillOffline, arg.TargetPublicKey, arg.Cutoff)
|
|
var offline bool
|
|
err := row.Scan(&offline)
|
|
return offline, err
|
|
}
|
|
|
|
const telemetryRequestTarget = `-- name: TelemetryRequestTarget :one
|
|
SELECT target_public_key FROM telemetry_requests WHERE id = $1 AND user_id = $2
|
|
`
|
|
|
|
type TelemetryRequestTargetParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) TelemetryRequestTarget(ctx context.Context, arg TelemetryRequestTargetParams) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, telemetryRequestTarget, arg.ID, arg.UserID)
|
|
var target_public_key []byte
|
|
err := row.Scan(&target_public_key)
|
|
return target_public_key, err
|
|
}
|
|
|
|
const touchPollingOtherState = `-- name: TouchPollingOtherState :exec
|
|
INSERT INTO polling_other_states (target_public_key, observer_public_key, seen_at) VALUES ($1, $2, $3)
|
|
ON CONFLICT (target_public_key, observer_public_key) DO UPDATE SET seen_at = EXCLUDED.seen_at
|
|
`
|
|
|
|
type TouchPollingOtherStateParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
SeenAt pgtype.Timestamptz `json:"seen_at"`
|
|
}
|
|
|
|
func (q *Queries) TouchPollingOtherState(ctx context.Context, arg TouchPollingOtherStateParams) error {
|
|
_, err := q.db.Exec(ctx, touchPollingOtherState, arg.TargetPublicKey, arg.ObserverPublicKey, arg.SeenAt)
|
|
return err
|
|
}
|
|
|
|
const updatePollingTarget = `-- name: UpdatePollingTarget :exec
|
|
UPDATE polling_targets SET active_request_id = $2, ver = $3, ver_changed_at = $4, no_password_blocked = $5
|
|
WHERE target_public_key = $1
|
|
`
|
|
|
|
type UpdatePollingTargetParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ActiveRequestID pgtype.UUID `json:"active_request_id"`
|
|
Ver int64 `json:"ver"`
|
|
VerChangedAt pgtype.Timestamptz `json:"ver_changed_at"`
|
|
NoPasswordBlocked bool `json:"no_password_blocked"`
|
|
}
|
|
|
|
func (q *Queries) UpdatePollingTarget(ctx context.Context, arg UpdatePollingTargetParams) error {
|
|
_, err := q.db.Exec(ctx, updatePollingTarget,
|
|
arg.TargetPublicKey,
|
|
arg.ActiveRequestID,
|
|
arg.Ver,
|
|
arg.VerChangedAt,
|
|
arg.NoPasswordBlocked,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateTelemetryRequest = `-- name: UpdateTelemetryRequest :exec
|
|
UPDATE telemetry_requests SET status = $2, status_reason = $3, password_ct = $4, password_nonce = $5,
|
|
password_failed = $6, updated_at = $7, enabled_at = $8
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateTelemetryRequestParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
PasswordFailed bool `json:"password_failed"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
EnabledAt pgtype.Timestamptz `json:"enabled_at"`
|
|
}
|
|
|
|
func (q *Queries) UpdateTelemetryRequest(ctx context.Context, arg UpdateTelemetryRequestParams) error {
|
|
_, err := q.db.Exec(ctx, updateTelemetryRequest,
|
|
arg.ID,
|
|
arg.Status,
|
|
arg.StatusReason,
|
|
arg.PasswordCt,
|
|
arg.PasswordNonce,
|
|
arg.PasswordFailed,
|
|
arg.UpdatedAt,
|
|
arg.EnabledAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const userRequestTargets = `-- name: UserRequestTargets :many
|
|
SELECT DISTINCT target_public_key FROM telemetry_requests WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) UserRequestTargets(ctx context.Context, userID uuid.UUID) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, userRequestTargets, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var target_public_key []byte
|
|
if err := rows.Scan(&target_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, target_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|