Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::any;
use std::borrow::Cow;
use std::collections::HashMap;
use std::future::Future;
use std::io::Write;
use std::path::{Component, PathBuf};
Expand Down Expand Up @@ -145,6 +146,7 @@ struct DirectoryPath {
struct ScanPaths {
directory: DirectoryPath,
subdirectory: Subdirectory,
metadata: HashMap<String, String>,
}

/// GraphQL type to provide current configuration for an instrument
Expand All @@ -153,6 +155,12 @@ struct CurrentConfiguration {
high_file: Option<u32>,
}

#[derive(Debug, InputObject)]
struct MetaKeyValue {
key: String,
value: String,
}

/// Error to be returned when a path contains non-unicode characters
#[derive(Debug, Display, Error)]
#[display("Path contains non-unicode characters")]
Expand Down Expand Up @@ -308,6 +316,12 @@ impl FieldSource<ScanField> for ScanPaths {
ScanField::Subdirectory => self.subdirectory.to_string().into(),
ScanField::ScanNumber => self.directory.info.scan_number().to_string().into(),
ScanField::Directory(dir) => self.directory.resolve(dir),
ScanField::Custom(key) => self
.metadata
.get(key)
.map(|s| s.as_str())
.unwrap_or("")
.into(),
}
}
}
Expand Down Expand Up @@ -397,6 +411,7 @@ impl Mutation {
instrument: String,
instrument_session: String,
sub: Option<Subdirectory>,
meta: Option<Vec<MetaKeyValue>>,
) -> async_graphql::Result<ScanPaths> {
check_auth(ctx, |policy, token| {
policy.check_access(token, &instrument, &instrument_session)
Expand All @@ -420,11 +435,18 @@ impl Mutation {
warn!("Failed to increment tracker file: {e}");
}

let metadata = meta
.into_iter()
.flatten()
.map(|kv| (kv.key, kv.value))
.collect();

Ok(ScanPaths {
directory: DirectoryPath {
instrument_session,
info: next_scan,
},
metadata,
subdirectory: sub.unwrap_or_default(),
})
}
Expand Down
13 changes: 8 additions & 5 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ pub enum DirectoryField {
Instrument,
}

#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Display, Clone, PartialEq, Eq, Hash)]
pub enum ScanField {
#[display("subdirectory")]
Subdirectory,
#[display("scan_number")]
ScanNumber,
#[display("{_0}")]
Directory(DirectoryField),
#[display("{_0}")]
Custom(String),
}

#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Display, Clone, PartialEq, Eq, Hash)]
pub enum DetectorField {
#[display("detector")]
Detector,
Expand Down Expand Up @@ -73,7 +75,10 @@ impl TryFrom<String> for ScanField {
match value.as_str() {
"scan_number" => Ok(ScanField::ScanNumber),
"subdirectory" => Ok(ScanField::Subdirectory),
_ => Ok(ScanField::Directory(DirectoryField::try_from(value)?)),
_ => match DirectoryField::try_from(value) {
Ok(bf) => Ok(ScanField::Directory(bf)),
Err(InvalidKey(value)) => Ok(ScanField::Custom(value)),
},
}
}
}
Expand Down Expand Up @@ -235,7 +240,6 @@ mod paths_tests {
#[case::invalid_path_incomplete("data/{unclosed", TemplateErrorType::Incomplete)]
#[case::invalid_path_empty("data/{}", TemplateErrorType::Empty)]
#[case::invalid_path_nested("data/{nes{ted}}", TemplateErrorType::Nested)]
#[case::invalid_path_unrecognised("data/{detector}", TemplateErrorType::Unrecognised)]
fn invalid_scan<E: PartialEq<InvalidPathTemplate> + Debug>(
#[case] template: &str,
#[case] err: E,
Expand All @@ -251,7 +255,6 @@ mod paths_tests {
#[case::invalid_path_incomplete("data/{unclosed", TemplateErrorType::Incomplete)]
#[case::invalid_path_empty("data/{}", TemplateErrorType::Empty)]
#[case::invalid_path_nested("data/{nes{ted}}", TemplateErrorType::Nested)]
#[case::invalid_path_unrecognised("data/{unknown}", TemplateErrorType::Unrecognised)]
fn invalid_detector<E: PartialEq<InvalidPathTemplate> + Debug>(
#[case] template: &str,
#[case] err: E,
Expand Down
7 changes: 6 additions & 1 deletion static/service_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ A template describing the path to the data directory for a given instrument sess
"""
scalar DirectoryTemplate

input MetaKeyValue {
key: String!
value: String!
}

"""
Queries that modify the state of the numtracker configuration in some way
"""
type Mutation {
"""
Generate scan file locations for the next scan
"""
scan(instrument: String!, instrumentSession: String!, sub: Subdirectory): ScanPaths!
scan(instrument: String!, instrumentSession: String!, sub: Subdirectory, meta: [MetaKeyValue!]): ScanPaths!
"""
Add or modify the stored configuration for an instrument
"""
Expand Down
Loading