Skip to content

Commit

Permalink
Merge pull request #26 from ALPHA-g-Experiment/ext
Browse files Browse the repository at this point in the history
Allow filename pattern selection
  • Loading branch information
DJDuque authored Sep 4, 2024
2 parents 4beecc0 + c1e5582 commit c5d54fe
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ changes will be documented in this file.
This contains all the information from the sequencer events, but also includes
the counts during each spill for all Chronobox channels.
- Download tabs now have a small spinner to indicate that processing is ongoing.
- It is now possible to specify (via the `--pattern` flag) a filename pattern
regex for the MIDAS files given a run number. This is useful in the case that
there are both `*.mid` and `*.mid.lz4` files in the data directory. The
default pattern will match any `runXXXXXsubYYY.mid*` file. To, for example,
match only `*.mid` files, add a `$` anchor to the end of the default pattern.

## [0.1.1] - 2024-08-24

Expand Down
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ futures = "0.3.30"
indicatif = "0.17.8"
jsonwebtoken = "9.3.0"
rand = "0.8.5"
regex = "1.10.6"
semver = "1.0.23"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ After=network.target

[Service]
Environment="AG_JWT_SECRET=a_random_shared_secret"
ExecStart=/home/my_user/.cargo/bin/alpha-g-data-handler serve -a 0.0.0.0:8080 -d /path/to/midas/files
ExecStart=/home/my_user/.cargo/bin/alpha-g-data-handler serve -a 0.0.0.0:8080 -d /path/to/midas/files -p "^run0*(?<run_number>\d+)sub\d+\.mid\.lz4$"
WorkingDirectory=/home/my_user
User=my_user
Restart=always
Expand Down
17 changes: 12 additions & 5 deletions src/core_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ pub(super) fn install_core_binaries() -> Result<()> {
Ok(())
}

// This is set (only once) at the beginning of the program based on the CLI
// These are set (only once) at the beginning of the program based on the CLI
// arguments.
// Throughout the module it should be assumed to be set.
// Throughout the module they should be assumed to be set.
pub(super) static MIDAS_DATA_PATH: OnceLock<PathBuf> = OnceLock::new();
pub(super) static FILENAME_PATTERN: OnceLock<regex::Regex> = OnceLock::new();

// Get all the MIDAS files for a given run number.
// If the returned value is OK, the vector is guaranteed to be non-empty.
async fn midas_files(run_number: u32) -> Result<Vec<PathBuf>> {
let dir = MIDAS_DATA_PATH.get().unwrap();
let prefix = format!("run{run_number:05}sub");
let re = FILENAME_PATTERN.get().unwrap();

let mut files = Vec::new();
let mut entries = fs::read_dir(dir)
Expand All @@ -57,8 +58,14 @@ async fn midas_files(run_number: u32) -> Result<Vec<PathBuf>> {
.await
.with_context(|| format!("failed to iterate over `{}`", dir.display()))?
{
if let Some(filename) = entry.file_name().to_str() {
if filename.starts_with(&prefix) {
if let Some(n) = entry
.file_name()
.to_str()
.and_then(|filename| re.captures(filename))
.and_then(|captures| captures.name("run_number"))
.and_then(|n| n.as_str().parse::<u32>().ok())
{
if n == run_number {
files.push(entry.path());
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ enum Commands {
/// Path to the MIDAS data directory
#[arg(short, long, default_value = ".")]
data_dir: std::path::PathBuf,
/// Filename pattern
#[arg(short, long, default_value = r"^run0*(?<run_number>\d+)sub\d+\.mid")]
pattern: regex::Regex,
},
}

Expand Down Expand Up @@ -97,10 +100,17 @@ async fn main() -> Result<(), anyhow::Error> {

spinner.finish_and_clear();
}
Commands::Serve { address, data_dir } => {
Commands::Serve {
address,
data_dir,
pattern,
} => {
core_command::MIDAS_DATA_PATH
.set(data_dir)
.expect("failed to set MIDAS_DATA_PATH");
core_command::FILENAME_PATTERN
.set(pattern)
.expect("failed to set FILENAME_PATTERN");

let app_state = Arc::new(AppState::default());
let app = Router::new()
Expand Down

0 comments on commit c5d54fe

Please sign in to comment.