Last active
August 23, 2020 05:41
-
-
Save rviscomi/7446359adca78164c0aad83407211ec4 to your computer and use it in GitHub Desktop.
HTTP Archive crawl status scraper
This file contains hidden or 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
const DESKTOP_API = 'https://dev.httparchive.org/bulktest/batch_report.php'; | |
const MOBILE_API = 'https://mobile.httparchive.org/bulktest/batch_report.php'; | |
const pattern = new RegExp(); | |
pattern.compile(/initial URLs\s+\d+\s+(\d+)\nsubmitted\s+\d+\s+(\d+)\ntested\s+\d+\s+(\d+)\nobtained\s+\d+\s+(\d+)\nHAR parsed\s+\d+\s+(\d+)\s+DONE:\s+(\d+).*success:\s+(\d+).*failed:\s+(\d+).*completed passes:\s+(\d)/s); | |
const patternMap = { | |
1: 'initial URLs', | |
2: 'submitted', | |
3: 'tested', | |
4: 'obtained', | |
5: 'HAR parsed', | |
6: 'DONE', | |
7: 'success', | |
8: 'failed', | |
9: 'completed passes', | |
10: 'tests per hour' | |
}; | |
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
function run() { | |
sheet.clear(); | |
let cell = sheet.setCurrentCell(sheet.getRange(1, 1)); | |
cell.setValue('Client'); | |
cell = sheet.setCurrentCell(sheet.getRange(1, 2)); | |
cell.setValue('Timestamp'); | |
Object.entries(patternMap).forEach(([id, value]) => { | |
const cell = sheet.setCurrentCell(sheet.getRange(1, parseInt(id, 10) + 2)); | |
cell.setValue(value); | |
}); | |
grabLatestStats('Desktop', DESKTOP_API); | |
grabLatestStats('Mobile', MOBILE_API); | |
} | |
function grabLatestStats(client, url) { | |
const response = UrlFetchApp.fetch(url).getContentText(); | |
parseStats(client, response); | |
} | |
function parseStats(client, response) { | |
if (!pattern.test(response)) { | |
return; | |
} | |
const stats = response.match(pattern); | |
let testsPerHour = 0; | |
try { | |
testsPerHour = getTestsPerHour(response); | |
} catch (e) { | |
Logger.log('Error: ' + e); | |
} | |
sheet.appendRow([client, new Date().toLocaleString()].concat(stats.slice(1,10)).concat([testsPerHour])); | |
} | |
function getTestsPerHour(response) { | |
const stats = Array.from(response.matchAll(/DONE:.*/g)).filter((e, i) => i == 1 || i == 4); | |
const [end, start] = stats.map(i => Array.from(i[0].match(/DONE:\s+(\d+)\s+(.*)/)).slice(1)).map(([done, time]) => ({done: +done, time: +new Date(`${time}, ${new Date().getFullYear()}`)})); | |
return Math.floor((end.done - start.done) * 3600000 / (end.time - start.time)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment