Skip to content

Commit

Permalink
modify command line options for fetchStatsFromGA script.
Browse files Browse the repository at this point in the history
- remove date validation to allow relative date pattern
- add useContentGroup option instead of guessing from start/end dates
  • Loading branch information
ztsai committed Aug 15, 2020
1 parent 800758e commit 0fbf65b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 114 deletions.
7 changes: 1 addition & 6 deletions README.md
Expand Up @@ -175,12 +175,7 @@ $ docker-compose exec api node_modules/.bin/babel-node src/scripts/cleanupUrls.j
$ node_modules/.bin/babel-node src/scripts/fetchStatsFromGA.js
```

- To fetch stats for a certain date range, run the above command with
```--startDate=YYYY-MM-DD --endDate=YYYY-MM-DD```

- If the script is ran for the first time, run the above command with
```--loadScript```

- For more options, run the above script with `--help` or see the file level comments.


## Troubleshooting
Expand Down
52 changes: 18 additions & 34 deletions src/scripts/__tests__/__snapshots__/fetchStatsFromGA.js.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_article_isCron=false 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_article_useContentGroup=false 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -40,7 +40,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_article_isCron=true 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_article_useContentGroup=true 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -80,7 +80,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_reply_isCron=false 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_reply_useContentGroup=false 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -120,7 +120,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_reply_isCron=true 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: LINE_reply_useContentGroup=true 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -160,7 +160,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_article_isCron=false 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_article_useContentGroup=false 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -200,7 +200,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_article_isCron=true 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_article_useContentGroup=true 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -240,7 +240,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_reply_isCron=false 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_reply_useContentGroup=false 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -280,7 +280,7 @@ Object {
}
`;

exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_reply_isCron=true 1`] = `
exports[`fetchStatsFromGA helper functions requestBodyBuilder should return right request body for different source and doc types: WEB_reply_useContentGroup=true 1`] = `
Object {
"dateRanges": Array [
Object {
Expand Down Expand Up @@ -877,16 +877,12 @@ Array [
Array [
"WEB",
undefined,
Object {
"isCron": true,
},
undefined,
],
Array [
"LINE",
undefined,
Object {
"isCron": true,
},
undefined,
],
]
`;
Expand All @@ -898,17 +894,17 @@ Array [
undefined,
Object {
"endDate": "today",
"isCron": false,
"startDate": "2020-07-10",
"useContentGroup": false,
},
],
Array [
"LINE",
undefined,
Object {
"endDate": "today",
"isCron": false,
"startDate": "2020-07-10",
"useContentGroup": false,
},
],
]
Expand Down Expand Up @@ -960,56 +956,44 @@ Array [
Array [
"WEB",
undefined,
Object {
"isCron": true,
},
undefined,
],
Array [
"WEB",
Object {
"article": 100,
"reply": 100,
},
Object {
"isCron": true,
},
undefined,
],
Array [
"WEB",
Object {
"article": 200,
"reply": -1,
},
Object {
"isCron": true,
},
undefined,
],
Array [
"LINE",
undefined,
Object {
"isCron": true,
},
undefined,
],
Array [
"LINE",
Object {
"article": 100,
"reply": 100,
},
Object {
"isCron": true,
},
undefined,
],
Array [
"LINE",
Object {
"article": -1,
"reply": 200,
},
Object {
"isCron": true,
},
undefined,
],
]
`;
Expand Down
87 changes: 41 additions & 46 deletions src/scripts/__tests__/fetchStatsFromGA.js
Expand Up @@ -62,28 +62,46 @@ describe('fetchStatsFromGA', () => {
});

it('without any arugments', async () => {
yargs.argvMock.mockReturnValue({});
yargs.argvMock.mockReturnValue({
useContentGroup: true,
loadScript: false,
});
await fetchStatsFromGA.main();
expect(updateStatsMock.mock.calls).toMatchObject([[{ isCron: true }]]);
expect(updateStatsMock.mock.calls).toMatchObject([
[{ useContentGroup: true }],
]);
expect(storeScriptInDBMock).not.toHaveBeenCalled();
});

it('with date arugments', async () => {
yargs.argvMock.mockReturnValue({
startDate: '2020-01-01',
endDate: '2020-02-01',
useContentGroup: true,
loadScript: false,
});
await fetchStatsFromGA.main();
expect(updateStatsMock.mock.calls).toMatchObject([
[{ isCron: false, startDate: '2020-01-01', endDate: '2020-02-01' }],
[
{
useContentGroup: true,
startDate: '2020-01-01',
endDate: '2020-02-01',
},
],
]);
expect(storeScriptInDBMock).not.toHaveBeenCalled();
});

it('with loadScript arugments', async () => {
yargs.argvMock.mockReturnValue({ loadScript: true });
yargs.argvMock.mockReturnValue({
loadScript: true,
useContentGroup: true,
});
await fetchStatsFromGA.main();
expect(updateStatsMock.mock.calls).toMatchObject([[{ isCron: true }]]);
expect(updateStatsMock.mock.calls).toMatchObject([
[{ useContentGroup: true }],
]);
expect(storeScriptInDBMock).toHaveBeenCalled();
});

Expand All @@ -92,10 +110,17 @@ describe('fetchStatsFromGA', () => {
loadScript: true,
startDate: '2020-01-01',
endDate: '2020-02-01',
useContentGroup: true,
});
await fetchStatsFromGA.main();
expect(updateStatsMock.mock.calls).toMatchObject([
[{ isCron: false, startDate: '2020-01-01', endDate: '2020-02-01' }],
[
{
useContentGroup: true,
startDate: '2020-01-01',
endDate: '2020-02-01',
},
],
]);
expect(storeScriptInDBMock).toHaveBeenCalled();
});
Expand All @@ -122,11 +147,11 @@ describe('fetchStatsFromGA', () => {

allSourceTypes.forEach(sourceType =>
allDocTypes.forEach(docType =>
[true, false].forEach(isCron => {
[true, false].forEach(useContentGroup => {
const params = {
isCron,
startDate: isCron ? undefined : '2020-07-10',
endDate: isCron ? undefined : 'today',
useContentGroup,
startDate: useContentGroup ? undefined : '2020-07-10',
endDate: useContentGroup ? undefined : 'today',
};
expect(
fetchStatsFromGA.requestBodyBuilder(
Expand All @@ -135,44 +160,14 @@ describe('fetchStatsFromGA', () => {
'',
params
)
).toMatchSnapshot(`${sourceType}_${docType}_isCron=${isCron}`);
).toMatchSnapshot(
`${sourceType}_${docType}_useContentGroup=${useContentGroup}`
);
})
)
);
});

it('processCommandLineArgs should return proper params', () => {
const processArgs = fetchStatsFromGA.processCommandLineArgs;
expect(processArgs({})).toStrictEqual({ isCron: true });
expect(
processArgs({ startDate: '2020-07-01', endDate: '2020-07-15' })
).toStrictEqual({
isCron: false,
startDate: '2020-07-01',
endDate: '2020-07-15',
});
expect(
processArgs({ startDate: '2020-07-01', endDate: '2020-07-01' })
).toStrictEqual({
isCron: false,
startDate: '2020-07-01',
endDate: '2020-07-01',
});
});

it('processCommandLineArgs should raise errors when given invalid arugments', () => {
[
{ startDate: '2020-01-01' },
{ startDate: '2020-01-01', endDate: '2019-01-01' },
{ startDate: '2019-01-01', endDate: '2020-01-01' },
{ startDate: '3000-01-01', endDate: '3000-01-01' },
].forEach(dateRange => {
expect(() =>
fetchStatsFromGA.processCommandLineArgs(dateRange)
).toThrow();
});
});

it('storeScriptInDB should store upsert script in db', async () => {
await fetchStatsFromGA.storeScriptInDB();
expect(
Expand Down Expand Up @@ -215,7 +210,7 @@ describe('fetchStatsFromGA', () => {

fetchReportsMock.mockClear();
await fetchStatsFromGA.updateStats({
isCron: false,
useContentGroup: false,
startDate: '2020-07-10',
endDate: 'today',
});
Expand Down Expand Up @@ -291,7 +286,7 @@ describe('fetchStatsFromGA', () => {
const fetchReportsResults = await fetchStatsFromGA.fetchReports(
'WEB',
{},
{ isCron: true }
{ useContentGroup: true }
);

expect(fetchReportsResults).toStrictEqual({
Expand All @@ -303,7 +298,7 @@ describe('fetchStatsFromGA', () => {

it('should send approate batchGet requests and return correct curated results', async () => {
const sourceType = 'WEB',
params = { isCron: true };
params = { useContentGroup: true };
let requestBuilderCalledTimes = 1;

for (const pageTokens of fixtures.fetchReports.allPossiblePageTokens) {
Expand Down

0 comments on commit 0fbf65b

Please sign in to comment.