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

Implement output folder naming conventions for saved reports #759

Merged
merged 8 commits into from
Jul 22, 2024
Merged
2 changes: 1 addition & 1 deletion packages/cli-dashboard/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href='data:image/svg+xml;base64,Cjxzdmcgd2lkdGg9IjEyOCIgaGVpZ2h0PSIxMjgiIHZpZXdCb3g9IjAgMCAxMjggMTI4IiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8ZyBjbGlwLXBhdGg9InVybCgjY2xpcDBfMzlfNTIwKSI+CjxwYXRoIGQ9Ik0xMDMuODg4IDIzLjQ0NTFDMTExLjIxMyAzMC43NTg4IDExMy44MDcgNDEuODE3IDExMS42MjUgNTQuMTUwM0MxMDkuNDQ0IDY2LjQ3MjkgMTAyLjUxMiA3OS43OTQ0IDkxLjIyMTYgOTEuMDY3NkM3OS45MzA4IDEwMi4zNDEgNjYuNTg4NSAxMDkuMjYyIDU0LjI0NjcgMTExLjQzOUM0MS44OTQyIDExMy42MTggMzAuODE4NyAxMTEuMDI4IDIzLjQ5MzYgMTAzLjcxNEMxNi4xNjg0IDk2LjQwMDcgMTMuNTc0NyA4NS4zNDI1IDE1Ljc1NzIgNzMuMDA5MkMxNy45Mzc4IDYwLjY4NjYgMjQuODY5MyA0Ny4zNjUyIDM2LjE2MDEgMzYuMDkxOUM0Ny40NTEgMjQuODE4NyA2MC43OTMyIDE3Ljg5OCA3My4xMzUgMTUuNzIwOEM4NS40ODc1IDEzLjU0MTcgOTYuNTYzIDE2LjEzMTQgMTAzLjg4OCAyMy40NDUxWiIgc3Ryb2tlPSIjOUU5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0MUYyMCIgc3Ryb2tlLXdpZHRoPSIyLjI4NyIgc3Ryb2tlLW1pdGVybGltaXQ9IjEwIi8+CjxwYXRoIGQ9Ik0xMTMuNDY0IDEwNEg4NC41NzYxTDgwIDExMi40TDg0LjU3NjEgMTIwLjhIMTEzLjQ2NEwxMTguMDQgMTEyLjRMMTEzLjQ2NCAxMDRaIiBmaWxsPSIjOUNEMUZCIi8+CjwvZz4KPGRlZnM+CjxjbGlwUGF0aCBpZD0iY2xpcDBfMzlfNTIwIj4KPHJlY3Qgd2lkdGg9IjEyOCIgaGVpZ2h0PSIxMjgiIGZpbGw9IndoaXRlIi8+CjwvY2xpcFBhdGg+CjwvZGVmcz4KPC9zdmc+Cg=='/>
<title>CLI Dashboard</title>
<title>PSAT Report</title>
</head>
<body>
<div id="root"></div>
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
filePathValidator,
urlValidator,
numericValidator,
getOutputFilePath,
} from './utils';
import { redLogger } from './utils/coloredLoggers';

