Skip to content

Commit

Permalink
Added command line argument logic, output final console.log as CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxcrawford committed Aug 10, 2020
1 parent 7ddd1e1 commit 98117eb
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions scripts/collect-unresolved-breaches-addresses.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

console.log("test");
// TODO: Confirm db row has index

const Knex = require("knex");
const knexConfig = require("../db/knexfile");
Expand All @@ -10,7 +10,6 @@ const HIBP = require("../hibp");

async function checkIfBreachesExist(sha1, breaches) {
const breachResults = await HIBP.getBreachesForEmail(sha1, breaches, true);
console.log("checkIfBreachesExist", breachResults);

if (breachResults.length > 1) {
return true;
Expand All @@ -19,26 +18,51 @@ async function checkIfBreachesExist(sha1, breaches) {
return false;
}

function getArgsValue(argument) {
const cliArguments = process.argv;

if (cliArguments.indexOf(argument) < 0) {
throw new Error(`You are missing the argument: ${argument}`);
}

const arguemntIndex = cliArguments.indexOf(argument);
const value = cliArguments[(arguemntIndex + 1)];

if (!value ) {
throw new Error(`No value set for ${argument}.`);
}

const valueNumber = parseInt(value);

if (Number.isNaN(valueNumber)) {
throw new Error(`The value for ${argument} is not an interger.`);
}

return valueNumber;

}

(async () => {
console.log("init");

console.log(process.argv);

const allHibpBreachesResp = await HIBP.req("/breaches");
const allHibpBreaches = allHibpBreachesResp.body;

// "SELECT primary_email, primary_sha1 FROM subscribers WHERE signup_language LIKE 'en%' AND breaches_resolved IS NULL ORDER BY random();"
const limitQuery = getArgsValue("--limit");
const cohortSize = getArgsValue("--cohort-size");

// TODO: Make limit number a command line arguemnt
// "SELECT primary_email, primary_sha1 FROM subscribers WHERE signup_language LIKE 'en%' AND breaches_resolved IS NULL ORDER BY random();"

const results = await knex("subscribers").where("signup_language", "like", "en%").andWhere({breaches_resolved: null}).orderByRaw("RANDOM()").limit(100).select("primary_email", "primary_sha1");
const results = await knex("subscribers").where("signup_language", "like", "en%").andWhere({breaches_resolved: null}).orderByRaw("RANDOM()").limit(limitQuery).select("primary_email", "primary_sha1");

const cohort = [];


for (const record of results) {
// console.log(record);
// TODO: Make target number a command line arguemnt

if (cohort.length > 10) {
if (cohort.length > cohortSize) {
// Print Cohort
console.log(cohort);
break;
Expand All @@ -49,6 +73,6 @@ async function checkIfBreachesExist(sha1, breaches) {
if (isValidCohortMember) { cohort.push(record.primary_email); }
}

console.log(cohort);
console.log(cohort.toString());

})();

0 comments on commit 98117eb

Please sign in to comment.