forked from asmexcaliburwoods/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.js
121 lines (104 loc) · 3.34 KB
/
stats.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Component } from 'react'
import fetch from 'isomorphic-unfetch'
import { object, string, boolean, number } from 'yup'
import Table, { Th, Td } from '../../table'
import Widget from '../../widget'
const schema = object().shape({
url: string().url().required(),
filterThirdPartyResources: boolean(),
interval: number(),
strategy: string(),
title: string()
})
export default class PageSpeedInsightsStats extends Component {
static defaultProps = {
filterThirdPartyResources: false,
interval: 1000 * 60 * 60 * 12,
strategy: 'desktop',
title: 'PageSpeed Stats'
}
state = {
stats: {},
loading: true,
error: false
}
componentDidMount () {
schema.validate(this.props)
.then(() => this.fetchInformation())
.catch((err) => {
console.error(`${err.name} @ ${this.constructor.name}`, err.errors)
this.setState({ error: true, loading: false })
})
}
componentWillUnmount () {
clearTimeout(this.timeout)
}
bytesToKilobytes (bytes) {
return bytes > 0 ? (bytes / 1024).toFixed(1) : 0
}
async fetchInformation () {
const { url, filterThirdPartyResources, strategy } = this.props
const searchParams = [
`url=${url}`,
`filter_third_party_resources=${filterThirdPartyResources}`,
`strategy=${strategy}`
].join('&')
try {
const res = await fetch(`https://www.googleapis.com/pagespeedonline/v2/runPagespeed?${searchParams}`)
const json = await res.json()
const pageStats = json.pageStats
const stats = {
cssCount: pageStats.numberCssResources || 0,
cssSize: this.bytesToKilobytes(pageStats.cssResponseBytes),
htmlSize: this.bytesToKilobytes(pageStats.htmlResponseBytes),
imageSize: this.bytesToKilobytes(pageStats.imageResponseBytes),
javascriptCount: pageStats.numberJsResources || 0,
javascriptSize: this.bytesToKilobytes(pageStats.javascriptResponseBytes),
requestCount: pageStats.numberResources || 0,
requestSize: this.bytesToKilobytes(pageStats.totalRequestBytes),
otherSize: this.bytesToKilobytes(pageStats.otherResponseBytes)
}
this.setState({ error: false, loading: false, stats })
} catch (error) {
this.setState({ error: true, loading: false })
} finally {
this.timeout = setTimeout(() => this.fetchInformation(), this.props.interval)
}
}
render () {
const { error, loading, stats } = this.state
const { title } = this.props
return (
<Widget title={title} loading={loading} error={error}>
<Table>
<tbody>
<tr>
<Th>Request</Th>
<Td>{stats.requestSize} KB ({stats.requestCount})</Td>
</tr>
<tr>
<Th>JavaScript</Th>
<Td>{stats.javascriptSize} KB ({stats.javascriptCount})</Td>
</tr>
<tr>
<Th>CSS</Th>
<Td>{stats.cssSize} KB ({stats.cssCount})</Td>
</tr>
<tr>
<Th>HTML</Th>
<Td>{stats.htmlSize} KB</Td>
</tr>
<tr>
<Th>Image</Th>
<Td>{stats.imageSize} KB</Td>
</tr>
<tr>
<Th>Other</Th>
<Td>{stats.otherSize} KB</Td>
</tr>
</tbody>
</Table>
</Widget>
)
}
}