Serge Bazanski | 6f59951 | 2023-04-26 19:08:19 +0200 | [diff] [blame^] | 1 | -- name: CountActiveWork :many |
| 2 | -- Return number of active work, grouped by process. |
| 3 | SELECT COUNT(*), work.process |
| 4 | FROM work |
| 5 | GROUP BY (work.process); |
| 6 | |
| 7 | -- name: CountActiveBackoffs :many |
| 8 | -- Return number of active backoffs, grouped by process. |
| 9 | SELECT COUNT(*), work_backoff.process |
| 10 | FROM work_backoff |
| 11 | GROUP BY (work_backoff.process); |
| 12 | |
| 13 | -- name: CountMachines :one |
| 14 | SELECT COUNT(*) |
| 15 | FROM machines; |
| 16 | |
| 17 | -- name: CountMachinesProvided :one |
| 18 | SELECT COUNT(*) |
| 19 | FROM machine_provided; |
| 20 | |
| 21 | -- name: CountMachinesAgentHeartbeating :one |
| 22 | SELECT COUNT(*) |
| 23 | FROM 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 |
| 26 | WHERE |
| 27 | now() < machine_agent_heartbeat.agent_heartbeat_at + interval '10 minute'; |
| 28 | |
| 29 | -- name: CountMachinesInstallationPending :one |
| 30 | SELECT COUNT(*) |
| 31 | FROM 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 |
| 35 | WHERE |
| 36 | machine_os_installation_request.generation IS DISTINCT FROM machine_os_installation_report.generation; |
| 37 | |
| 38 | -- name: CountMachinesInstallationComplete :one |
| 39 | SELECT COUNT(*) |
| 40 | FROM 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 |
| 44 | WHERE |
| 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. |
| 50 | SELECT COUNT(machine_provided) |
| 51 | FROM 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 |
| 58 | WHERE |
| 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. |
| 71 | SELECT COUNT(machine_provided) |
| 72 | FROM 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 |
| 80 | WHERE |
| 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 |
| 102 | SELECT |
| 103 | machine_provided.*, |
| 104 | machine_hardware_report.* |
| 105 | FROM 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 |
| 108 | WHERE machines.machine_id > $1 |
| 109 | ORDER BY machines.machine_id ASC |
| 110 | LIMIT $2; |