Last active
May 31, 2022 19:10
-
-
Save eeeps/a9b607e6fbf727fc89ca78c698ac0152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TEMPORARY FUNCTION getImageDensity(payload STRING) | |
RETURNS ARRAY<STRUCT<clientWidth INT64, approximateResourceWidth INT64, d FLOAT64>> | |
LANGUAGE js AS ''' | |
try { | |
var $ = JSON.parse(payload); | |
var responsiveImages = JSON.parse($._responsive_images); | |
responsiveImages = responsiveImages['responsive-images']; | |
return responsiveImages.map(({approximateResourceWidth, clientWidth}) => ({ | |
approximateResourceWidth: parseInt(approximateResourceWidth) || 0, | |
clientWidth: parseInt(clientWidth) || 0, | |
d: (parseInt(clientWidth) > 0 ? parseInt(approximateResourceWidth) / parseInt(clientWidth) : 0), | |
})); | |
} catch (e) { | |
return []; | |
} | |
'''; | |
SELECT | |
percentile, | |
client, | |
APPROX_QUANTILES(image.d, 1000)[OFFSET(percentile * 10)] AS imgDensity | |
FROM ( | |
SELECT | |
_TABLE_SUFFIX AS client, | |
image | |
FROM | |
`httparchive.pages.2022_05_01_*`, | |
UNNEST(getImageDensity(payload)) AS image), | |
UNNEST([10, 25, 50, 75, 90]) AS percentile | |
WHERE image.approximateResourceWidth > 1 AND image.clientWidth > 0 | |
GROUP BY | |
percentile, | |
client | |
ORDER BY | |
percentile, | |
client |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results