Skip to content

Commit

Permalink
fix for summary row in multi page report calls
Browse files Browse the repository at this point in the history
  • Loading branch information
avermeil committed Feb 13, 2024
1 parent abe39ff commit ea18c81
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export class Customer extends ServiceFactory {
response: services.IGoogleAdsRow[];
nextPageToken: PageToken;
totalResultsCount?: number;
summaryRow?: services.IGoogleAdsRow;
}> {
const accessToken = await this.getAccessToken();

Expand All @@ -212,23 +213,19 @@ export class Customer extends ServiceFactory {
const searchResponse = rawResponse.data as any;

const results = searchResponse.results ?? [];
console.time("parsing");
const response = this.clientOptions.disable_parsing
// console.time("parsing");
const response: any[] = this.clientOptions.disable_parsing
? results
: results.map((row: any) => decamelizeKeys(row));
console.timeEnd("parsing");
// console.timeEnd("parsing");

const summaryRow = decamelizeKeys(searchResponse.summaryRow);
const nextPageToken = searchResponse.nextPageToken;
const totalResultsCount = searchResponse.totalResultsCount
? +searchResponse.totalResultsCount
: undefined;

if (summaryRow) {
response.unshift(summaryRow);
}

return { response, nextPageToken, totalResultsCount };
return { response, nextPageToken, totalResultsCount, summaryRow };
} catch (e: any) {
console.dir({ e: e.response.data }, { depth: null });
if (e.response.data.error.details[0]) {
Expand All @@ -251,16 +248,25 @@ export class Customer extends ServiceFactory {
let nextPageToken: PageToken = undefined;
const initialSearch = await this.search(gaqlQuery, requestOptions);
const totalResultsCount = initialSearch.totalResultsCount;
let summaryRow = initialSearch.summaryRow;

response.push(...initialSearch.response);
nextPageToken = initialSearch.nextPageToken;

while (nextPageToken) {
const nextSearch = await this.search(gaqlQuery, {
...requestOptions,
page_token: nextPageToken,
});
response.push(...initialSearch.response);
nextPageToken = nextSearch.nextPageToken;
if (nextSearch.summaryRow) {
summaryRow = nextSearch.summaryRow;
}
}

if (summaryRow) {
response.unshift(summaryRow);
}

return { response, totalResultsCount };
Expand Down Expand Up @@ -366,7 +372,7 @@ export class Customer extends ServiceFactory {

const accessToken = await this.getAccessToken();

console.time("request");
// console.time("request");

let streamFinished = false;
const accumulator: T[] = [];
Expand All @@ -393,20 +399,19 @@ export class Customer extends ServiceFactory {
const pipeline = chain([stream, parser(), streamArray()]);

stream.once("data", () => {
console.timeEnd("request");
// console.timeEnd("request");
});

pipeline.on("data", (data) => {
console.log("got chunk:", data);
console.time("chunk parsing");
// console.time("chunk parsing");

const results = data.value.results ?? [data.value.summaryRow];

const parsed = this.clientOptions.disable_parsing
? results
: results.map((row: any) => decamelizeKeys(row));

console.timeEnd("chunk parsing");
// console.timeEnd("chunk parsing");

accumulator.push(...(parsed as T[]));

Expand Down

0 comments on commit ea18c81

Please sign in to comment.