langfuse_client/apis/
metrics_api.rs

1/*
2 * langfuse
3 *
4 * ## Authentication  Authenticate with the API using [Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication), get API keys in the project settings:  - username: Langfuse Public Key - password: Langfuse Secret Key  ## Exports  - OpenAPI spec: https://cloud.langfuse.com/generated/api/openapi.yml - Postman collection: https://cloud.langfuse.com/generated/postman/collection.json
5 *
6 * The version of the OpenAPI document: 
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`metrics_daily`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum MetricsDailyError {
22    Status400(serde_json::Value),
23    Status401(serde_json::Value),
24    Status403(serde_json::Value),
25    Status404(serde_json::Value),
26    Status405(serde_json::Value),
27    UnknownValue(serde_json::Value),
28}
29
30
31/// Get daily metrics of the Langfuse project
32pub async fn metrics_daily(configuration: &configuration::Configuration, page: Option<i32>, limit: Option<i32>, trace_name: Option<&str>, user_id: Option<&str>, tags: Option<Vec<String>>, environment: Option<Vec<String>>, from_timestamp: Option<String>, to_timestamp: Option<String>) -> Result<models::DailyMetrics, Error<MetricsDailyError>> {
33    // add a prefix to parameters to efficiently prevent name collisions
34    let p_page = page;
35    let p_limit = limit;
36    let p_trace_name = trace_name;
37    let p_user_id = user_id;
38    let p_tags = tags;
39    let p_environment = environment;
40    let p_from_timestamp = from_timestamp;
41    let p_to_timestamp = to_timestamp;
42
43    let uri_str = format!("{}/api/public/metrics/daily", configuration.base_path);
44    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
45
46    if let Some(ref param_value) = p_page {
47        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
48    }
49    if let Some(ref param_value) = p_limit {
50        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
51    }
52    if let Some(ref param_value) = p_trace_name {
53        req_builder = req_builder.query(&[("traceName", &param_value.to_string())]);
54    }
55    if let Some(ref param_value) = p_user_id {
56        req_builder = req_builder.query(&[("userId", &param_value.to_string())]);
57    }
58    if let Some(ref param_value) = p_tags {
59        req_builder = match "multi" {
60            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("tags".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
61            _ => req_builder.query(&[("tags", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
62        };
63    }
64    if let Some(ref param_value) = p_environment {
65        req_builder = match "multi" {
66            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("environment".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
67            _ => req_builder.query(&[("environment", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
68        };
69    }
70    if let Some(ref param_value) = p_from_timestamp {
71        req_builder = req_builder.query(&[("fromTimestamp", &param_value.to_string())]);
72    }
73    if let Some(ref param_value) = p_to_timestamp {
74        req_builder = req_builder.query(&[("toTimestamp", &param_value.to_string())]);
75    }
76    if let Some(ref user_agent) = configuration.user_agent {
77        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
78    }
79    if let Some(ref auth_conf) = configuration.basic_auth {
80        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
81    };
82
83    let req = req_builder.build()?;
84    let resp = configuration.client.execute(req).await?;
85
86    let status = resp.status();
87    let content_type = resp
88        .headers()
89        .get("content-type")
90        .and_then(|v| v.to_str().ok())
91        .unwrap_or("application/octet-stream");
92    let content_type = super::ContentType::from(content_type);
93
94    if !status.is_client_error() && !status.is_server_error() {
95        let content = resp.text().await?;
96        match content_type {
97            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
98            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DailyMetrics`"))),
99            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::DailyMetrics`")))),
100        }
101    } else {
102        let content = resp.text().await?;
103        let entity: Option<MetricsDailyError> = serde_json::from_str(&content).ok();
104        Err(Error::ResponseError(ResponseContent { status, content, entity }))
105    }
106}
107