with 
capacity as (
select "Cluster" as cluster, count(*) as hosts, sum(cores) as cores, round(sum(ghz)) as ghz, round(sum(ram)) as ram 
from (
	select "Cluster", "Host"
	, avg("Host_CPU_Cores") as cores, avg("Host_CPU_Cores"*"Host_CPU_Speed") as ghz, avg("Host_RAM"*.85) as ram 
	from vmreports.compute 
	where ts=(select max(ts) from vmreports.compute) 
	group by "Cluster","Host"
	order by "Cluster","Host"
) host 
group by "Cluster"),

usage as (
select "Cluster" as cluster
, sum("VM_vCPU_count") as vm_cores
, round(sum("VM_vCPU_count"*"VDC_CPU_Speed")) as vm_ghz
, round(sum("VM_CPU_Reserv_GHz")) as vm_ghz_reserved
, round(sum("VM_vCPU_count"*"VDC_CPU_Speed"*"VM_CPU_Reserv_perc"/100)) as vm_ghz_reserved_c
, round(sum("VM_vCPU_count"*"VDC_CPU_Speed"*(case when "VM_CPU_Reserv_perc" IS null then 20 when "VM_CPU_Reserv_perc"<=20 then 20 else "VM_CPU_Reserv_perc" end)/100)) as vm_ghz_promised
-- promised - зарезервированные ГГц, если включить минимальное согласованное резервирование
, round(sum("VM_RAM_Allocated")) as vm_ram_alloc
, round(sum("VM_RAM_Used")) as vm_ram_used
from vmreports.compute 
where ts=(select max(ts) from vmreports.compute) 
group by "Cluster"),

cluster_stat as (
SELECT 
 h."Cluster" as cluster
,h."Cluster_v2" as cluster_v2
,m.models
,COUNT(*) as hosts_total
,sum(CASE WHEN "Maintenance"='inMaintenance' THEN 1 ELSE 0 END) as hosts_in_maintenance
,sum(CASE WHEN "Maintenance"='notInMaintenance' THEN 1 ELSE 0 END) as hosts_in_service
,sum("CPU_Cores"*"CPU_Sockets") as cores 
,sum(CASE WHEN "Maintenance"='notInMaintenance' THEN "CPU_Cores"*"CPU_Sockets" ELSE 0 END) as cores_in_service
,round(sum(CASE WHEN "Maintenance"='notInMaintenance' THEN "CPU_Cores"*"CPU_Sockets"*"CPU_Speed" ELSE 0 END)::numeric,1) as ghz_in_service
,round(avg(CASE WHEN "Maintenance"='notInMaintenance' THEN "CPU_Speed" ELSE 0 END)::numeric,1) as ghz
,CASE WHEN sum("CPU_Cores"*"CPU_Sockets") > 0 THEN round((sum("CPU_Used_AVG"*"CPU_Cores"*"CPU_Sockets")/sum("CPU_Cores"*"CPU_Sockets"))::numeric,0) ELSE NULL END as cpu_usage_perc
FROM vmreports.host h 
LEFT OUTER JOIN (
	SELECT hi."Cluster", STRING_AGG(DISTINCT hi."Model" || ' [' || (hi.cnt::text) || '] ',', ') as models 
	FROM (
		SELECT hii."Cluster", hii."Model", count(hii."Model") as cnt 
		FROM vmreports.host hii
		WHERE hii.ts=(SELECT max(ts) FROM vmreports.host)
		GROUP BY hii."Cluster", hii."Model"
	) hi GROUP BY hi."Cluster"
) m ON (h."Cluster"=m."Cluster")
WHERE ts=(SELECT max(ts) FROM vmreports.host)
GROUP BY h."Cluster", h."Cluster_v2", m.models
ORDER BY h."Cluster"
)

select h.cluster
,h.cluster_v2
,h.models
,c.hosts, c.cores as Cores_Capacity
, c.ghz as GHz_Capacity, u.vm_ghz_reserved, u.vm_ghz_promised, round((u.vm_ghz_promised/c.ghz)::numeric,2) as GHz_provision_rate
, c.ram as RAM_Capacity, u.vm_ram_alloc, u.vm_ram_used, round((u.vm_ram_alloc/c.ram)::numeric,2) as GB_provision_rate
from cluster_stat h
left outer join usage u on (h.cluster=u.cluster)
left outer join capacity c on (h.cluster=c.cluster)