From c7dcc4aac34fb048e4ef258286392aaee804c0e6 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 21 Feb 2023 22:04:47 -0800 Subject: [PATCH] actually return a json result from influx --- web3_proxy/src/frontend/errors.rs | 17 +++++++++++++++-- web3_proxy/src/frontend/users.rs | 2 +- web3_proxy/src/stats/influxdb_queries.rs | 19 +++++++++++-------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/web3_proxy/src/frontend/errors.rs b/web3_proxy/src/frontend/errors.rs index 01c3cd13..cf791e88 100644 --- a/web3_proxy/src/frontend/errors.rs +++ b/web3_proxy/src/frontend/errors.rs @@ -11,7 +11,7 @@ use axum::{ use derive_more::From; use http::header::InvalidHeaderValue; use ipnet::AddrParseError; -use log::{debug, trace, warn}; +use log::{debug, error, trace, warn}; use migration::sea_orm::DbErr; use redis_rate_limiter::redis::RedisError; use reqwest::header::ToStrError; @@ -30,6 +30,7 @@ pub enum FrontendErrorResponse { Database(DbErr), Headers(headers::Error), HeaderToString(ToStrError), + InfluxDb2RequestError(influxdb2::RequestError), InvalidHeaderValue(InvalidHeaderValue), IpAddrParse(AddrParseError), JoinError(JoinError), @@ -85,7 +86,7 @@ impl FrontendErrorResponse { ) } Self::Database(err) => { - warn!("database err={:?}", err); + error!("database err={:?}", err); ( StatusCode::INTERNAL_SERVER_ERROR, JsonRpcForwardedResponse::from_str( @@ -106,6 +107,18 @@ impl FrontendErrorResponse { ), ) } + Self::InfluxDb2RequestError(err) => { + // TODO: attach a request id to the message and to this error so that if people report problems, we can dig in sentry to find out more + error!("influxdb2 err={:?}", err); + ( + StatusCode::INTERNAL_SERVER_ERROR, + JsonRpcForwardedResponse::from_str( + "influxdb2 error!", + Some(StatusCode::INTERNAL_SERVER_ERROR.as_u16().into()), + None, + ), + ) + } Self::IpAddrParse(err) => { warn!("IpAddrParse err={:?}", err); ( diff --git a/web3_proxy/src/frontend/users.rs b/web3_proxy/src/frontend/users.rs index fe765c6a..29210ae4 100644 --- a/web3_proxy/src/frontend/users.rs +++ b/web3_proxy/src/frontend/users.rs @@ -5,7 +5,7 @@ use crate::app::Web3ProxyApp; use crate::http_params::{ get_chain_id_from_params, get_page_from_params, get_query_start_from_params, }; -use crate::stats::db_queries::query_user_stats; +use crate::stats::influxdb_queries::query_user_stats; use crate::stats::StatType; use crate::user_token::UserBearerToken; use crate::{PostLogin, PostLoginQuery}; diff --git a/web3_proxy/src/stats/influxdb_queries.rs b/web3_proxy/src/stats/influxdb_queries.rs index 68425ab8..d38f5865 100644 --- a/web3_proxy/src/stats/influxdb_queries.rs +++ b/web3_proxy/src/stats/influxdb_queries.rs @@ -3,24 +3,26 @@ use crate::{ app::Web3ProxyApp, frontend::errors::FrontendErrorResponse, http_params::{ - get_chain_id_from_params, get_page_from_params, get_query_start_from_params, - get_query_stop_from_params, get_query_window_seconds_from_params, get_user_id_from_params, + get_chain_id_from_params, get_query_start_from_params, get_query_stop_from_params, + get_query_window_seconds_from_params, get_user_id_from_params, }, }; use anyhow::Context; use axum::{ headers::{authorization::Bearer, Authorization}, - response::Response, - TypedHeader, + response::{IntoResponse, Response}, + Json, TypedHeader, }; use chrono::{DateTime, FixedOffset}; use fstrings::{f, format_args_f}; use hashbrown::HashMap; use influxdb2::models::Query; use influxdb2::FromDataPoint; +use serde::Serialize; use serde_json::json; -#[derive(Debug, Default, FromDataPoint)] +// TODO: include chain_id, method, and some other things in this struct +#[derive(Debug, Default, FromDataPoint, Serialize)] pub struct AggregatedRpcAccounting { field: String, value: f64, @@ -57,7 +59,6 @@ pub async fn query_user_stats<'a>( let query_start = get_query_start_from_params(params)?.timestamp(); let query_stop = get_query_stop_from_params(params)?.timestamp(); let chain_id = get_chain_id_from_params(app, params)?; - let page = get_page_from_params(params)?; let measurement = if user_id == 0 { "global_proxy" @@ -99,9 +100,11 @@ pub async fn query_user_stats<'a>( |> yield(name: "mean") "#); - let query = Query::new(qs.to_string()); + let query = Query::new(query.to_string()); + // TODO: do not unwrap. add this error to FrontErrorResponse + // TODO: StatType::Aggregated and StatType::Detailed might need different types let res: Vec = influxdb_client.query(Some(query)).await?; - todo!(); + Ok(Json(json!(res)).into_response()) }