blob: d868a6c8f9ce5d276765e3486c672473b5892697 [file] [log] [blame]
Serge Bazanski6f599512023-04-26 19:08:19 +02001-- name: CountActiveWork :many
2-- Return number of active work, grouped by process.
3SELECT COUNT(*), work.process
4FROM work
5GROUP BY (work.process);
6
7-- name: CountActiveBackoffs :many
8-- Return number of active backoffs, grouped by process.
9SELECT COUNT(*), work_backoff.process
10FROM work_backoff
11GROUP BY (work_backoff.process);
12
13-- name: CountMachines :one
14SELECT COUNT(*)
15FROM machines;
16
17-- name: CountMachinesProvided :one
18SELECT COUNT(*)
19FROM machine_provided;
20
21-- name: CountMachinesAgentHeartbeating :one
22SELECT COUNT(*)
23FROM machines
24 INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
25 INNER JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
26WHERE
27 now() < machine_agent_heartbeat.agent_heartbeat_at + interval '10 minute';
28
29-- name: CountMachinesInstallationPending :one
30SELECT COUNT(*)
31FROM machines
32 INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
33 INNER JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
34 LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
35WHERE
36 machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation;
37
38-- name: CountMachinesInstallationComplete :one
39SELECT COUNT(*)
40FROM machines
41 INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
42 INNER JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
43 INNER JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
44WHERE
45 machine_os_installation_request.generation IS NOT DISTINCT FROM machine_os_installation_report.generation;
46
47-- name: CountMachinesForAgentStart :one
48-- Return number of machines eligible for agent start.
49-- ONCHANGE(queries_workflows.sql): constraints must be kept in sync with GetMachinesForAgentStart.
50SELECT COUNT(machine_provided)
51FROM machines
52 INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
53 LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
54 LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdAgentStart'
55 LEFT JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
56 LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
57 LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
58WHERE
59 work.machine_id IS NULL
60 AND work_backoff.machine_id IS NULL
61 AND machine_agent_started.machine_id IS NULL
62 -- If there is a pending installation request, it must not have been fulfilled already.
63 AND (
64 machine_os_installation_request.machine_id IS NULL
65 OR machine_os_installation_report.generation IS DISTINCT FROM machine_os_installation_request.generation
66 );
67
68-- name: CountMachinesForAgentRecovery :one
69-- Return number of machines eligible for agent recovery.
70-- ONCHANGE(queries_workflows.sql): constraints must be kept in sync with GetMachineForAgentRecovery.
71SELECT COUNT(machine_provided)
72FROM machines
73 INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
74 LEFT JOIN work ON machines.machine_id = work.machine_id AND work.process IN ('ShepherdAccess', 'ShepherdAgentStart', 'ShepherdRecovery')
75 LEFT JOIN work_backoff ON machines.machine_id = work_backoff.machine_id AND work_backoff.until > now() AND work_backoff.process = 'ShepherdRecovery'
76 INNER JOIN machine_agent_started ON machines.machine_id = machine_agent_started.machine_id
77 LEFT JOIN machine_agent_heartbeat ON machines.machine_id = machine_agent_heartbeat.machine_id
78 LEFT JOIN machine_os_installation_request ON machines.machine_id = machine_os_installation_request.machine_id
79 LEFT JOIN machine_os_installation_report ON machines.machine_id = machine_os_installation_report.machine_id
80WHERE
81 work.machine_id IS NULL
82 AND work_backoff.machine_id IS NULL
83 -- Only act on machines where the agent is expected to have been started:
84 -- 1. If there is a pending installation request, it must not have been fulfilled already.
85 AND (
86 machine_os_installation_request.machine_id IS NULL
87 OR machine_os_installation_report.generation IS DISTINCT FROM machine_os_installation_request.generation
88 )
89 -- 2. The agent must have never heartbeat or must have stopped heartbeating.
90 AND (
91 -- No heartbeat 30 minutes after starting the agent.
92 ( machine_agent_heartbeat.machine_id IS NULL
93 AND now() > (machine_agent_started.agent_started_at + interval '30 minutes')
94 )
95 -- Heartbeats ceased for 10 minutes.
96 OR ( machine_agent_heartbeat.machine_id IS NOT NULL
97 AND now() > (machine_agent_heartbeat.agent_heartbeat_at + interval '10 minutes')
98 )
99 );
100
101-- name: ListMachineHardware :many
102SELECT
103 machine_provided.*,
104 machine_hardware_report.*
105FROM machines
106 INNER JOIN machine_provided ON machines.machine_id = machine_provided.machine_id
107 INNER JOIN machine_hardware_report ON machines.machine_id = machine_hardware_report.machine_id
108WHERE machines.machine_id > $1
109ORDER BY machines.machine_id ASC
110LIMIT $2;