CREATE VIEW vwrealtimemonitoroverview AS (
WITH service_rows AS (
SELECT realtimeadaptermonitordata.keyid,
realtimeadaptermonitordata.servicename,
realtimeadaptermonitordata.status,
realtimeadaptermonitordata.isrunning,
realtimeadaptermonitordata.eventsprocessed,
realtimeadaptermonitordata.errorcount,
realtimeadaptermonitordata.activeconnections,
realtimeadaptermonitordata.totalwebsockets,
realtimeadaptermonitordata.activeconversations,
realtimeadaptermonitordata.staleconversationcount,
realtimeadaptermonitordata.oldeststaleconversationageseconds,
realtimeadaptermonitordata.healthpercentage,
realtimeadaptermonitordata.acwpendingcount,
realtimeadaptermonitordata.staleacwcount,
realtimeadaptermonitordata.lastactivityutc,
realtimeadaptermonitordata.lastconversationnotificationutc,
realtimeadaptermonitordata.lastconversationhealthcheckutc,
realtimeadaptermonitordata.lastconversationverifiedactiveutc,
realtimeadaptermonitordata.updated,
realtimeadaptermonitordata.notes
FROM realtimeadaptermonitordata
WHERE ((realtimeadaptermonitordata.servicename IS NOT NULL) AND ((realtimeadaptermonitordata.keyid)::text = (realtimeadaptermonitordata.servicename)::text))
), health_rows AS (
SELECT replace((realtimeadaptermonitordata.keyid)::text, 'RealtimeMonitorService:service:'::text, ''::text) AS servicename,
realtimeadaptermonitordata.healthpercentage,
realtimeadaptermonitordata.updated AS healthupdated,
realtimeadaptermonitordata.notes AS healthnotes
FROM realtimeadaptermonitordata
WHERE ((realtimeadaptermonitordata.keyid)::text ~~ 'RealtimeMonitorService:service:%'::text)
), overview AS (
SELECT sr.servicename,
sr.status,
sr.isrunning,
COALESCE(hr.healthpercentage, sr.healthpercentage) AS healthpercentage,
sr.eventsprocessed,
sr.errorcount,
sr.activeconnections,
sr.totalwebsockets,
sr.activeconversations,
sr.staleconversationcount,
sr.oldeststaleconversationageseconds,
((sr.oldeststaleconversationageseconds)::numeric / 60.0) AS oldeststaleconversationageminutes,
sr.acwpendingcount,
sr.staleacwcount,
sr.lastactivityutc,
sr.lastconversationnotificationutc,
sr.lastconversationhealthcheckutc,
sr.lastconversationverifiedactiveutc,
sr.updated,
CASE
WHEN (sr.updated IS NULL) THEN NULL::integer
ELSE GREATEST(0, (floor(EXTRACT(epoch FROM (timezone('utc'::text, now()) - sr.updated))))::integer)
END AS monitorrowageseconds,
hr.healthupdated,
CASE
WHEN (hr.healthupdated IS NULL) THEN NULL::integer
ELSE GREATEST(0, (floor(EXTRACT(epoch FROM (timezone('utc'::text, now()) - hr.healthupdated))))::integer)
END AS healthrowageseconds,
sr.notes,
hr.healthnotes
FROM (service_rows sr
LEFT JOIN health_rows hr ON ((hr.servicename = (sr.servicename)::text)))
)
SELECT overview.servicename,
CASE
WHEN ((overview.servicename)::text = 'RealtimeMonitorService'::text) THEN 'Overall Realtime Health'::character varying
ELSE overview.servicename
END AS displayname,
CASE
WHEN ((overview.servicename)::text = 'RealtimeMonitorService'::text) THEN 'Overall'::text
ELSE 'Service'::text
END AS healthscope,
overview.status,
overview.isrunning,
overview.healthpercentage,
CASE
WHEN (overview.healthpercentage IS NULL) THEN 'Unknown'::text
WHEN (overview.healthpercentage >= (90)::numeric) THEN 'Healthy'::text
WHEN (overview.healthpercentage >= (75)::numeric) THEN 'Degraded'::text
WHEN (overview.healthpercentage >= (60)::numeric) THEN 'Warning'::text
ELSE 'Critical'::text
END AS healthstatus,
((overview.healthpercentage IS NOT NULL) AND (overview.healthpercentage < (60)::numeric) AND ((COALESCE(overview.healthnotes, ''::character varying))::text !~~ '%restart_suppressed=true%'::text)) AS restartrecommended,
((COALESCE(overview.healthnotes, ''::character varying))::text ~~ '%restart_suppressed=true%'::text) AS restartsuppressed,
CASE
WHEN ((COALESCE(overview.healthnotes, ''::character varying))::text ~~ '%restart_suppression_reason=outside_business_hours%'::text) THEN 'outside_business_hours'::text
ELSE NULL::text
END AS restartpolicyreason,
overview.eventsprocessed,
overview.errorcount,
overview.activeconnections,
overview.totalwebsockets,
overview.activeconversations,
overview.staleconversationcount,
overview.oldeststaleconversationageseconds,
overview.oldeststaleconversationageminutes,
overview.acwpendingcount,
overview.staleacwcount,
overview.lastactivityutc,
overview.lastconversationnotificationutc,
overview.lastconversationhealthcheckutc,
overview.lastconversationverifiedactiveutc,
overview.updated,
overview.monitorrowageseconds,
overview.healthupdated,
overview.healthrowageseconds,
overview.notes,
overview.healthnotes
FROM overview
)