33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
55import { readFile } from "node:fs/promises" ;
6+ import { BigQuery } from "@google-cloud/bigquery" ;
67import { logger } from "../../app/functions/server/logging" ;
78
89const AUDITS_TO_INCLUDE = [
@@ -29,6 +30,42 @@ type LighthouseResult = {
2930 } ;
3031} ;
3132
33+ type LighthouseFullResult = {
34+ url : LighthouseResult [ "url" ] ;
35+ fetchTime : string ;
36+ isRepresentativeRun : boolean ;
37+ summary : LighthouseResult [ "summary" ] ;
38+ audits : {
39+ id : string ;
40+ score : number ;
41+ numericValue : number ;
42+ } [ ] ;
43+ } ;
44+
45+ async function uploadLighthouseReport ( results : LighthouseFullResult [ ] ) {
46+ if (
47+ ! process . env . BQ_LIGHTHOUSE_PROJECT ||
48+ ! process . env . BQ_LIGHTHOUSE_DATASET ||
49+ ! process . env . BQ_LIGHTHOUSE_TABLE
50+ ) {
51+ console . error ( "Missing environment variables" ) ;
52+ return ;
53+ }
54+
55+ try {
56+ const bigQueryClient = new BigQuery ( {
57+ projectId : process . env . BQ_LIGHTHOUSE_PROJECT ,
58+ } ) ;
59+
60+ const table = bigQueryClient
61+ . dataset ( process . env . BQ_LIGHTHOUSE_DATASET )
62+ . table ( process . env . BQ_LIGHTHOUSE_TABLE ) ;
63+ await table . insert ( results ) ;
64+ } catch ( error ) {
65+ console . error ( "Error uploading results" , JSON . stringify ( error , null , 2 ) ) ;
66+ }
67+ }
68+
3269async function run ( ) {
3370 // The Lighthouse report that will be created by running LHCI.
3471 const lighthouseResults : LighthouseResult [ ] =
@@ -49,7 +86,7 @@ async function run() {
4986 lighthouseResults
5087 . filter ( ( result ) => result . isRepresentativeRun === true )
5188 . map ( async ( medianResult ) => {
52- const { jsonPath, url, summary } = medianResult ;
89+ const { jsonPath, url, isRepresentativeRun , summary } = medianResult ;
5390 const fullReport = JSON . parse (
5491 await readFile ( new URL ( jsonPath , import . meta. url ) , {
5592 encoding : "utf8" ,
@@ -60,11 +97,26 @@ async function run() {
6097 return { id, score, numericValue } ;
6198 } ) ;
6299
63- return { url, summary, audits } ;
100+ return {
101+ url,
102+ fetchTime : fullReport . fetchTime ,
103+ isRepresentativeRun,
104+ summary,
105+ audits,
106+ } ;
64107 } ) ,
65108 ) ;
66109
67- logger . info ( "lighthouse_report" , lighthouseReport ) ;
110+ const reportPreview = lighthouseReport . map ( ( item ) => {
111+ return {
112+ url : item . url ,
113+ ...item . summary ,
114+ } ;
115+ } ) ;
116+ console . table ( reportPreview ) ;
117+
118+ await uploadLighthouseReport ( lighthouseReport ) ;
119+ console . info ( "Uploaded Lighthouse report successfully" ) ;
68120}
69121
70122try {
0 commit comments