Expand Down Expand Up @@ -219,11 +220,12 @@
})}</script>` +
htmlText.substring(htmlText.indexOf('</head>'));

const outFileFullDir = path.resolve(outDir + '/index.html');
const outputFilePath = getOutputFilePath(outDir);
const outFileFullDir = path.resolve(outputFilePath);
const htmlBlob = new Blob([html]);
const buffer = Buffer.from(await htmlBlob.arrayBuffer());

writeFile(outDir + '/index.html', buffer, () =>
writeFile(outputFilePath, buffer, () =>
console.log(`\nReport: ${URL.pathToFileURL(outFileFullDir)}`)
);
};
Expand Down Expand Up @@ -335,7 +337,7 @@
'Done analyzing cookies!'
);

let technologyAnalysisData: any = null;

Check warning on line 340 in packages/cli/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type

if (shouldDoTechnologyAnalysis) {
spinnies.add('technology-spinner', {
Expand Down
53 changes: 53 additions & 0 deletions packages/cli/src/utils/getCurrentDateAndTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Formats the current date and time into a specified string format.
* The supported formats are:
* - 'YYYY-MM-DD_HH-MM-SS': 24-hour format with seconds (e.g., 2024-07-19_14-30-45)
* - 'DD MMMM, YYYY, hh:mm:ssam/pm': 12-hour format with seconds and AM/PM (e.g., 19 July, 2024, 02:30:45pm)
* @param format {string} The format in which the date is required.
* @returns {string} The formatted date string.
*/
function getCurrentDateAndTime(
format: 'YYYY-MM-DD_HH-MM-SS' | 'DD MMMM, YYYY, hh:mm:ssam/pm'
): string {
const date = new Date();

const day = String(date.getDate()).padStart(2, '0');
const monthLong = date.toLocaleString('en-US', { month: 'long' });
const monthShort = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
const hours24 = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');

let hours12 = date.getHours();
const ampm = hours12 >= 12 ? 'pm' : 'am';

hours12 = hours12 % 12;
hours12 = hours12 ? hours12 : 12; // the hour '0' should be '12'
const hoursPadded12 = String(hours12).padStart(2, '0');

if (format === 'YYYY-MM-DD_HH-MM-SS') {
return `${year}-${monthShort}-${day}_${hours24}-${minutes}-${seconds}`;
} else if (format === 'DD MMMM, YYYY, hh:mm:ssam/pm') {
return `${day} ${monthLong}, ${year}, ${hoursPadded12}:${minutes}:${seconds}${ampm}`;
} else {
throw new Error('Invalid format');
}
}

export default getCurrentDateAndTime;
33 changes: 33 additions & 0 deletions packages/cli/src/utils/getOutputFilePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Internal dependencies.
*/
import { getCurrentDateAndTime } from '.';

/**
* Get output file path.
* @param {string} outDir Out directory
* @returns {string} file path
*/
const getOutputFilePath = (outDir: string): string => {
const currentDateAndTime = getCurrentDateAndTime('YYYY-MM-DD_HH-MM-SS');
const fileName = outDir + `/report_` + currentDateAndTime + '.html';

return fileName;
};

export default getOutputFilePath;
2 changes: 2 additions & 0 deletions packages/cli/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export { default as saveCSVReports } from './saveCSVReports';
export { default as getUrlsFromSitemap } from './getUrlListFromArgs';
export { default as askUserInput } from './askUserInput';
export { default as generatePrefix } from './generatePrefix';
export { default as getCurrentDateAndTime } from './getCurrentDateAndTime';
export { default as getOutputFilePath } from './getOutputFilePath';
export * from './validators';
43 changes: 43 additions & 0 deletions packages/cli/src/utils/tests/getCurrentDateAndTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Internal dependencies.
*/
import { getCurrentDateAndTime } from '..';

describe('getCurrentDateAndTime', () => {
beforeAll(() => {
const mockDate = new Date(2024, 6, 16, 18, 30, 15); // 2024-07-16 18:30
global.Date = jest.fn(() => mockDate) as unknown as typeof Date;
});

it('should return date and time in YYYY-MM-DD_HH-MM format', () => {
const result = getCurrentDateAndTime('YYYY-MM-DD_HH-MM-SS');
expect(result).toBe('2024-07-16_18-30-15');
});

it('should return date and time in DD MMMM, YYYY, hh:mmam/pm format', () => {
const result = getCurrentDateAndTime('DD MMMM, YYYY, hh:mm:ssam/pm');
expect(result).toBe('16 July, 2024, 06:30:15pm');
});

it('should throw an error for an invalid format', () => {
// @ts-ignore - invalid-format added for testing.
expect(() => getCurrentDateAndTime('invalid-format')).toThrow(
'Invalid format'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { WarningBare } from '../../../../icons';

const sidebarData: SidebarItems = {
[SIDEBAR_ITEMS_KEYS.COOKIES]: {
title: () => I18n.getMessage('sitemapReport'),
title: () => 'Aggregated Report',
children: {},
dropdownOpen: true,
},
Expand Down
3 changes: 0 additions & 3 deletions packages/i18n/_locales/messages/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@
"session": {
"message": "Session"
},
"sitemapReport": {
"message": "Sitemap report"
},
"cookieIssues": {
"message": "Cookie Issues"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/i18n/_locales/messages/es/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@
"session": {
"message": "Sesión"
},
"sitemapReport": {
"message": "Informe \"Sitemaps\""
},
"cookieIssues": {
"message": "Problemas de cookies"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/i18n/_locales/messages/hi/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@
"session": {
"message": "सेशन"
},
"sitemapReport": {
"message": "साइटमैप के बारे में रिपोर्ट"
},
"cookieIssues": {
"message": "कुकी से जुड़ी समस्याएं"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/i18n/_locales/messages/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@
"session": {
"message": "セッション"
},
"sitemapReport": {
"message": "サイトマップ レポート"
},
"cookieIssues": {
"message": "Cookie に関する問題"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/i18n/_locales/messages/ko/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@
"session": {
"message": "세션"
},
"sitemapReport": {
"message": "사이트맵 보고서"
},
"cookieIssues": {
"message": "쿠키 문제"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/i18n/_locales/messages/pt_BR/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@
"session": {
"message": "Sessão"
},
"sitemapReport": {
"message": "Relatório de sitemaps"
},
"cookieIssues": {
"message": "Problemas com cookies"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/report/src/dummyData/PSAT_DATA.js
Original file line number Diff line number Diff line change
Expand Up @@ -1918,9 +1918,6 @@ export default {
session: {
message: 'Session',
},
sitemapReport: {
message: 'Sitemap report',
},
cookieIssues: {
message: 'Cookie Issues',
},
Expand Down
Loading