-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtech_report_lighthouse.js
More file actions
107 lines (101 loc) · 2.72 KB
/
tech_report_lighthouse.js
File metadata and controls
107 lines (101 loc) · 2.72 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const pastMonth = constants.fnPastMonth(constants.currentMonth)
publish('tech_report_lighthouse', {
schema: 'reports',
type: 'incremental',
protected: true,
bigquery: {
partitionBy: 'date',
clusterBy: ['rank', 'geo']
},
tags: ['crux_ready']
}).preOps(ctx => `
CREATE TEMPORARY FUNCTION GET_LIGHTHOUSE(
records ARRAY<STRUCT<
client STRING,
median_lighthouse_score_accessibility NUMERIC,
median_lighthouse_score_best_practices NUMERIC,
median_lighthouse_score_performance NUMERIC,
median_lighthouse_score_seo NUMERIC
>>
)
RETURNS ARRAY<STRUCT<
name STRING,
desktop STRUCT<
median_score FLOAT64
>,
mobile STRUCT<
median_score FLOAT64
>
>>
LANGUAGE js AS '''
const METRIC_MAP = {
accessibility: 'median_lighthouse_score_accessibility',
best_practices: 'median_lighthouse_score_best_practices',
performance: 'median_lighthouse_score_performance',
seo: 'median_lighthouse_score_seo',
}
// Initialize the Lighthouse map.
const lighthouse = Object.fromEntries(Object.keys(METRIC_MAP).map(metricName => {
return [metricName, {name: metricName}]
}));
// Populate each client record.
records.forEach(record => {
Object.entries(METRIC_MAP).forEach(([metricName, median_score]) => {
lighthouse[metricName][record.client] = {median_score: record[median_score]}
});
});
return Object.values(lighthouse)
''';
DELETE FROM ${ctx.self()}
WHERE date = '${pastMonth}';
`).query(ctx => `
SELECT
date,
geo,
rank,
technology,
version,
GET_LIGHTHOUSE(ARRAY_AGG(STRUCT(
client,
median_lighthouse_score.accessibility,
median_lighthouse_score.best_practices,
median_lighthouse_score.performance,
median_lighthouse_score.seo
))) AS lighthouse
FROM ${ctx.ref('reports', 'tech_crux')}
WHERE date = '${pastMonth}'
GROUP BY
date,
geo,
rank,
technology,
version
`).postOps(ctx => `
SELECT
reports.run_export_job(
JSON '''{
"destination": "firestore",
"config": {
"database": "tech-report-api-${constants.environment}",
"collection": "lighthouse",
"type": "report",
"date": "${pastMonth}"
},
"query": "SELECT STRING(date) AS date, * EXCEPT(date) FROM ${ctx.self()} WHERE date = '${pastMonth}'"
}'''
);
// legacy export to tech-report-apis database
SELECT
reports.run_export_job(
JSON '''{
"destination": "firestore",
"config": {
"database": "tech-report-api-${constants.environment}",
"collection": "lighthouse",
"type": "report",
"date": "${pastMonth}"
},
"query": "SELECT STRING(date) AS date, * EXCEPT(date, version) FROM ${ctx.self()} WHERE date = '${pastMonth}' AND version = 'ALL'"
}'''
);
`)