Skip to content

Commit

Permalink
Merge pull request #207 from cofacts/emptyRows
Browse files Browse the repository at this point in the history
handle the case where there's no user activities
  • Loading branch information
MrOrz committed Aug 15, 2020
2 parents 083920b + ed97993 commit 800758e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/scripts/__tests__/fetchStatsFromGA.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ describe('fetchStatsFromGA', () => {
});
afterEach(() => {
batchGetMock.mockReset();
requestBuilderSpy.mockClear();
});

it('should handle empty results', async () => {
const emptyRows = { data: { totals: [{ values: [0, 0] }] } };
const res = { data: { reports: [emptyRows, emptyRows] } };
batchGetMock.mockResolvedValueOnce(res);

const fetchReportsResults = await fetchStatsFromGA.fetchReports(
'WEB',
{},
{ isCron: true }
);

expect(fetchReportsResults).toStrictEqual({
results: { article: undefined, reply: undefined },
pageTokens: { article: -1, reply: -1 },
hasMore: false,
});
});

it('should send approate batchGet requests and return correct curated results', async () => {
Expand Down
27 changes: 18 additions & 9 deletions src/scripts/fetchStatsFromGA.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,26 @@ const fetchReports = async function(sourceType, pageTokens = {}, params) {
const reports = res.data.reports;
let results = {};
let hasMore = false;

requestDocTypes.forEach((docType, i) => {
let report = reports[i];
console.log(
`fetched ${report.data.rows.length} starting at ${pageTokens[docType] ||
0} ` +
`out of total ${
report.data.rowCount
} rows for ${sourceType} ${docType}` +
` with next pageToken ${report.nextPageToken}`
);
results[docType] = report.data.rows;
const rows = report.data.rows;
if (rows) {
console.log(
`fetched ${rows.length} starting at ${pageTokens[docType] || 0} ` +
`out of total ${
report.data.rowCount
} rows for ${sourceType} ${docType}` +
` ${
report.nextPageToken
? `with next pageToken ${report.nextPageToken}`
: ''
}`
);
} else {
console.log(`no entries for ${sourceType} ${docType}`);
}
results[docType] = rows;
nextPageTokens[docType] = report.nextPageToken || -1;
hasMore = hasMore || report.nextPageToken != null;
});
Expand Down

0 comments on commit 800758e

Please sign in to comment.