-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
supports_criteria.sql
66 lines (57 loc) · 1.58 KB
/
supports_criteria.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
#standardSQL
CREATE TEMPORARY FUNCTION getSupports(css STRING)
RETURNS ARRAY<STRING>
LANGUAGE js
OPTIONS (library = "gs://httparchive/lib/css-utils.js")
AS '''
try {
function compute(ast) {
let ret = {};
walkRules(ast, rule => {
incrementByKey(ret, "total");
let condition = rule.supports;
// Drop whitespace around parens
condition = condition.replace(/\\s*\\(\\s*/g, "(").replace(/\\s*\\)\\s*/g, ")");
// Match property: value queries first
for (let match of condition.matchAll(/\\([\\w-]+\\s*:/g)) {
let arg = parsel.gobbleParens(condition, match.index);
incrementByKey(ret, arg);
}
// Then find selector queries
for (let match of condition.matchAll(/selector\\(/gi)) {
let arg = parsel.gobbleParens(condition, match.index + match[0].length - 1);
incrementByKey(ret, "selector" + arg);
}
}, {type: "supports"});
ret = sortObject(ret);
return ret;
}
const ast = JSON.parse(css);
let supports = compute(ast);
return Object.entries(supports).filter(([criteria]) => {
return criteria != 'total';
}).flatMap(([criteria, freq]) => {
return new Array(freq).fill(criteria);
});
} catch (e) {
return [];
}
''';
SELECT
client,
supports,
COUNT(DISTINCT page) AS pages,
COUNT(0) AS freq,
SUM(COUNT(0)) OVER (PARTITION BY client) AS total,
COUNT(0) / SUM(COUNT(0)) OVER (PARTITION BY client) AS pct
FROM
`httparchive.almanac.parsed_css`,
UNNEST(getSupports(css)) AS supports
WHERE
date = '2021-07-01'
GROUP BY
client,
supports
ORDER BY
pct DESC
LIMIT 300