Обновить hardwareAvailabilityReport.sql

This commit is contained in:
2026-06-28 21:19:01 +00:00
parent f3eeae39cc
commit 113864947e
+11 -16
View File
@@ -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,