Skip to content

Commit 492cd35

Browse files
Expand configuration query
Expand configuration query to be able to return multiple configurations. Change name to configurations. Make beamline parameter optional (and rename to beamlineFilter). When a filter is provided, only details for that beamline are returned, otherwise all beamline configurations are returned. Also add a test for the multi-configuration DB query and update the example query in the README.
1 parent 89f41d1 commit 492cd35

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ Get the visit directory for a beamline and visit
109109
}
110110
```
111111

112-
#### configuration
112+
#### configurations
113113
Get the current configuration values for the given beamline
114114

115115
##### Query
116116
```graphql
117117
{
118-
configuration(beamline: "i22") {
118+
configurations(beamlineFilter: "i22") {
119119
visitTemplate
120120
scanTemplate
121121
detectorTemplate
@@ -127,12 +127,14 @@ Get the current configuration values for the given beamline
127127
##### Response
128128
```json
129129
{
130-
"configuration": {
131-
"visitTemplate": "/data/{instrument}/data/{year}/{visit}",
132-
"scanTemplate": "{subdirectory}/{instrument}-{scan_number}",
133-
"detectorTemplate": "{subdirectory}/{instrument}-{scan_number}-{detector}",
134-
"latestScanNumber": 20839
135-
}
130+
"configurations": [
131+
{
132+
"visitTemplate": "/tmp/{instrument}/data/{year}/{visit}",
133+
"scanTemplate": "{subdirectory}/{instrument}-{scan_number}",
134+
"detectorTemplate": "{subdirectory}/{instrument}-{scan_number}-{detector}",
135+
"latestScanNumber": 12345
136+
}
137+
]
136138
}
137139
```
138140

src/db_service.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ impl SqliteScanPathService {
303303
.ok_or(ConfigurationError::MissingBeamline(beamline.into()))
304304
}
305305

306+
pub async fn configurations(&self) -> Result<Vec<BeamlineConfiguration>, ConfigurationError> {
307+
Ok(query_as!(DbBeamlineConfig, "SELECT * FROM beamline")
308+
.fetch_all(&self.pool)
309+
.await?
310+
.into_iter()
311+
.map(BeamlineConfiguration::from)
312+
.collect())
313+
}
314+
306315
pub async fn next_scan_configuration(
307316
&self,
308317
beamline: &str,
@@ -604,6 +613,33 @@ mod db_tests {
604613
assert_eq!(fb.extension, "ext");
605614
}
606615

616+
#[rstest]
617+
#[test]
618+
async fn configurations(#[future(awt)] db: SqliteScanPathService) {
619+
let confs = ok!(db.configurations());
620+
assert_eq!(confs.len(), 1);
621+
let conf = confs.first().unwrap();
622+
assert_eq!(conf.name(), "i22");
623+
assert_eq!(conf.scan_number(), 122);
624+
assert_eq!(
625+
conf.visit().unwrap().to_string(),
626+
"/tmp/{instrument}/data/{year}/{visit}"
627+
);
628+
assert_eq!(
629+
conf.scan().unwrap().to_string(),
630+
"{subdirectory}/{instrument}-{scan_number}"
631+
);
632+
assert_eq!(
633+
conf.detector().unwrap().to_string(),
634+
"{subdirectory}/{instrument}-{scan_number}-{detector}"
635+
);
636+
let Some(fb) = conf.fallback() else {
637+
panic!("Missing fallback configuration");
638+
};
639+
assert_eq!(fb.directory, "/tmp/trackers");
640+
assert_eq!(fb.extension, "ext");
641+
}
642+
607643
type Update = BeamlineConfigurationUpdate;
608644

609645
#[rstest]

src/graphql.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,23 @@ impl Query {
255255
}
256256

257257
#[instrument(skip(self, ctx))]
258-
async fn configuration(
258+
async fn configurations(
259259
&self,
260260
ctx: &Context<'_>,
261-
beamline: String,
262-
) -> async_graphql::Result<BeamlineConfiguration> {
261+
beamline_filter: Option<String>,
262+
) -> async_graphql::Result<Vec<BeamlineConfiguration>> {
263263
let db = ctx.data::<SqliteScanPathService>()?;
264-
trace!("Getting config for {beamline:?}");
265-
Ok(db.current_configuration(&beamline).await?)
264+
match beamline_filter {
265+
Some(filter) => {
266+
trace!("Getting configs matching {filter:?}");
267+
let singleton = db.current_configuration(&filter).await?;
268+
Ok(vec![singleton])
269+
}
270+
None => {
271+
trace!("Getting all configs");
272+
Ok(db.configurations().await?)
273+
}
274+
}
266275
}
267276
}
268277

0 commit comments

Comments
 (0)