-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathindex.ts
More file actions
87 lines (73 loc) · 2.88 KB
/
Copy pathindex.ts
File metadata and controls
87 lines (73 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE
import { SheetsConfig, MetricsResults, Oauth2Client, AuthorizeCredentials, GSheetsValuesToAppend } from '../../types/types';
const GoogleOauth = require('../oauth/google-oauth');
import * as gsheets from './gsheets';
// @todo add 'import' after moving all stuff to typescript
const { getMessage } = require('../utils/messages');
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) {
this.validateOptions(config, clientSecret);
}
validateOptions(config: SheetsConfig, clientSecret: AuthorizeCredentials) {
if (!config || !Object.keys(config).length)
throw new Error(getMessage('NO_GOOGLE_SHEET_OPTIONS'));
const sheetType = config.type;
if (!Object.keys(SHEET_TYPES).includes(sheetType)) {
throw new Error(getMessage('NO_SHEET_TYPE', sheetType));
}
switch (sheetType) {
case SHEET_TYPES.GOOGLE_SHEETS:
if (!config.options.spreadsheetId || !config.options.tableName || !clientSecret)
throw new Error(getMessage('NO_GOOGLE_SHEET_OPTIONS'));
break;
default:
throw new Error(getMessage('NO_GOOGLE_SHEET_OPTIONS'));
}
}
appendResults(results: Array<MetricsResults>) {
switch (this.config.type) {
case SHEET_TYPES.GOOGLE_SHEETS:
return this.appendResultsToGSheets(results);
}
}
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(typeof data.fileName !== 'undefined' && typeof data.fileName !== 'undefined') {
viewerUrl = VIEWER_URL_PREFIX + data.fileId;
}
// order matters
valuesToAppend.push([
data.lighthouseVersion,
data.url,
`${dateObj.toLocaleDateString()} ${dateObj.toLocaleTimeString()}`,
getTiming(metricsIds.TTFCP),
getTiming(metricsIds.TTFMP),
getTiming(metricsIds.PSI),
getTiming(metricsIds.FV),
getTiming(metricsIds.VC100),
getTiming(metricsIds.TTFI),
getTiming(metricsIds.TTCI),
getTiming(metricsIds.VC85),
viewerUrl
]);
});
try {
const googleOauth = new GoogleOauth();
const oauth: Oauth2Client = await googleOauth.authenticate(this.clientSecret);
await gsheets.appendResults(oauth, valuesToAppend, this.config.options);
} catch(error) {
throw error;
}
}
}
module.exports = Sheets;