use crate::model::ollama::{OllamaModel, OllamaResponse};
use anyhow::Result;
use reqwest::Client;
use std::time::Duration;
#[derive(Clone)]
pub struct OllamaCollector {
client: Client,
endpoint: String,
}
impl OllamaCollector {
pub fn new(base_url: &str) -> Result<Self> {
Ok(Self {
client: Client::builder()
.connect_timeout(Duration::from_millis(500))
.timeout(Duration::from_secs(2))
.build()?,
endpoint: format!("{}/api/ps", base_url.trim_end_matches('/')),
})
}
pub async fn collect(&self) -> Result<Vec<OllamaModel>> {
Ok(self
.client
.get(&self.endpoint)
.send()
.await?
.error_for_status()?
.json::<OllamaResponse>()
.await?
.models)
}
}
#[cfg(test)]
mod tests {
use super::OllamaResponse;
#[test]
fn parses_model_response() {
let response: OllamaResponse = serde_json::from_str(r#"{"models":[{"name":"qwen3.5:4b","size":100,"size_vram":90,"processor":"100% GPU","details":{"quantization_level":"Q4_K_M"}}]}"#).unwrap();
assert_eq!(response.models[0].name, "qwen3.5:4b");
assert_eq!(
response.models[0]
.details
.as_ref()
.unwrap()
.quantization_level
.as_deref(),
Some("Q4_K_M")
);
}
}