mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 10:54:34 +00:00
A repeater read by a poller this server does not assign now counts as polled while a trusted reading is under 36 hours old. gone_quiet takes 3 and 6 hours on every radio.
477 lines
16 KiB
Go
477 lines
16 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: alerts.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const alertNodeCaps = `-- name: AlertNodeCaps :many
|
|
SELECT n.id, n.public_key, n.node_type, COALESCE(n.name, '')::text AS name,
|
|
COALESCE(c.readings, 0)::int AS readings,
|
|
COALESCE(c.battery, false)::boolean AS has_battery, COALESCE(c.temperature, false)::boolean AS has_temperature,
|
|
COALESCE(c.noise, false)::boolean AS has_noise, COALESCE(c.uptime, false)::boolean AS has_uptime,
|
|
-- Polled is a current source, not history, while polling is on: some observer holds the repeater as a polling
|
|
-- assignment, or a trusted reading of it was taken since fresh_since (a poller this server does not assign).
|
|
($1::boolean AND (
|
|
EXISTS (SELECT 1 FROM polling_assignments a WHERE a.target_public_key = n.public_key)
|
|
OR EXISTS (SELECT 1 FROM repeater_readings f WHERE f.target_public_key = n.public_key AND f.trusted
|
|
AND f.sampled_at >= $2)))::boolean AS polled
|
|
FROM nodes n
|
|
LEFT JOIN LATERAL (
|
|
SELECT count(*) AS readings, bool_or(r.battery_mv IS NOT NULL) AS battery, bool_or(r.temperature_c IS NOT NULL) AS temperature,
|
|
bool_or(r.noise_floor IS NOT NULL) AS noise, bool_or(r.uptime_secs IS NOT NULL) AS uptime
|
|
FROM repeater_readings r
|
|
WHERE r.target_public_key = n.public_key AND r.trusted AND r.sampled_at >= $3
|
|
) c ON true
|
|
WHERE n.public_key = ANY($4::bytea[])
|
|
`
|
|
|
|
type AlertNodeCapsParams struct {
|
|
PollingEnabled bool `json:"polling_enabled"`
|
|
FreshSince pgtype.Timestamptz `json:"fresh_since"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
Keys [][]byte `json:"keys"`
|
|
}
|
|
|
|
type AlertNodeCapsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
PublicKey []byte `json:"public_key"`
|
|
NodeType int16 `json:"node_type"`
|
|
Name string `json:"name"`
|
|
Readings int32 `json:"readings"`
|
|
HasBattery bool `json:"has_battery"`
|
|
HasTemperature bool `json:"has_temperature"`
|
|
HasNoise bool `json:"has_noise"`
|
|
HasUptime bool `json:"has_uptime"`
|
|
Polled bool `json:"polled"`
|
|
}
|
|
|
|
// What each of the nodes is and what a polled one reports, over the readings since the cutoff.
|
|
func (q *Queries) AlertNodeCaps(ctx context.Context, arg AlertNodeCapsParams) ([]AlertNodeCapsRow, error) {
|
|
rows, err := q.db.Query(ctx, alertNodeCaps,
|
|
arg.PollingEnabled,
|
|
arg.FreshSince,
|
|
arg.Since,
|
|
arg.Keys,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []AlertNodeCapsRow{}
|
|
for rows.Next() {
|
|
var i AlertNodeCapsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.PublicKey,
|
|
&i.NodeType,
|
|
&i.Name,
|
|
&i.Readings,
|
|
&i.HasBattery,
|
|
&i.HasTemperature,
|
|
&i.HasNoise,
|
|
&i.HasUptime,
|
|
&i.Polled,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const alertObserverCaps = `-- name: AlertObserverCaps :many
|
|
SELECT o.id, o.public_key, COALESCE(o.display_name, '')::text AS name,
|
|
COALESCE(c.battery, false)::boolean AS has_battery, COALESCE(c.noise, false)::boolean AS has_noise,
|
|
COALESCE(c.uptime, false)::boolean AS has_uptime,
|
|
-- Polled is a current source, not history: status telemetry arrived since fresh_since.
|
|
EXISTS (SELECT 1 FROM observer_telemetry f WHERE f.observer_id = o.id AND f.reported_at >= $1) AS polled
|
|
FROM observers o
|
|
LEFT JOIN LATERAL (
|
|
SELECT bool_or(t.battery_voltage_mv IS NOT NULL) AS battery, bool_or(t.noise_floor_db IS NOT NULL) AS noise,
|
|
bool_or(t.uptime_seconds IS NOT NULL) AS uptime
|
|
FROM observer_telemetry t
|
|
WHERE t.observer_id = o.id AND t.reported_at >= $2
|
|
) c ON true
|
|
WHERE o.public_key = ANY($3::bytea[])
|
|
`
|
|
|
|
type AlertObserverCapsParams struct {
|
|
FreshSince pgtype.Timestamptz `json:"fresh_since"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
Keys [][]byte `json:"keys"`
|
|
}
|
|
|
|
type AlertObserverCapsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
PublicKey []byte `json:"public_key"`
|
|
Name string `json:"name"`
|
|
HasBattery bool `json:"has_battery"`
|
|
HasNoise bool `json:"has_noise"`
|
|
HasUptime bool `json:"has_uptime"`
|
|
Polled bool `json:"polled"`
|
|
}
|
|
|
|
func (q *Queries) AlertObserverCaps(ctx context.Context, arg AlertObserverCapsParams) ([]AlertObserverCapsRow, error) {
|
|
rows, err := q.db.Query(ctx, alertObserverCaps, arg.FreshSince, arg.Since, arg.Keys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []AlertObserverCapsRow{}
|
|
for rows.Next() {
|
|
var i AlertObserverCapsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.PublicKey,
|
|
&i.Name,
|
|
&i.HasBattery,
|
|
&i.HasNoise,
|
|
&i.HasUptime,
|
|
&i.Polled,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const deleteAlertRule = `-- name: DeleteAlertRule :execrows
|
|
DELETE FROM user_alert_rules WHERE id = $1 AND user_id = $2
|
|
`
|
|
|
|
type DeleteAlertRuleParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) DeleteAlertRule(ctx context.Context, arg DeleteAlertRuleParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteAlertRule, arg.ID, arg.UserID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteSessionDeviceOtherToken = `-- name: DeleteSessionDeviceOtherToken :exec
|
|
DELETE FROM user_devices WHERE session_id = $1 AND push_token <> $2
|
|
`
|
|
|
|
type DeleteSessionDeviceOtherTokenParams struct {
|
|
SessionID uuid.UUID `json:"session_id"`
|
|
PushToken string `json:"push_token"`
|
|
}
|
|
|
|
// A session has one device: registering a new token replaces the old one.
|
|
func (q *Queries) DeleteSessionDeviceOtherToken(ctx context.Context, arg DeleteSessionDeviceOtherTokenParams) error {
|
|
_, err := q.db.Exec(ctx, deleteSessionDeviceOtherToken, arg.SessionID, arg.PushToken)
|
|
return err
|
|
}
|
|
|
|
const deleteUnpinnedAlertRules = `-- name: DeleteUnpinnedAlertRules :exec
|
|
DELETE FROM user_alert_rules r
|
|
WHERE r.user_id = $1
|
|
AND NOT EXISTS (SELECT 1 FROM user_atlas_pins p WHERE p.user_id = r.user_id AND p.public_key = r.target_public_key)
|
|
`
|
|
|
|
// Run in the Atlas write's transaction: a radio taken off the Atlas takes its rules with it.
|
|
func (q *Queries) DeleteUnpinnedAlertRules(ctx context.Context, userID uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteUnpinnedAlertRules, userID)
|
|
return err
|
|
}
|
|
|
|
const deleteUserDevice = `-- name: DeleteUserDevice :exec
|
|
DELETE FROM user_devices WHERE session_id = $1 AND user_id = $2
|
|
`
|
|
|
|
type DeleteUserDeviceParams struct {
|
|
SessionID uuid.UUID `json:"session_id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) DeleteUserDevice(ctx context.Context, arg DeleteUserDeviceParams) error {
|
|
_, err := q.db.Exec(ctx, deleteUserDevice, arg.SessionID, arg.UserID)
|
|
return err
|
|
}
|
|
|
|
const listUserAlertRules = `-- name: ListUserAlertRules :many
|
|
SELECT id, target_kind, target_public_key, kind, threshold, threshold_high, enabled, state, state_changed_at, last_sent_at
|
|
FROM user_alert_rules WHERE user_id = $1 ORDER BY created_at, id
|
|
`
|
|
|
|
type ListUserAlertRulesRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
TargetKind string `json:"target_kind"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Kind string `json:"kind"`
|
|
Threshold *int32 `json:"threshold"`
|
|
ThresholdHigh *int32 `json:"threshold_high"`
|
|
Enabled bool `json:"enabled"`
|
|
State string `json:"state"`
|
|
StateChangedAt pgtype.Timestamptz `json:"state_changed_at"`
|
|
LastSentAt pgtype.Timestamptz `json:"last_sent_at"`
|
|
}
|
|
|
|
func (q *Queries) ListUserAlertRules(ctx context.Context, userID uuid.UUID) ([]ListUserAlertRulesRow, error) {
|
|
rows, err := q.db.Query(ctx, listUserAlertRules, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListUserAlertRulesRow{}
|
|
for rows.Next() {
|
|
var i ListUserAlertRulesRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TargetKind,
|
|
&i.TargetPublicKey,
|
|
&i.Kind,
|
|
&i.Threshold,
|
|
&i.ThresholdHigh,
|
|
&i.Enabled,
|
|
&i.State,
|
|
&i.StateChangedAt,
|
|
&i.LastSentAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const setUserAlertChannels = `-- name: SetUserAlertChannels :execrows
|
|
UPDATE users SET alert_email = COALESCE($1, alert_email), alert_push = COALESCE($2, alert_push)
|
|
WHERE id = $3
|
|
`
|
|
|
|
type SetUserAlertChannelsParams struct {
|
|
AlertEmail *bool `json:"alert_email"`
|
|
AlertPush *bool `json:"alert_push"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) SetUserAlertChannels(ctx context.Context, arg SetUserAlertChannelsParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setUserAlertChannels, arg.AlertEmail, arg.AlertPush, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const turnOnAlertPush = `-- name: TurnOnAlertPush :exec
|
|
UPDATE users SET alert_push = true WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) TurnOnAlertPush(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, turnOnAlertPush, id)
|
|
return err
|
|
}
|
|
|
|
const upsertAlertRule = `-- name: UpsertAlertRule :one
|
|
INSERT INTO user_alert_rules (user_id, target_kind, target_public_key, kind, threshold, threshold_high, enabled)
|
|
VALUES ($1, $2, $3, $4, $5,
|
|
$6, $7)
|
|
ON CONFLICT (user_id, target_public_key, kind) DO UPDATE SET
|
|
target_kind = EXCLUDED.target_kind,
|
|
threshold = EXCLUDED.threshold,
|
|
threshold_high = EXCLUDED.threshold_high,
|
|
enabled = EXCLUDED.enabled,
|
|
version = user_alert_rules.version + 1,
|
|
state = CASE WHEN user_alert_rules.threshold IS DISTINCT FROM EXCLUDED.threshold
|
|
OR user_alert_rules.threshold_high IS DISTINCT FROM EXCLUDED.threshold_high
|
|
OR (EXCLUDED.enabled AND NOT user_alert_rules.enabled) THEN 'unknown' ELSE user_alert_rules.state END,
|
|
state_changed_at = CASE WHEN user_alert_rules.threshold IS DISTINCT FROM EXCLUDED.threshold
|
|
OR user_alert_rules.threshold_high IS DISTINCT FROM EXCLUDED.threshold_high
|
|
OR (EXCLUDED.enabled AND NOT user_alert_rules.enabled) THEN now() ELSE user_alert_rules.state_changed_at END,
|
|
cursor_at = CASE WHEN user_alert_rules.threshold IS DISTINCT FROM EXCLUDED.threshold
|
|
OR user_alert_rules.threshold_high IS DISTINCT FROM EXCLUDED.threshold_high
|
|
OR (EXCLUDED.enabled AND NOT user_alert_rules.enabled) THEN now() ELSE user_alert_rules.cursor_at END,
|
|
streak = CASE WHEN user_alert_rules.threshold IS DISTINCT FROM EXCLUDED.threshold
|
|
OR user_alert_rules.threshold_high IS DISTINCT FROM EXCLUDED.threshold_high
|
|
OR (EXCLUDED.enabled AND NOT user_alert_rules.enabled) THEN 0 ELSE user_alert_rules.streak END
|
|
RETURNING id, target_kind, target_public_key, kind, threshold, threshold_high, enabled, state, state_changed_at, last_sent_at
|
|
`
|
|
|
|
type UpsertAlertRuleParams struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
TargetKind string `json:"target_kind"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Kind string `json:"kind"`
|
|
Threshold *int32 `json:"threshold"`
|
|
ThresholdHigh *int32 `json:"threshold_high"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
type UpsertAlertRuleRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
TargetKind string `json:"target_kind"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Kind string `json:"kind"`
|
|
Threshold *int32 `json:"threshold"`
|
|
ThresholdHigh *int32 `json:"threshold_high"`
|
|
Enabled bool `json:"enabled"`
|
|
State string `json:"state"`
|
|
StateChangedAt pgtype.Timestamptz `json:"state_changed_at"`
|
|
LastSentAt pgtype.Timestamptz `json:"last_sent_at"`
|
|
}
|
|
|
|
// A change of thresholds, or turning a rule on, starts its state again: no stale notification, and the data the
|
|
// evaluator looks at starts now.
|
|
func (q *Queries) UpsertAlertRule(ctx context.Context, arg UpsertAlertRuleParams) (UpsertAlertRuleRow, error) {
|
|
row := q.db.QueryRow(ctx, upsertAlertRule,
|
|
arg.UserID,
|
|
arg.TargetKind,
|
|
arg.TargetPublicKey,
|
|
arg.Kind,
|
|
arg.Threshold,
|
|
arg.ThresholdHigh,
|
|
arg.Enabled,
|
|
)
|
|
var i UpsertAlertRuleRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.TargetKind,
|
|
&i.TargetPublicKey,
|
|
&i.Kind,
|
|
&i.Threshold,
|
|
&i.ThresholdHigh,
|
|
&i.Enabled,
|
|
&i.State,
|
|
&i.StateChangedAt,
|
|
&i.LastSentAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upsertUserDevice = `-- name: UpsertUserDevice :exec
|
|
INSERT INTO user_devices (session_id, user_id, platform, push_token, payload_key)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT (push_token) DO UPDATE
|
|
SET session_id = EXCLUDED.session_id, user_id = EXCLUDED.user_id, platform = EXCLUDED.platform,
|
|
payload_key = EXCLUDED.payload_key, updated_at = now()
|
|
`
|
|
|
|
type UpsertUserDeviceParams struct {
|
|
SessionID uuid.UUID `json:"session_id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Platform string `json:"platform"`
|
|
PushToken string `json:"push_token"`
|
|
PayloadKey []byte `json:"payload_key"`
|
|
}
|
|
|
|
// The handle is unique, so a registration for a handle another session holds moves that row (crossing users on
|
|
// purpose: a phone belongs to one account, so the previous user's alerts never reach its next user).
|
|
func (q *Queries) UpsertUserDevice(ctx context.Context, arg UpsertUserDeviceParams) error {
|
|
_, err := q.db.Exec(ctx, upsertUserDevice,
|
|
arg.SessionID,
|
|
arg.UserID,
|
|
arg.Platform,
|
|
arg.PushToken,
|
|
arg.PayloadKey,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const userAlertChannels = `-- name: UserAlertChannels :one
|
|
SELECT alert_email, alert_push, (email IS NOT NULL AND email <> '')::boolean AS has_email,
|
|
(email_verified_at IS NOT NULL)::boolean AS email_verified
|
|
FROM users WHERE id = $1
|
|
`
|
|
|
|
type UserAlertChannelsRow struct {
|
|
AlertEmail bool `json:"alert_email"`
|
|
AlertPush bool `json:"alert_push"`
|
|
HasEmail bool `json:"has_email"`
|
|
EmailVerified bool `json:"email_verified"`
|
|
}
|
|
|
|
func (q *Queries) UserAlertChannels(ctx context.Context, id uuid.UUID) (UserAlertChannelsRow, error) {
|
|
row := q.db.QueryRow(ctx, userAlertChannels, id)
|
|
var i UserAlertChannelsRow
|
|
err := row.Scan(
|
|
&i.AlertEmail,
|
|
&i.AlertPush,
|
|
&i.HasEmail,
|
|
&i.EmailVerified,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const userAlertRuleKinds = `-- name: UserAlertRuleKinds :many
|
|
SELECT kind FROM user_alert_rules WHERE user_id = $1 AND target_public_key = $2 AND enabled
|
|
`
|
|
|
|
type UserAlertRuleKindsParams struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
}
|
|
|
|
// Only enabled rules: back_online needs a gone_quiet rule that is on.
|
|
func (q *Queries) UserAlertRuleKinds(ctx context.Context, arg UserAlertRuleKindsParams) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, userAlertRuleKinds, arg.UserID, arg.TargetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var kind string
|
|
if err := rows.Scan(&kind); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, kind)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const userHasAtlasPin = `-- name: UserHasAtlasPin :one
|
|
SELECT EXISTS (SELECT 1 FROM user_atlas_pins WHERE user_id = $1 AND public_key = $2)
|
|
`
|
|
|
|
type UserHasAtlasPinParams struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
PublicKey []byte `json:"public_key"`
|
|
}
|
|
|
|
func (q *Queries) UserHasAtlasPin(ctx context.Context, arg UserHasAtlasPinParams) (bool, error) {
|
|
row := q.db.QueryRow(ctx, userHasAtlasPin, arg.UserID, arg.PublicKey)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const userHasLiveDevice = `-- name: UserHasLiveDevice :one
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM user_devices d JOIN user_sessions s ON s.id = d.session_id
|
|
WHERE d.user_id = $1 AND s.ended_at IS NULL AND s.expires_at > now() AND (s.hard_expires_at IS NULL OR s.hard_expires_at > now())
|
|
)
|
|
`
|
|
|
|
func (q *Queries) UserHasLiveDevice(ctx context.Context, userID uuid.UUID) (bool, error) {
|
|
row := q.db.QueryRow(ctx, userHasLiveDevice, userID)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|