Обновить hardwareAvailabilityReport.sql
This commit is contained in:
@@ -54,10 +54,10 @@ checks_with_prev AS (
|
|||||||
SELECT
|
SELECT
|
||||||
*,
|
*,
|
||||||
LAG(status) OVER (PARTITION BY host_name ORDER BY check_time) AS prev_status,
|
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
|
FROM checks
|
||||||
),
|
),
|
||||||
--select * from (
|
|
||||||
-- 3. Определяем моменты смены статуса (начало нового интервала)
|
-- 3. Определяем моменты смены статуса (начало нового интервала)
|
||||||
status_changes AS (
|
status_changes AS (
|
||||||
SELECT
|
SELECT
|
||||||
@@ -70,16 +70,12 @@ status_changes AS (
|
|||||||
status,
|
status,
|
||||||
prev_status,
|
prev_status,
|
||||||
case
|
case
|
||||||
when DATE(next_check_time) > DATE(check_time) then date_trunc('day', check_time) + interval '23:59:59'
|
when DATE(next_check_time) > DATE(check_time) then date_trunc('day', next_check_time) + interval '00:00:00'
|
||||||
else next_check_time - interval '1 sec'
|
else next_check_time
|
||||||
end as next_check_time,
|
end as next_check_time,
|
||||||
CASE WHEN status != prev_status THEN 1
|
CASE WHEN status != prev_status THEN 1
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END AS is_start,
|
END AS is_start
|
||||||
case
|
|
||||||
when DATE(next_check_time) > DATE(check_time) then 1
|
|
||||||
else 0
|
|
||||||
end as is_24hh
|
|
||||||
FROM checks_with_prev
|
FROM checks_with_prev
|
||||||
union all
|
union all
|
||||||
SELECT
|
SELECT
|
||||||
@@ -91,11 +87,10 @@ status_changes AS (
|
|||||||
date_trunc('day', next_check_time) + interval '00:00:00' as check_time,
|
date_trunc('day', next_check_time) + interval '00:00:00' as check_time,
|
||||||
status,
|
status,
|
||||||
prev_status,
|
prev_status,
|
||||||
next_check_time - interval '1 sec',
|
next_check_time,
|
||||||
1 as is_start,
|
1 as is_start
|
||||||
0 as is_24hh
|
|
||||||
FROM checks_with_prev
|
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. Нумеруем интервалы для каждого устройства
|
-- 4. Нумеруем интервалы для каждого устройства
|
||||||
@@ -118,8 +113,7 @@ grouped_intervals AS (
|
|||||||
coordinates,
|
coordinates,
|
||||||
MIN(date_trunc('second', check_time)) AS check_time,
|
MIN(date_trunc('second', check_time)) AS check_time,
|
||||||
MAX(date_trunc('second', next_check_time)) AS next_check_time,
|
MAX(date_trunc('second', next_check_time)) AS next_check_time,
|
||||||
MAX(status) AS status,
|
MAX(status) AS status
|
||||||
MAX(is_24hh) AS is_24hh
|
|
||||||
FROM intervals
|
FROM intervals
|
||||||
GROUP BY
|
GROUP BY
|
||||||
host_name,
|
host_name,
|
||||||
@@ -148,7 +142,8 @@ daily_stats AS (
|
|||||||
coordinates,
|
coordinates,
|
||||||
DATE(check_time) AS check_date,
|
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 = 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
|
FROM grouped_intervals
|
||||||
GROUP BY
|
GROUP BY
|
||||||
host_name,
|
host_name,
|
||||||
|
|||||||
Reference in New Issue
Block a user