-----------------------------------------------------------------------------------------------------------------------------------------------
-- Query SQL / API per il monitoragio dello stato di caricamento delle serie storiche di osservazioni
-----------------------------------------------------------------------------------------------------------------------------------------------


-- Numero osservazioni per centralina e osservabile (filtrato su finestra temporale)

SELECT th."NAME" AS centralina, 
       ds."NAME" AS osservabile, 
       COUNT(*) AS cnt 
FROM "OBSERVATIONS" obs
LEFT JOIN "DATASTREAMS" ds ON ds."ID" = obs."DATASTREAM_ID"
LEFT JOIN "THINGS" th ON th."ID" = ds."THING_ID"
WHERE th."NAME" LIKE '%LS0623020122%' 
  AND obs."RESULT_TIME" >= '2024-06-07T14:52:32Z'::timestamptz
  AND obs."RESULT_TIME" < '2024-06-07T14:53:00Z'::timestamptz
GROUP BY th."NAME", ds."NAME" 
ORDER BY 1, 2;



-----------------------------------------------------------------------------------------------------------------------------------------------
-- Numero osservazioni per centralina e mese in confronto con il numero teorico aspettato (assumendo una misura ogni 5 minuti)
-- Prima di lanciarla verificare che siano coperti tutti i mesi di interesse (vanno scritti a mano nella query)

