Skip to content

Commit

Permalink
Modifications in response to change requests [WIP]
Browse files Browse the repository at this point in the history
Strip out uneeded changes from prior commits on this branch.

Add `configIsSubmitAndUpload()` to lib/index.ts
Pass that fn to Sheets class.

Add null value `viwerUrl` to Metrics class return object to prevent undefined type errors in Sheets

paulirish#131
  • Loading branch information
David Benson committed Aug 4, 2017
1 parent d770a4c commit 22e2558
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
13 changes: 10 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ 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);
await sheets.appendResults(results.runs, this.flags.upload);
const sheets = new Sheets(this.sheets, this.clientSecret, this.configIsSubmitAndUpload);
await sheets.appendResults(results.runs);
}
}

Expand Down Expand Up @@ -207,7 +207,6 @@ class PWMetrics {
const driveResponse = await upload(data, this.clientSecret);

preparedData.fileId = driveResponse.id;
preparedData.fileName = driveResponse.name;

this.view(driveResponse.id);
}
Expand Down Expand Up @@ -235,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: 2 additions & 2 deletions lib/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ function prepareData(res: LighthouseResults): MetricsResults {

return {
fileId: '',
fileName: '',
timings,
timestamps,
generatedTime: res.generatedTime,
lighthouseVersion: res.lighthouseVersion,
initialUrl: res.initialUrl,
url: res.url
url: res.url,
viewerUrl: null
};
}
16 changes: 6 additions & 10 deletions lib/sheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const metricsIds = require('../metrics').ids;
const SHEET_TYPES = {
'GOOGLE_SHEETS': 'GOOGLE_SHEETS'
};
const VIEWER_URL_PREFIX = 'https://chromedevtools.github.io/timeline-viewer/?loadTimelineFromURL=drive://';

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

Expand All @@ -39,23 +38,20 @@ class Sheets {
}
}

appendResults(results: Array<MetricsResults>, upload: boolean) {
appendResults(results: Array<MetricsResults>) {
switch (this.config.type) {
case SHEET_TYPES.GOOGLE_SHEETS:
return this.appendResultsToGSheets(results, upload);
return this.appendResultsToGSheets(results);
}
}

async appendResultsToGSheets(results: Array<MetricsResults>, upload: boolean) {
async appendResultsToGSheets(results: Array<MetricsResults>) {
let valuesToAppend: Array<GSheetsValuesToAppend> = [];
results.forEach(data => {
const getTiming = (key: string) => data.timings.find(t => t.id === key).timing;
const dateObj = new Date(data.generatedTime);
let viewerUrl = '';

if(upload && typeof data.fileId !== 'undefined') {
viewerUrl = VIEWER_URL_PREFIX + data.fileId;
}
data = this.configIsSubmitAndUpload(data);

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

Expand Down
4 changes: 2 additions & 2 deletions types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ interface FeatureFlags {
expectations: Boolean;
output: Boolean;
chromeFlags: Array<string>;
chromePath?: string
chromePath?: string;
port?: number;
}

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

interface PWMetricsResults {
Expand Down

0 comments on commit 22e2558

Please sign in to comment.