From 775d7b2bd506f5c6d437734b44001cefde264f73 Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Fri, 24 Mar 2017 17:37:33 -0700 Subject: [PATCH] update for dom report --- lighthouse-cli/bin.ts | 2 +- lighthouse-cli/printer.ts | 8 ++--- lighthouse-core/config/default.json | 1 + lighthouse-core/report/report-generator.js | 35 ++----------------- lighthouse-core/report/v2/report-generator.js | 3 +- lighthouse-core/report/v2/report-renderer.js | 19 ++++++++++ lighthouse-core/runner.js | 7 ++-- .../test/report/v2/report-generator-test.js | 12 +++++++ 8 files changed, 46 insertions(+), 41 deletions(-) diff --git a/lighthouse-cli/bin.ts b/lighthouse-cli/bin.ts index 4011b0d04d77..e8dd9a38fd17 100755 --- a/lighthouse-cli/bin.ts +++ b/lighthouse-cli/bin.ts @@ -294,7 +294,7 @@ function saveResults(results: Results, }, Promise.resolve(results)); } else { let outputPath = flags.outputPath || `${resolvedPath}.report.${flags.output}`; - outputPath = outputPath.replace(/\.htmlv2$/, '.html'); + outputPath = outputPath.replace(/\.domhtml$/, '.html'); return Printer.write(results, flags.output, outputPath).then(results => { if (flags.output === Printer.OutputMode[Printer.OutputMode.html]) { if (flags.view) { diff --git a/lighthouse-cli/printer.ts b/lighthouse-cli/printer.ts index 86960753d973..7a329559f2aa 100644 --- a/lighthouse-cli/printer.ts +++ b/lighthouse-cli/printer.ts @@ -22,8 +22,8 @@ * 'json': JSON formatted results * 'html': An HTML report */ -enum OutputMode { json, html, htmlv2 }; -type Mode = 'json' | 'html' | 'htmlv2'; +enum OutputMode { json, html, domhtml }; +type Mode = 'json' | 'html' | 'domhtml'; import {Results} from './types/types'; @@ -56,7 +56,7 @@ function createOutput(results: Results, outputMode: OutputMode): string { return reportGenerator.generateHTML(results, 'cli'); } - if (outputMode === OutputMode.htmlv2) { + if (outputMode === OutputMode.domhtml) { return new ReportGeneratorV2().generateReportHtml(results); } @@ -123,7 +123,7 @@ function write(results: Results, mode: Mode, path: string): Promise { function GetValidOutputOptions():Array { return [OutputMode[OutputMode.json] as Mode, OutputMode[OutputMode.html] as Mode, - OutputMode[OutputMode.htmlv2] as Mode]; + OutputMode[OutputMode.domhtml] as Mode]; } export { diff --git a/lighthouse-core/config/default.json b/lighthouse-core/config/default.json index 16a517196a81..f996b7d179ce 100644 --- a/lighthouse-core/config/default.json +++ b/lighthouse-core/config/default.json @@ -454,6 +454,7 @@ "categories": { "pwa": { "name": "Progressive Web App", + "weight": 1, "description": "These audits validate the aspects of a Progressive Web App. They are a subset of the [PWA Checklist](https://developers.google.com/web/progressive-web-apps/checklist).", "audits": [ {"id": "service-worker", "weight": 1}, diff --git a/lighthouse-core/report/report-generator.js b/lighthouse-core/report/report-generator.js index fbb60f69b70e..2fb6776783de 100644 --- a/lighthouse-core/report/report-generator.js +++ b/lighthouse-core/report/report-generator.js @@ -148,45 +148,14 @@ class ReportGenerator { }); } - /** - * Returns aggregations directly from results or maps Config v2 categories to equivalent Config - * v1 aggregations. - * @param {{aggregations: !Array, reportCategories: !Array}} results - * @param {boolean=} forceV2 - * @return {!Array} - */ - _getAggregations(results, forceV2 = false) { - if (results.aggregations.length && !forceV2) { - return results.aggregations; - } - - return results.reportCategories.map(category => { - const name = category.name; - const description = category.description; - - return { - name, description, - categorizable: false, - scored: category.id === 'pwa', - total: category.score / 100, - score: [{ - name, description, - overall: category.score / 100, - subItems: category.audits.map(child => child.result), - }], - }; - }); - } - /** * Generates the Lighthouse report HTML. * @param {!Object} results Lighthouse results. * @param {!string} reportContext What app is requesting the report (eg. devtools, extension) * @param {?Object} reportsCatalog Basic info about all the reports to include in left nav bar - * @param {boolean=} forceV2 * @return {string} HTML of the report page. */ - generateHTML(results, reportContext = 'extension', reportsCatalog, forceV2) { + generateHTML(results, reportContext = 'extension', reportsCatalog) { this._registerPartials(results.audits); results.aggregations.forEach(aggregation => { @@ -207,7 +176,7 @@ class ReportGenerator { stylesheets: this.getReportCSS(), reportContext: reportContext, scripts: this.getReportJS(reportContext), - aggregations: this._getAggregations(results, forceV2), + aggregations: results.aggregations, auditsByCategory: this._createPWAAuditsByCategory(results.aggregations), runtimeConfig: results.runtimeConfig, reportsCatalog diff --git a/lighthouse-core/report/v2/report-generator.js b/lighthouse-core/report/v2/report-generator.js index 3654dc5fdec5..40d0647df71d 100644 --- a/lighthouse-core/report/v2/report-generator.js +++ b/lighthouse-core/report/v2/report-generator.js @@ -67,7 +67,8 @@ class ReportGeneratorV2 { return Object.assign({}, category, {audits, score}); }); - return {categories}; + const score = ReportGeneratorV2.arithmeticMean(categories); + return {score, categories}; } /** diff --git a/lighthouse-core/report/v2/report-renderer.js b/lighthouse-core/report/v2/report-renderer.js index 3c1fcab571ff..e78c7e513875 100644 --- a/lighthouse-core/report/v2/report-renderer.js +++ b/lighthouse-core/report/v2/report-renderer.js @@ -1,3 +1,22 @@ +/** + * Copyright 2017 Google Inc. All rights reserved. + * + * 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. + */ +'use strict'; + +/* eslint-env browser */ + class ReportRenderer { /** * @param {!Object} report diff --git a/lighthouse-core/runner.js b/lighthouse-core/runner.js index dd1d16fd441f..527094d02a57 100644 --- a/lighthouse-core/runner.js +++ b/lighthouse-core/runner.js @@ -135,11 +135,13 @@ class Runner { a => Aggregate.aggregate(a, runResults.auditResults)); } - // compute config v2 categories if available let reportCategories = []; + let score = 0; if (config.categories) { const reportGenerator = new ReportGeneratorV2(); - reportCategories = reportGenerator.generateReportJson(config, resultsById).categories; + const report = reportGenerator.generateReportJson(config, resultsById); + reportCategories = report.categories; + score = report.score; } return { @@ -150,6 +152,7 @@ class Runner { audits: resultsById, artifacts: runResults.artifacts, runtimeConfig: Runner.getRuntimeConfig(opts.flags), + score, reportCategories, aggregations }; diff --git a/lighthouse-core/test/report/v2/report-generator-test.js b/lighthouse-core/test/report/v2/report-generator-test.js index a5b4485cad1e..26bcab25a8f0 100644 --- a/lighthouse-core/test/report/v2/report-generator-test.js +++ b/lighthouse-core/test/report/v2/report-generator-test.js @@ -53,6 +53,18 @@ describe('ReportGeneratorV2', () => { }); describe('#generateReportJson', () => { + it('should return a score', () => { + const result = new ReportGeneratorV2().generateReportJson({ + categories: { + 'categoryA': {weight: 1, audits: [{id: 'auditA', weight: 1}]}, + 'categoryB': {weight: 4, audits: [{id: 'auditB', weight: 1}]}, + 'categoryC': {audits: []}, + } + }, {auditA: {score: 50}, auditB: {score: 100}}); + + assert.equal(result.score, 90); + }); + it('should return categories', () => { const result = new ReportGeneratorV2().generateReportJson({ categories: {