|
14 | 14 |
|
15 | 15 | use std::any; |
16 | 16 | use std::borrow::Cow; |
| 17 | +use std::collections::HashMap; |
17 | 18 | use std::future::Future; |
18 | 19 | use std::io::Write; |
19 | 20 | use std::path::{Component, PathBuf}; |
@@ -144,6 +145,7 @@ struct DirectoryPath { |
144 | 145 | /// GraphQL type to provide path data for the next scan for a given instrument session |
145 | 146 | struct ScanPaths { |
146 | 147 | directory: DirectoryPath, |
| 148 | + extra_templates: HashMap<String, PathTemplate<ScanField>>, |
147 | 149 | subdirectory: Subdirectory, |
148 | 150 | } |
149 | 151 |
|
@@ -224,6 +226,12 @@ impl ScanPaths { |
224 | 226 | self.directory.info.scan_number() |
225 | 227 | } |
226 | 228 |
|
| 229 | + async fn template(&self, name: String) -> async_graphql::Result<String> { |
| 230 | + Ok(path_to_string( |
| 231 | + self.extra_templates.get(&name).unwrap().render(self), |
| 232 | + )?) |
| 233 | + } |
| 234 | + |
227 | 235 | /// The paths where the given detectors should write their files. |
228 | 236 | /// |
229 | 237 | /// Detector names are normalised before being used in file names by replacing any |
@@ -410,7 +418,7 @@ impl Query { |
410 | 418 | names: Option<Vec<String>>, |
411 | 419 | ) -> async_graphql::Result<Vec<NamedTemplate>> { |
412 | 420 | check_auth(ctx, |policy, token| { |
413 | | - policy.check_beamline_admin(token, &beamline) |
| 421 | + policy.check_instrument_admin(token, &beamline) |
414 | 422 | }) |
415 | 423 | .await?; |
416 | 424 | let db = ctx.data::<SqliteScanPathService>()?; |
@@ -455,11 +463,38 @@ impl Mutation { |
455 | 463 | warn!("Failed to increment tracker file: {e}"); |
456 | 464 | } |
457 | 465 |
|
| 466 | + let required_templates = ctx |
| 467 | + .field() |
| 468 | + .selection_set() |
| 469 | + .filter(|slct| slct.name() == "template") |
| 470 | + .flat_map(|slct| slct.arguments()) |
| 471 | + .filter_map(|args| { |
| 472 | + args.get(0).map(|arg| { |
| 473 | + let Value::String(name) = &arg.1 else { |
| 474 | + panic!("name isn't a string") |
| 475 | + }; |
| 476 | + name.into() |
| 477 | + }) |
| 478 | + }) |
| 479 | + .collect::<Vec<_>>(); |
| 480 | + let extra_templates = db |
| 481 | + .additional_templates(&instrument, required_templates) |
| 482 | + .await? |
| 483 | + .into_iter() |
| 484 | + .map(|template| { |
| 485 | + ( |
| 486 | + template.name, |
| 487 | + ScanTemplate::new_checked(&template.template).unwrap(), |
| 488 | + ) |
| 489 | + }) |
| 490 | + .collect(); |
| 491 | + |
458 | 492 | Ok(ScanPaths { |
459 | 493 | directory: DirectoryPath { |
460 | 494 | instrument_session, |
461 | 495 | info: next_scan, |
462 | 496 | }, |
| 497 | + extra_templates, |
463 | 498 | subdirectory: sub.unwrap_or_default(), |
464 | 499 | }) |
465 | 500 | } |
@@ -494,7 +529,7 @@ impl Mutation { |
494 | 529 | beamline: String, |
495 | 530 | template: TemplateInput, |
496 | 531 | ) -> async_graphql::Result<NamedTemplate> { |
497 | | - check_auth(ctx, |pc, token| pc.check_beamline_admin(token, &beamline)).await?; |
| 532 | + check_auth(ctx, |pc, token| pc.check_instrument_admin(token, &beamline)).await?; |
498 | 533 | let db = ctx.data::<SqliteScanPathService>()?; |
499 | 534 | Ok(db |
500 | 535 | .register_template(&beamline, template.name, template.template) |
|
0 commit comments