with base as (
	select 
		th."PROPERTIES"->>'localIdentifier' as local_identifier, th."PROPERTIES"->>'identifier' as identifier, 
		substring((obs."PHENOMENON_TIME_START" at time zone 'UTC')::text, 1, 7) as "month", count(*) cnt
	from "OBSERVATIONS" obs
	left join "DATASTREAMS" ds on ds."ID" = obs."DATASTREAM_ID"
	left join "THINGS" th on th."ID" = ds."THING_ID"
	group by 1, 2, 3
)
select
	local_identifier, identifier,
	sum(case when "month" = '2023-07' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2023-07' then n_ds * n_days * 288 else 0 end)::text as "2023-07",
	sum(case when "month" = '2023-08' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2023-08' then n_ds * n_days * 288 else 0 end)::text as "2023-08",
	sum(case when "month" = '2023-09' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2023-09' then n_ds * n_days * 288 else 0 end)::text as "2023-09",
	sum(case when "month" = '2023-10' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2023-10' then n_ds * n_days * 288 else 0 end)::text as "2023-10",
	sum(case when "month" = '2023-11' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2023-11' then n_ds * n_days * 288 else 0 end)::text as "2023-11",
	sum(case when "month" = '2023-12' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2023-12' then n_ds * n_days * 288 else 0 end)::text as "2023-12",
	sum(case when "month" = '2024-01' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2024-01' then n_ds * n_days * 288 else 0 end)::text as "2024-01",
	sum(case when "month" = '2024-02' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2024-02' then n_ds * n_days * 288 else 0 end)::text as "2024-02",
	sum(case when "month" = '2024-03' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2024-03' then n_ds * n_days * 288 else 0 end)::text as "2024-03",
	sum(case when "month" = '2024-04' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2024-04' then n_ds * n_days * 288 else 0 end)::text as "2024-04",
	sum(case when "month" = '2024-05' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2024-05' then n_ds * n_days * 288 else 0 end)::text as "2024-05",
	sum(case when "month" = '2024-06' then cnt else 0 end)::text || ' / ' || sum(case when "month" = '2024-06' then n_ds * n_days * 288 else 0 end)::text as "2024-06"
from (
	select 
		list.*,  
		(case when list.local_identifier in ('LS0623020166', 'LS0623020165', 'LS0623020164', 'LS0623020163') then 11 else 10 end) n_ds,
		coalesce(base.cnt, 0) cnt
	from (
		select local_identifier, identifier, "month", n_days from (
			select '2023-07' as "month", 31 n_days UNION
			select '2023-08' as "month", 31 n_days UNION
			select '2023-09' as "month", 30 n_days UNION
			select '2023-10' as "month", 31 n_days UNION
			select '2023-11' as "month", 30 n_days UNION
			select '2023-12' as "month", 31 n_days UNION
			select '2024-01' as "month", 31 n_days UNION
			select '2024-02' as "month", 29 n_days UNION
			select '2024-03' as "month", 31 n_days UNION
			select '2024-04' as "month", 30 n_days UNION
			select '2024-05' as "month", 31 n_days UNION
			select '2024-06' as "month", 30 n_days
		) m, (select distinct local_identifier, identifier from base) id
	) list
	left join base on list.local_identifier = base.local_identifier and list."month" = base."month"
	--order by list.local_identifier, list."month"
) goo
group by 1, 2 order by 2;




-----------------------------------------------------------------------------------------------------------------------------------------------

Per avere la lista di tutti i datastream di tutte le locations e i timestamp dell'ultima osservazione caricata in ogni datastream
https://frost.labservice.it/FROST-Server/v1.1/Locations?$select=name&$expand=Things($select=id),Things/Datastreams($select=name,phenomenonTime)


Come il precedente, ma vengono elencati solo i datastream in ritardo rispetto ad una specifica data
https://frost.labservice.it/FROST-Server/v1.1/Locations?$select=name&$expand=Things($select=id),Things/Datastreams($select=name,phenomenonTime;$filter=phenomenonTime lt 2024-07-01T00:00:00Z)



-----------------------------------------------------------------------------------------------------------------------------------------------
Soluzioni temporanee (perchè non efficienti) per avere il conteggio delle osservazioni presenti nel server per un dato pilot


https://kdp-ediaqi.know-center.at/FROST-Server/v1.1/Observations?$select=id&$expand=Datastream($select=properties/identifier;$filter=substringof('.FE.',properties/identifier))


https://kdp-ediaqi.know-center.at/FROST-Server/v1.1/Datastreams?$select=name&$filter=substringof('.FE.',properties/identifier)&$expand=Observations($select=id)&$count=true


-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------


PROPOSTE DI DASHBOARD PER EDIAQI

In tutte e tre le tabelle si deve poter specificare in anticipo quali sono i mese/anno rilevanti (anche semplicemente indicando un range)


1) Una unica tabella che descrive 
- il numero osservazioni per centralina e mese in confronto con il numero teorico aspettato (ANALISI BUCHI)
- il range di date disponibili per centralina (ANALISI RITARDI)

Numero seriale centralina (th."PROPERTIES"->>'localIdentifier')
ID univoco EDIAQI (th."PROPERTIES"->>'identifier')
conteggio reale (per ogni mese/anno rilevante)
conteggio aspettato (per ogni mese/anno rilevante)
prima misura disponibile (se i datastreams non sono concordi, indicare il range)
ultma misura disponibile (se i datastreams non sono concordi, indicare il range)
flag ritardo (da capire come gestirlo se nei campi sopra c'è un range)


2) Una tabella per centralina che descrive :
- il numero osservazioni per osservabile e mese in confronto con il numero teorico aspettato (ANALISI BUCHI)
- il range di date disponibili per osservabile (ANALISI RITARDI)

Nome osservabile (ds."NAME")
conteggio reale (per ogni mese/anno rilevante)
conteggio aspettato (per ogni mese/anno rilevante)
prima misura disponibile 
ultma misura disponibile
flag ritardo


3) Una unica tabella che confronta tra due server FROST (A e B): 
- il numero osservazioni presenti per centralina e mese 
- il range di date disponibili per centralina

Numero seriale centralina (th."PROPERTIES"->>'localIdentifier')
ID univoco EDIAQI (th."PROPERTIES"->>'identifier')
conteggio server A (per ogni mese/anno rilevante)
conteggio server B (per ogni mese/anno rilevante)
conteggio aspettato (per ogni mese/anno rilevante)
prima misura disponibile server A [*]
prima misura disponibile server B [*]
ultma misura disponibile server A [*]
ultma misura disponibile server B [*]

[*] se i datastreams non sono concordi, indicare il range

Endpoint generale:https://frost.labservice.it/FROST-Server/v1.1/
utente: read
password: read-Quieta-Sm\si

Elenco di tutte le centraline disponibili:
https://frost.labservice.it/FROST-Server/v1.1/Things

--> tabella con l'elenco delle things 

Elenco di tutte le locations (dove sono poste le centraline):
https://frost.labservice.it/FROST-Server/v1.1/Locations
https://frost.labservice.it/FROST-Server/v1.1/Locations?$select=name
https://frost.labservice.it/FROST-Server/v1.1/Locations?$select=name&$expand=Things

Elenco di tutte le osservazioni disponibili:
https://frost.labservice.it/FROST-Server/v1.1/Observations

Elenco di tutti i Datastreams:
https://frost.labservice.it/FROST-Server/v1.1/Datastreams




PYTHON parametro --> ediaqi




https://frost.labservice.it/FROST-Server/v1.1/Fornitori(LIKE 'AIRBREAK')

FOrnitori airbreak
.......