-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
custom_property_depth.sql
105 lines (90 loc) · 2.48 KB
/
custom_property_depth.sql
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
#standardSQL
CREATE TEMPORARY FUNCTION getCustomPropertyLengths(payload STRING)
RETURNS ARRAY<STRUCT<depth INT64, freq INT64>>
LANGUAGE js
OPTIONS (library = "gs://httparchive/lib/css-utils.js")
AS '''
try {
function compute(vars) {
function walkElements(node, callback, parent) {
if (Array.isArray(node)) {
for (let n of node) {
walkElements(n, callback, parent);
}
}
else {
callback(node, parent);
if (node.children) {
walkElements(node.children, callback, node);
}
}
}
let ret = {
depths: {}
};
function countDependencyLength(node, property) {
if (!node) {
return 0;
}
let declarations = node.declarations;
if (!declarations || !(property in declarations)) {
return countDependencyLength(node.parent, property);
}
let o = declarations[property];
if (!o.references || o.references.length === 0) {
return 0;
}
let lengths = o.references.map(p => countDependencyLength(node, p));
return 1 + Math.max(...lengths);
}
walkElements(vars.computed, (node, parent) => {
if (parent && !node.parent) {
node.parent = parent;
}
if (node.declarations) {
for (let property in node.declarations) {
let o = node.declarations[property];
if (o.computed && o.computed.trim() !== o.value.trim() && (o.computed === "initial" || o.computed === "null")) {
// Cycle or missing ref
incrementByKey(ret, "cycles_or_initial");
}
else {
let depth = countDependencyLength(node, property);
incrementByKey(ret.depths, depth);
}
}
}
});
return ret;
}
var $ = JSON.parse(payload);
var vars = JSON.parse($['_css-variables']);
if (!vars || !vars.computed) return null;
var custom_props = compute(vars);
return Object.entries(custom_props.depths).map(([depth, freq]) => ({depth, freq}))
} catch (e) {
return [];
}
''';
SELECT
client,
depth,
COUNT(DISTINCT url) AS pages,
SUM(freq) AS freq,
SUM(SUM(freq)) OVER (PARTITION BY client) AS total,
SUM(freq) / SUM(SUM(freq)) OVER (PARTITION BY client) AS pct
FROM (
SELECT
_TABLE_SUFFIX AS client,
url,
custom_properties.depth,
custom_properties.freq
FROM
`httparchive.pages.2021_07_01_*`,
UNNEST(getCustomPropertyLengths(payload)) AS custom_properties)
GROUP BY
client,
depth
ORDER BY
depth,
client