From 113864947e7920ae60b516b137cc8dc92a51b283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5=D1=86=D0=BA=D0=B8=D0=B9?= Date: Sun, 28 Jun 2026 21:19:01 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20hardwareAvailabilityReport.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hardwareAvailabilityReport.sql | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/hardwareAvailabilityReport.sql b/hardwareAvailabilityReport.sql index d3522ff..b19b209 100644 --- a/hardwareAvailabilityReport.sql +++ b/hardwareAvailabilityReport.sql @@ -54,10 +54,10 @@ checks_with_prev AS ( SELECT *, LAG(status) OVER (PARTITION BY host_name ORDER BY check_time) AS prev_status, - coalesce(lead(check_time) OVER (PARTITION BY host_name ORDER BY check_time), date_trunc('day', check_time) + interval '1 day - 1 sec') AS next_check_time + coalesce(lead(check_time) OVER (PARTITION BY host_name ORDER BY check_time), date_trunc('day', check_time) + interval '1 day') AS next_check_time FROM checks ), ---select * from ( + -- 3. Определяем моменты смены статуса (начало нового интервала) status_changes AS ( SELECT @@ -70,16 +70,12 @@ status_changes AS ( status, prev_status, case - when DATE(next_check_time) > DATE(check_time) then date_trunc('day', check_time) + interval '23:59:59' - else next_check_time - interval '1 sec' + when DATE(next_check_time) > DATE(check_time) then date_trunc('day', next_check_time) + interval '00:00:00' + else next_check_time end as next_check_time, CASE WHEN status != prev_status THEN 1 ELSE 0 - END AS is_start, - case - when DATE(next_check_time) > DATE(check_time) then 1 - else 0 - end as is_24hh + END AS is_start FROM checks_with_prev union all SELECT @@ -91,11 +87,10 @@ status_changes AS ( date_trunc('day', next_check_time) + interval '00:00:00' as check_time, status, prev_status, - next_check_time - interval '1 sec', - 1 as is_start, - 0 as is_24hh + next_check_time, + 1 as is_start FROM checks_with_prev - where DATE(next_check_time) > DATE(check_time) --)f where status != prev_status and date_trunc('sec', ts) = date_trunc('sec', next_check_time) + where DATE(next_check_time) > DATE(check_time) ), -- 4. Нумеруем интервалы для каждого устройства @@ -118,8 +113,7 @@ grouped_intervals AS ( coordinates, MIN(date_trunc('second', check_time)) AS check_time, MAX(date_trunc('second', next_check_time)) AS next_check_time, - MAX(status) AS status, - MAX(is_24hh) AS is_24hh + MAX(status) AS status FROM intervals GROUP BY host_name, @@ -148,7 +142,8 @@ daily_stats AS ( coordinates, DATE(check_time) AS check_date, sum(case when status = 1 then next_check_time - check_time else null end) AS available_hours, - sum(case when status = 0 then next_check_time - check_time else null end) AS unavailable_hours + sum(case when status = 0 then next_check_time - check_time else null end) AS unavailable_hours, + sum(next_check_time - check_time) AS all_hours FROM grouped_intervals GROUP BY host_name,