Last active
October 4, 2018 21:14
-
-
Save acdvorak/6cd85c6565c3d46f3e9662c85af873fe to your computer and use it in GitHub Desktop.
MDC Web Screenshot Datastore stat counter
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 CloudDatastore = require('../infra/lib/cloud-datastore'); | |
const cloudDatastore = new CloudDatastore(); | |
async function runAsync() { | |
const ds = cloudDatastore.datastore_; | |
const query = ds.createQuery('ScreenshotStatus'); | |
// runQuery returns an array: [resultArray, cursorInfoObject] | |
const queryResult = await ds.runQuery(query); | |
/** @type {!Array<!DatastoreScreenshotStatus>} */ | |
const statusArray = queryResult[0]; | |
const maps = {}; | |
const increment = (status, propName) => { | |
const value = status[propName]; | |
if (propName === 'state' && !/^(?:RUNNING|STARTING|PASSED|FAILED|ERROR)$/.test(value)) { | |
return; | |
} | |
maps[propName] = maps[propName] || {}; | |
maps[propName][value] = (maps[propName][value] || 0) + 1; | |
}; | |
statusArray.forEach((status) => { | |
increment(status, 'state'); | |
increment(status, 'git_branch'); | |
increment(status, 'pull_request_number'); | |
increment(status, 'git_commit_hash'); | |
increment(status, 'travis_build_id'); | |
increment(status, 'travis_build_number'); | |
increment(status, 'travis_job_id'); | |
increment(status, 'travis_job_number'); | |
}); | |
const displayables = []; | |
for (const [propName, valueMap] of Object.entries(maps)) { | |
let total = 0; | |
for (const [value, count] of Object.entries(valueMap)) { | |
total += count; | |
} | |
const valueArray = []; | |
for (const [value, count] of Object.entries(valueMap)) { | |
const percent = total > 0 ? (100 * count / total) : 0; | |
valueArray.push({value, count, percent}); | |
} | |
valueArray.sort((a, b) => b.count - a.count); | |
displayables.push({ | |
propName, | |
valueArray, | |
}); | |
} | |
const header = `= ${statusArray.length.toLocaleString()} status entities =`; | |
console.log(''.padStart(header.length, '=')); | |
console.log(header); | |
console.log(''.padStart(header.length, '=')); | |
console.log(''); | |
displayables.forEach((displayable) => { | |
console.log(`${displayable.propName} (${displayable.valueArray.length} unique values)`); | |
const blocklistRegExp = /^(?:git_commit_hash|travis_build_id|travis_build_number|travis_job_id|travis_job_number)$/; | |
if (blocklistRegExp.test(displayable.propName)) { | |
return; | |
} | |
console.log(''.padStart(displayable.propName.length, '-')); | |
displayable.valueArray.forEach((entry) => { | |
console.log(`${entry.value}\t${entry.count}\t${Math.round(entry.percent)}%`) | |
}); | |
console.log(''); | |
}); | |
} | |
runAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment