Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print total gads api consumption to trace #14

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mcc-gaql"
description = "Execute GAQL across MCC child accounts."
version = "0.8.0"
version = "0.8.1"
authors = ["Michael S. Huang <[email protected]>"]
edition = "2021"

Expand Down
16 changes: 11 additions & 5 deletions src/googleads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub async fn gaql_query_with_client(
mut client: GoogleAdsServiceClient<InterceptedService<Channel, GoogleAdsAPIAccess>>,
customer_id: String,
query: String,
) -> Result<DataFrame> {
) -> Result<(DataFrame, i64)> {
let result: Result<Response<Streaming<SearchGoogleAdsStreamResponse>>, Status> = client
.search_stream(SearchGoogleAdsStreamRequest {
customer_id: customer_id.clone(),
Expand All @@ -194,16 +194,20 @@ pub async fn gaql_query_with_client(
})
.await;

let df = match result {
let (df, total_api_consumption) = match result {
Ok(response) => {
let mut stream = response.into_inner();

let mut columns: Vec<Vec<String>> = Vec::new();
let mut headers: Option<Vec<String>> = None;
let mut api_consumption: i64 = 0;

while let Some(item) = stream.next().await {
match item {
Ok(stream_response) => {
// aggregate api consumption
api_consumption += stream_response.query_resource_consumption;

let field_mask = stream_response.field_mask.unwrap();
if headers.is_none() {
headers = Some(field_mask.paths.clone());
Expand Down Expand Up @@ -269,7 +273,9 @@ pub async fn gaql_query_with_client(
}
}

DataFrame::new(series_vec).unwrap()
let df = DataFrame::new(series_vec).unwrap();

(df, api_consumption)
}
Err(status) => {
bail!(
Expand All @@ -280,15 +286,15 @@ pub async fn gaql_query_with_client(
}
};

Ok(df)
Ok((df, total_api_consumption))
}

/// Run query via GoogleAdsServiceClient to get performance data
pub async fn gaql_query(
api_context: GoogleAdsAPIAccess,
customer_id: String,
query: String,
) -> Result<DataFrame> {
) -> Result<(DataFrame, i64)> {
let client: GoogleAdsServiceClient<InterceptedService<Channel, GoogleAdsAPIAccess>> =
GoogleAdsServiceClient::with_interceptor(api_context.channel.clone(), api_context);

Expand Down
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let dataframe: Option<DataFrame> =
match googleads::gaql_query(api_context, customer_id, query).await {
Ok(df) => {
Ok((df, _api_consumption)) => {
Some(df)
}
Err(e) => {
Expand Down Expand Up @@ -247,15 +247,17 @@ async fn gaql_query_async(
let start = Instant::now();

let mut total_rows: usize = 0;
let mut total_api_consumption: i64 = 0;

// collect asynchronous query results
while let Some(result) = gaql_handles.next().await {
match result {
Ok(result) => {
match result {
Ok(df) => {
Ok((df, api_consumption)) => {
if !df.is_empty() {
total_rows += df.height();
total_api_consumption += api_consumption;

// log::debug!("Future returned non-empty GAQL results");
if !&groupby.is_empty() {
Expand Down Expand Up @@ -283,10 +285,12 @@ async fn gaql_query_async(
}

let duration = start.elapsed();
log::debug!(
"All queries returned {} rows in {} msec",
log::info!(
"GAQL returned {} rows in {} msec across {} accounts using {} API units",
total_rows.separate_with_commas(),
duration.as_millis().separate_with_commas()
duration.as_millis().separate_with_commas(),
customer_id_vector.len().separate_with_commas(),
total_api_consumption.separate_with_commas()
);

// collect 1st pass groupby results
Expand Down
Loading