Skip to content
13 changes: 12 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PWMetrics {
console.log(messages.getMessage('MEDIAN_RUN'));
this.displayOutput(results.median);
} else if (this.flags.submit) {
const sheets = new Sheets(this.sheets, this.clientSecret);
const sheets = new Sheets(this.sheets, this.clientSecret, this.configIsSubmitAndUpload);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if not pass function to the sheets but extend results.runs with viewerUrl like

const sheets = new Sheets(this.sheets, this.clientSecret, this.configIsSubmitAndUpload);
await sheets.appendResults(extendResultsToSubmit(results.runs));

....
//extendResultsToSubmit method
return results.map(data => {
  if (this.flags.submit && this.flags.upload) {
    data.viewerUrl = getTimelineViewerUrl(data.fileId);
  }
  return data;
});

in this case we can cut off some stuff
wdyt?

await sheets.appendResults(results.runs);
}
}
Expand Down Expand Up @@ -205,6 +205,9 @@ class PWMetrics {

if (this.flags.upload) {
const driveResponse = await upload(data, this.clientSecret);

preparedData.fileId = driveResponse.id;

this.view(driveResponse.id);
}

Expand All @@ -231,6 +234,14 @@ class PWMetrics {
return data;
}

configIsSubmitAndUpload = (data: MetricsResults) => {
if(this.flags.submit && this.flags.upload) {
data.viewerUrl = getTimelineViewerUrl(data.fileId);
}

return data;
}

showChart(data: MetricsResults) {
// reverse to preserve the order, because cli-chart.
let timings = data.timings;
Expand Down
4 changes: 3 additions & 1 deletion lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ function prepareData(res: LighthouseResults): MetricsResults {
});

return {
fileId: '',
timings,
timestamps,
generatedTime: res.generatedTime,
lighthouseVersion: res.lighthouseVersion,
initialUrl: res.initialUrl,
url: res.url
url: res.url,
viewerUrl: null
};
}
8 changes: 6 additions & 2 deletions lib/sheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SHEET_TYPES = {
};

class Sheets {
constructor(public config: SheetsConfig, public clientSecret: AuthorizeCredentials) {
constructor(public config: SheetsConfig, public clientSecret: AuthorizeCredentials, public configIsSubmitAndUpload: Function) {
this.validateOptions(config, clientSecret);
}

Expand Down Expand Up @@ -50,6 +50,9 @@ class Sheets {
results.forEach(data => {
const getTiming = (key: string) => data.timings.find(t => t.id === key).timing;
const dateObj = new Date(data.generatedTime);

data = this.configIsSubmitAndUpload(data);

// order matters
valuesToAppend.push([
data.lighthouseVersion,
Expand All @@ -62,7 +65,8 @@ class Sheets {
getTiming(metricsIds.VC100),
getTiming(metricsIds.TTFI),
getTiming(metricsIds.TTCI),
getTiming(metricsIds.VC85)
getTiming(metricsIds.VC85),
data.viewerUrl
]);
});

Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ Show Lighthouse traces in timeline-viewer.

[timeline-viewer](https://chromedevtools.github.io/timeline-viewer/) - Shareable URLs for your Chrome DevTools Timeline traces.

If both upload and submit flags are set, a link to the viewer for each result trace will be added to the Notes column of the sheet. This is particularly useful when multiple runs have been stored, as otherwise it is dificult to view the trace for any given run.


```sh
# run pwmetrics with config in package.json
Expand Down
4 changes: 3 additions & 1 deletion types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ interface FeatureFlags {
expectations: Boolean;
output: Boolean;
chromeFlags: Array<string>;
chromePath?: string
chromePath?: string;
port?: number;
}

interface MetricsResults {
fileId: string,
timestamps: Timestamp[];
timings: Timing[];
generatedTime: string;
lighthouseVersion: string;
url: string;
initialUrl: string;
viewerUrl?: string;
}

interface PWMetricsResults {
Expand Down