@@ -21,7 +21,9 @@ pub struct Stinfosys {
2121}
2222
2323type StationTotimeMap = HashMap < i32 , DateTime < Utc > > ;
24+ type StationFromtimeMap = HashMap < i32 , DateTime < Utc > > ;
2425type ObsPgmTotimeMap = HashMap < MetTimeseriesKey , DateTime < Utc > > ;
26+ type ObsPgmFromtimeMap = HashMap < MetTimeseriesKey , DateTime < Utc > > ;
2527
2628impl Stinfosys {
2729 pub fn new ( conn_string : String , levels : LevelTable ) -> Self {
@@ -36,6 +38,8 @@ impl Stinfosys {
3638 ) -> Result <
3739 (
3840 HashMap < i32 , DateTime < Utc > > ,
41+ HashMap < i32 , DateTime < Utc > > ,
42+ HashMap < MetTimeseriesKey , DateTime < Utc > > ,
3943 HashMap < MetTimeseriesKey , DateTime < Utc > > ,
4044 ) ,
4145 Error ,
@@ -49,37 +53,61 @@ impl Stinfosys {
4953 } ) ;
5054
5155 // Fetch all deactivated timeseries in Stinfosys
52- let ( station_totime, obs_pgm_totime) = tokio:: try_join!(
56+ let ( station_totime, station_fromtime , obs_pgm_totime, obs_pgm_fromtime ) = tokio:: try_join!(
5357 fetch_station_totime( & client) ,
58+ fetch_station_fromtime( & client) ,
5459 fetch_obs_pgm_totime( self . levels. clone( ) , & client) ,
60+ fetch_obs_pgm_fromtime( self . levels. clone( ) , & client) ,
5561 ) ?;
5662
57- Ok ( ( station_totime, obs_pgm_totime) )
63+ Ok ( (
64+ station_totime,
65+ station_fromtime,
66+ obs_pgm_totime,
67+ obs_pgm_fromtime,
68+ ) )
5869 }
5970}
6071
6172pub async fn fetch_deactivated (
6273 obs_pgm_totime : & HashMap < MetTimeseriesKey , DateTime < Utc > > ,
74+ obs_pgm_fromtime : & HashMap < MetTimeseriesKey , DateTime < Utc > > ,
6375 station_totime : & HashMap < i32 , DateTime < Utc > > ,
76+ station_fromtime : & HashMap < i32 , DateTime < Utc > > ,
6477 labels : Vec < MetLabel > ,
6578) -> Result < Vec < DeactivatedTimeseries > , Error > {
6679 let mut futures = labels
6780 . iter ( )
6881 . map ( async |label| -> Result < _ , Error > {
69- // Prefer obs_pgm if available
82+ // TODO: Figure out how best to set these when obs_pgm makes no sense ...
83+ // particularly when it does not overlap at all with the time range
84+ // |------obs_pgm------|
85+ // |--station--|
86+ // but also if the from or to from obs_pgm actually open beyond when the
87+ // station had data aka:
88+ // |------obs_pgm------|
89+ // |--station--|
7090 let totime = obs_pgm_totime
7191 . get ( & label. key )
7292 . or ( station_totime. get ( & label. key . station_id ) )
7393 . copied ( ) ;
94+ let fromtime = obs_pgm_fromtime
95+ . get ( & label. key )
96+ . or ( station_fromtime. get ( & label. key . station_id ) )
97+ . copied ( ) ;
7498
75- Ok ( ( label. id , totime) )
99+ Ok ( ( label. id , fromtime , totime) )
76100 } )
77101 . collect :: < FuturesUnordered < _ > > ( ) ;
78102
79103 let mut deactivated = vec ! [ ] ;
80104 while let Some ( res) = futures. next ( ) . await {
81105 let ts = match res? {
82- ( tsid, Some ( totime) ) => DeactivatedTimeseries { tsid, totime } ,
106+ ( tsid, Some ( fromtime) , Some ( totime) ) => DeactivatedTimeseries {
107+ tsid,
108+ fromtime,
109+ totime,
110+ } ,
83111 // Skip if a valid totime was not found in stinfosys
84112 _ => continue ,
85113 } ;
@@ -133,6 +161,46 @@ async fn fetch_obs_pgm_totime(levels: LevelTable, conn: &Client) -> Result<ObsPg
133161 Ok ( map)
134162}
135163
164+ async fn fetch_obs_pgm_fromtime (
165+ levels : LevelTable ,
166+ conn : & Client ,
167+ ) -> Result < ObsPgmFromtimeMap , Error > {
168+ const OBS_PGM_QUERY : & str = "\
169+ SELECT \
170+ stationid, \
171+ paramid, \
172+ hlevel, \
173+ nsensor, \
174+ priority_messageid, \
175+ (ARRAY_AGG(fromtime ORDER BY fromtime ASC))[1] \
176+ FROM obs_pgm \
177+ GROUP BY stationid, paramid, hlevel, nsensor, priority_messageid \
178+ HAVING (ARRAY_AGG(fromtime ORDER BY fromtime ASC))[1] IS NOT NULL";
179+
180+ let rows = conn. query ( OBS_PGM_QUERY , & [ ] ) . await ?;
181+
182+ let mut map = ObsPgmFromtimeMap :: new ( ) ;
183+ for row in rows {
184+ let param_id: i32 = row. get ( 1 ) ;
185+
186+ let level = row. get ( 2 ) ;
187+ let level = param_get_level ( levels. clone ( ) , param_id, level) ?;
188+
189+ let key = MetTimeseriesKey {
190+ station_id : row. get ( 0 ) ,
191+ param_id,
192+ level,
193+ sensor : row. get ( 3 ) ,
194+ type_id : row. get ( 4 ) ,
195+ } ;
196+
197+ let totime: NaiveDateTime = row. get ( 5 ) ;
198+ map. insert ( key, totime. and_utc ( ) ) ;
199+ }
200+
201+ Ok ( map)
202+ }
203+
136204async fn fetch_station_totime ( conn : & Client ) -> Result < StationTotimeMap , Error > {
137205 // The funny looking ARRAY_AGG is needed because each station can have multiple from/to times.
138206 // For example, the timeseries might have been "reset" after a change of the station position,
@@ -159,3 +227,24 @@ async fn fetch_station_totime(conn: &Client) -> Result<StationTotimeMap, Error>
159227 } )
160228 . collect ( ) )
161229}
230+
231+ async fn fetch_station_fromtime ( conn : & Client ) -> Result < StationFromtimeMap , Error > {
232+ const STATION_QUERY : & str = "\
233+ SELECT \
234+ stationid, \
235+ (ARRAY_AGG(fromtime ORDER BY fromtime ASC))[1] \
236+ FROM station \
237+ GROUP BY stationid \
238+ HAVING (ARRAY_AGG(fromtime ORDER BY fromtime ASC))[1] IS NOT NULL";
239+
240+ let rows = conn. query ( STATION_QUERY , & [ ] ) . await ?;
241+
242+ Ok ( rows
243+ . iter ( )
244+ . map ( |row| {
245+ let totime: NaiveDateTime = row. get ( 1 ) ;
246+
247+ ( row. get ( 0 ) , totime. and_utc ( ) )
248+ } )
249+ . collect ( ) )
250+ }
0 commit comments