Skip to content

Commit

Permalink
feat: improve search parser for packages + options
Browse files Browse the repository at this point in the history
- move searchers into separate modules
- pass fully qualified flake identifier to the eval script
- don't search empty queries
  • Loading branch information
PhilTaken committed Mar 14, 2024
1 parent 0a9beee commit f3e2255
Show file tree
Hide file tree
Showing 10 changed files with 708 additions and 587 deletions.
12 changes: 8 additions & 4 deletions benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ iterations: 5000
rampup: 2

plan:
- name: Fetch overview
request:
url: /

- name: Search options
request:
url: /search/options?q=nginx

- name: Search short option
request:
url: /search/options?q=nix

- name: Search packages
request:
url: /search/packages?q=nginx

- name: Search short package
request:
url: /search/packages?q=nix
4 changes: 2 additions & 2 deletions nix/eval.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{branch}: let
{flake}: let
system = builtins.currentSystem;
fc-nixos = builtins.getFlake "github:flyingcircusio/fc-nixos/${branch}";
fc-nixos = builtins.getFlake flake;

versions_json =
if builtins.pathExists "${fc-nixos}/release/versions.json"
Expand Down
24 changes: 19 additions & 5 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,22 @@ impl AppState {
let branch_path = state_dir.join(branchname.clone());

debug!("starting searcher for branch {}", &branchname);
let searcher = ChannelSearcher::new(&branch_path, &flake);
let searcher = ChannelSearcher::new(&branch_path, flake);

// attempt not to (re)build multiple channels at the same time by spreading them 5
// minutes apart
let freq = Duration::from_hours(5);
let weak = if start_timers {
let freq = Duration::from_hours(5);
let start_time = tokio::time::Instant::now() + Duration::from_mins(i as u64 * 5);
let interval = interval_at(start_time, freq);
searcher.start_timer(interval)
} else {
let start_time = tokio::time::Instant::now() + Duration::from_days(100_000);
let start_time = if !searcher.active() {
tokio::time::Instant::now()
} else {
tokio::time::Instant::now() + Duration::from_days(100_000)
};
let freq = Duration::from_days(100_000);
let interval = interval_at(start_time, freq);
searcher.start_timer(interval)
};
Expand Down Expand Up @@ -172,7 +177,12 @@ async fn search_options_handler<'a>(
headers: HeaderMap,
form: axum::extract::Form<SearchForm>,
) -> impl IntoResponse {
let search_results = search_with_channel(&state, &form.channel, |c| c.search_options(&form.q));
let search_results = if !form.q.is_empty() {
search_with_channel(&state, &form.channel, |c| c.search_options(&form.q))
} else {
Vec::new()
};

if headers.contains_key("HX-Request") {
let template = OptionItemTemplate {
results: search_results,
Expand All @@ -194,7 +204,11 @@ async fn search_packages_handler<'a>(
headers: HeaderMap,
form: axum::extract::Form<SearchForm>,
) -> impl IntoResponse {
let search_results = search_with_channel(&state, &form.channel, |c| c.search_packages(&form.q));
let search_results = if !form.q.is_empty() {
search_with_channel(&state, &form.channel, |c| c.search_packages(&form.q))
} else {
Vec::new()
};

if headers.contains_key("HX-Request") {
let template = PackageItemTemplate {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/simple-tantivy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use fc_search::option_to_naive;
use std::collections::HashMap;
use tempfile::TempDir;

use fc_search::search::{OptionsSearcher, Searcher};
use fc_search::search::{options::OptionsSearcher, Searcher};

fn main() -> anyhow::Result<()> {
let index_path = TempDir::new()?;
Expand Down
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ impl Flake {
}

pub fn flake_uri(&self) -> String {
format!("github:{}/{}/{}", self.owner, self.name, self.branch)
match &self.rev {
FlakeRev::Specific(r) => format!("github:{}/{}?rev={r}", self.owner, self.name),
_ => format!("github:{}/{}/{}", self.owner, self.name, self.branch),
}
}

pub fn github_base_url(&self) -> String {
Expand Down Expand Up @@ -353,3 +356,13 @@ pub fn load_packages_and_options(
let packages = serde_json::from_str(&packages_raw)?;
Ok((options, packages))
}

pub trait LogError<T> {
fn log_to_option(self, context: &str) -> Option<T>;
}

impl<T, E: Display> LogError<T> for Result<T, E> {
fn log_to_option(self, context: &str) -> Option<T> {
self.map_err(|e| error!("{}: {e}", context)).ok()
}
}
5 changes: 1 addition & 4 deletions src/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ pub fn build_options_for_fcio_branch(
HashMap<String, NaiveNixosOption>,
HashMap<String, NixPackage>,
)> {
anyhow::ensure!(flake.owner == "flyingcircusio");
anyhow::ensure!(flake.name == "fc-nixos");

let eval_nixfile = {
let data = NixFiles::get("eval.nix").unwrap().data;
let mut tmp = tempfile::NamedTempFile::new()?;
Expand All @@ -112,7 +109,7 @@ pub fn build_options_for_fcio_branch(
debug!("starting nix-instantiate");
let derivation_cmd = Command::new("nix-instantiate")
.arg(eval_nixfile.path())
.args(["--argstr", "branch", &flake.branch])
.args(["--argstr", "flake", &flake.flake_uri()])
.output()?;

drop(eval_nixfile);
Expand Down
Loading

0 comments on commit f3e2255

Please sign in to comment.