Skip to content

Commit

Permalink
core(lhr): s/fetchedAt/fetchTime (#5112)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny authored May 4, 2018
1 parent b49e6ce commit f0558f7
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/understanding-results.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The top-level Lighthouse Result object (LHR) is what the lighthouse node module
| Name | Description |
| - | - |
| lighthouseVersion | The version of Lighthouse with which this result were generated. |
| fetchedAt | The ISO-8601 timestamp of when the result was generated. |
| fetchTime | The ISO-8601 timestamp of when the result was generated. |
| userAgent | The user agent string of the version of Chrome that was used by Lighthouse. |
| initialUrl | The URL that was supplied to Lighthouse and initially navigated to. |
| url | The URL that Lighthouse ended up auditing after redirects were followed. |
Expand All @@ -26,7 +26,7 @@ The top-level Lighthouse Result object (LHR) is what the lighthouse node module
```json
{
"lighthouseVersion": "2.4.0",
"fetchedAt": "2017-10-05T20:50:54.185Z",
"fetchTime": "2017-10-05T20:50:54.185Z",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3233.0 Safari/537.36",
"initialUrl": "http://example.com",
"url": "https://www.example.com/",
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-cli/test/cli/run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('CLI run', function() {
assert.equal(results.audits.viewport.rawValue, false);

// passed results match saved results
assert.strictEqual(results.fetchedAt, lhr.fetchedAt);
assert.strictEqual(results.fetchTime, lhr.fetchTime);
assert.strictEqual(results.url, lhr.url);
assert.strictEqual(results.audits.viewport.rawValue, lhr.audits.viewport.rawValue);
assert.strictEqual(
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class GatherRunner {
/** @type {GathererResults} */
const gathererResults = {
LighthouseRunWarnings: [],
fetchedAt: [(new Date()).toJSON()],
fetchTime: [(new Date()).toJSON()],
};

return driver.connect()
Expand Down
24 changes: 17 additions & 7 deletions lighthouse-core/lib/file-namer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
* 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 http://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.
*/
// @ts-nocheck
'use strict';

/* global URL */

/**
* @fileoverview
* @suppress {reportUnknownTypes}
*/

/**
* Generate a filenamePrefix of hostname_YYYY-MM-DD_HH-MM-SS
* Date/time uses the local timezone, however Node has unreliable ICU
* support, so we must construct a YYYY-MM-DD date format manually. :/
* @param {{url: string, fetchedAt: string}} lhr
* @param {{url: string, fetchTime: string}} lhr
* @return {string}
*/
function getFilenamePrefix(lhr) {
const hostname = new (URLConstructor || URL)(lhr.url).hostname;
const date = (lhr.fetchedAt && new Date(lhr.fetchedAt)) || new Date();
const hostname = new (getUrlConstructor())(lhr.url).hostname;
const date = (lhr.fetchTime && new Date(lhr.fetchTime)) || new Date();

const timeStr = date.toLocaleTimeString('en-US', {hour12: false});
const dateParts = date.toLocaleDateString('en-US', {
year: 'numeric', month: '2-digit', day: '2-digit',
}).split('/');
// @ts-ignore - parts exists
dateParts.unshift(dateParts.pop());
const dateStr = dateParts.join('-');

Expand All @@ -31,9 +36,14 @@ function getFilenamePrefix(lhr) {
return filenamePrefix.replace(/[/?<>\\:*|":]/g, '-');
}

let URLConstructor;
if (typeof module !== 'undefined' && module.exports) {
URLConstructor = require('./url-shim');
function getUrlConstructor() {
if (typeof module !== 'undefined' && module.exports) {
return require('./url-shim');
} else {
return URL;
}
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = {getFilenamePrefix};
}
7 changes: 3 additions & 4 deletions lighthouse-core/report/html/renderer/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ReportRenderer {
_renderReportHeader(report) {
const header = this._dom.cloneTemplate('#tmpl-lh-heading', this._templateContext);
this._dom.find('.lh-config__timestamp', header).textContent =
Util.formatDateTime(report.fetchedAt);
Util.formatDateTime(report.fetchTime);
const url = this._dom.find('.lh-metadata__url', header);
url.href = report.url;
url.textContent = report.url;
Expand All @@ -83,7 +83,7 @@ class ReportRenderer {
const footer = this._dom.cloneTemplate('#tmpl-lh-footer', this._templateContext);
this._dom.find('.lh-footer__version', footer).textContent = report.lighthouseVersion;
this._dom.find('.lh-footer__timestamp', footer).textContent =
Util.formatDateTime(report.fetchedAt);
Util.formatDateTime(report.fetchTime);
return footer;
}

Expand Down Expand Up @@ -257,8 +257,7 @@ ReportRenderer.GroupJSON; // eslint-disable-line no-unused-expressions
* @typedef {{
* lighthouseVersion: string,
* userAgent: string,
* fetchedAt: string,
* generatedTime: string,
* fetchTime: string,
* timing: {total: number},
* initialUrl: string,
* url: string,
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/report/html/renderer/report-ui-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class ReportUIFeatures {
_saveFile(blob) {
const filename = getFilenamePrefix({
url: this.json.url,
fetchedAt: this.json.fetchedAt,
fetchTime: this.json.fetchTime,
});

const ext = blob.type.match('json') ? '.json' : '.html';
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/report/html/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Util {
if (typeof displayValue === 'string') return displayValue;

const replacementRegex = /%([0-9]*(\.[0-9]+)?d|s)/;
const template = /** @type {string} */ displayValue.shift();
const template = /** @type {string} */ (displayValue.shift());
if (typeof template !== 'string') {
// First value should always be the format string, but we don't want to fail to build
// a report, return a placeholder.
Expand Down
3 changes: 1 addition & 2 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ class Runner {
const lhr = {
userAgent: artifacts.UserAgent,
lighthouseVersion,
fetchedAt: artifacts.fetchedAt,
generatedTime: 'Please use .fetchedAt instead',
fetchTime: artifacts.fetchTime,
initialUrl: opts.initialUrl,
url: opts.url,
runWarnings: lighthouseRunWarnings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Also a warning"
],
"UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3358.0 Safari/537.36",
"fetchedAt": "2018-03-13T00:55:45.840Z",
"fetchTime": "2018-03-13T00:55:45.840Z",
"URL": {
"initialUrl": "https://www.reddit.com/r/nba",
"finalUrl": "https://www.reddit.com/r/nba"
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Module Tests', function() {
assert.ok(/<html/.test(results.report), 'did not create html report');
assert.ok(results.artifacts.ViewportDimensions, 'did not set artifacts');
assert.ok(results.lhr.lighthouseVersion);
assert.ok(results.lhr.fetchedAt);
assert.ok(results.lhr.fetchTime);
assert.equal(results.lhr.url, exampleUrl);
assert.equal(results.lhr.initialUrl, exampleUrl);
assert.ok(Array.isArray(results.lhr.reportCategories));
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/lib/file-namer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('file-namer helper', () => {
it('generates filename prefixes', () => {
const results = {
url: 'https://testexample.com',
fetchedAt: '2017-01-06T02:34:56.217Z',
fetchTime: '2017-01-06T02:34:56.217Z',
};
const str = getFilenamePrefix(results);
// we want the filename to match user timezone, however these tests will run on multiple TZs
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/results/artifacts/artifacts.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"LighthouseRunWarnings": [],
"UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3358.0 Safari/537.36",
"fetchedAt": "2018-03-13T00:55:45.840Z",
"fetchTime": "2018-03-13T00:55:45.840Z",
"URL": {
"initialUrl": "http://localhost:10200/dobetterweb/dbw_tester.html",
"finalUrl": "http://localhost:10200/dobetterweb/dbw_tester.html"
Expand Down
3 changes: 1 addition & 2 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3358.0 Safari/537.36",
"lighthouseVersion": "3.0.0-alpha",
"fetchedAt": "2018-03-13T00:55:45.840Z",
"generatedTime": "Please use .fetchedAt instead",
"fetchTime": "2018-03-13T00:55:45.840Z",
"initialUrl": "http://localhost/dobetterweb/dbw_tester.html",
"url": "http://localhost/dobetterweb/dbw_tester.html",
"runWarnings": [],
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/test/runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ describe('Runner', () => {

return Runner.run(null, {url, config, driverMock}).then(results => {
assert.ok(results.lhr.lighthouseVersion);
assert.ok(results.lhr.fetchedAt);
assert.ok(results.lhr.fetchTime);
assert.equal(results.lhr.initialUrl, url);
assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called');
assert.equal(results.lhr.audits['content-width'].name, 'content-width');
Expand Down Expand Up @@ -441,7 +441,7 @@ describe('Runner', () => {

return Runner.run(null, {url, config, driverMock}).then(results => {
assert.ok(results.lhr.lighthouseVersion);
assert.ok(results.lhr.fetchedAt);
assert.ok(results.lhr.fetchTime);
assert.equal(results.lhr.initialUrl, url);
assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called');
assert.equal(results.lhr.audits['content-width'].name, 'content-width');
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-viewer/app/src/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GithubApi {
.then(accessToken => {
const filename = getFilenamePrefix({
url: jsonFile.url,
fetchedAt: jsonFile.fetchedAt,
fetchTime: jsonFile.fetchTime,
});
const body = {
description: 'Lighthouse json report',
Expand Down
2 changes: 1 addition & 1 deletion typings/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare global {
module LH {
export interface Artifacts extends ComputedArtifacts {
// Created by by gather-runner
fetchedAt: string;
fetchTime: string;
LighthouseRunWarnings: string[];
UserAgent: string;
traces: {[passName: string]: Trace};
Expand Down
2 changes: 1 addition & 1 deletion typings/lhr-lite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare global {
/** The post-redirects URL that Lighthouse loaded. */
finalUrl: string;
/** The ISO-8601 timestamp of when the results were generated. */
fetchedAt: string;
fetchTime: string;
/** The version of Lighthouse with which these results were generated. */
lighthouseVersion: string;
/** An object containing the results of the audits. */
Expand Down
4 changes: 1 addition & 3 deletions typings/lhr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare global {
/** The post-redirects URL that Lighthouse loaded. */
url: string;
/** The ISO-8601 timestamp of when the results were generated. */
fetchedAt: string;
fetchTime: string;
/** The version of Lighthouse with which these results were generated. */
lighthouseVersion: string;
/** An object containing the results of the audits, keyed by the audits' `id` identifier. */
Expand All @@ -33,8 +33,6 @@ declare global {
runWarnings: string[];
/** The User-Agent string of the browser used run Lighthouse for these results. */
userAgent: string;
/** Deprecated. Use fetchedAt instead. */
generatedTime?: string;
}

// Result namespace
Expand Down

0 comments on commit f0558f7

Please sign in to comment.