// 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.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 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 >= $1 ) c ON true WHERE n.public_key = ANY($2::bytea[]) ` type AlertNodeCapsParams struct { Since pgtype.Timestamptz `json:"since"` Keys [][]byte `json:"keys"` } type AlertNodeCapsRow struct { 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"` } // 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.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.PublicKey, &i.NodeType, &i.Name, &i.Readings, &i.HasBattery, &i.HasTemperature, &i.HasNoise, &i.HasUptime, ); 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.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 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 >= $1 ) c ON true WHERE o.public_key = ANY($2::bytea[]) ` type AlertObserverCapsParams struct { Since pgtype.Timestamptz `json:"since"` Keys [][]byte `json:"keys"` } type AlertObserverCapsRow struct { PublicKey []byte `json:"public_key"` Name string `json:"name"` HasBattery bool `json:"has_battery"` HasNoise bool `json:"has_noise"` HasUptime bool `json:"has_uptime"` } func (q *Queries) AlertObserverCaps(ctx context.Context, arg AlertObserverCapsParams) ([]AlertObserverCapsRow, error) { rows, err := q.db.Query(ctx, alertObserverCaps, 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.PublicKey, &i.Name, &i.HasBattery, &i.HasNoise, &i.HasUptime, ); 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 deleteDevicesWithToken = `-- name: DeleteDevicesWithToken :exec DELETE FROM user_devices WHERE push_token = $1 AND session_id <> $2 ` type DeleteDevicesWithTokenParams struct { PushToken string `json:"push_token"` SessionID uuid.UUID `json:"session_id"` } // A token belongs to one session: a device signing in again takes it over. func (q *Queries) DeleteDevicesWithToken(ctx context.Context, arg DeleteDevicesWithTokenParams) error { _, err := q.db.Exec(ctx, deleteDevicesWithToken, arg.PushToken, arg.SessionID) 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 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) VALUES ($1, $2, $3, $4) ON CONFLICT (session_id) DO UPDATE SET platform = EXCLUDED.platform, push_token = EXCLUDED.push_token, 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"` } func (q *Queries) UpsertUserDevice(ctx context.Context, arg UpsertUserDeviceParams) error { _, err := q.db.Exec(ctx, upsertUserDevice, arg.SessionID, arg.UserID, arg.Platform, arg.PushToken, ) return err } const userAlertRuleKinds = `-- name: UserAlertRuleKinds :many SELECT kind FROM user_alert_rules WHERE user_id = $1 AND target_public_key = $2 ` type UserAlertRuleKindsParams struct { UserID uuid.UUID `json:"user_id"` TargetPublicKey []byte `json:"target_public_key"` } 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 }