Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Nov 30, 2023
1 parent 7811124 commit 819a449
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: docker-compose-actions-workflow
on:
on:
push:
pull_request:
branches:
Expand Down
56 changes: 30 additions & 26 deletions dev/cassandraRandom.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
const cassandra = require('../store/cassandra');
const db = require('../store/db');
const cassandra = require("../store/cassandra");
const db = require("../store/db");

function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}

let total = 0;
let haveRows = 0;

async function start() {
// Get the current max_match_id from postgres, subtract 200000000
let max = (await db.raw(`select max(match_id) from public_matches`))?.rows?.[0]?.max;
let limit = max - 200000000;
while(true) {
// Test a random match ID
const rand = randomInteger(1, limit);
// Get the current max_match_id from postgres, subtract 200000000
let max = (await db.raw(`select max(match_id) from public_matches`))
?.rows?.[0]?.max;
let limit = max - 200000000;
while (true) {
// Test a random match ID
const rand = randomInteger(1, limit);

let result = await cassandra.execute(`select match_id, player_slot, stuns from player_matches where match_id = ?`, [rand.toString()], {
prepare: true,
fetchSize: 10,
autoPage: true,
});
total += 1;
// Check if there are rows
if (result.rows.length) {
haveRows += 1;
console.log(result.rows[0].match_id.toString(), 'has rows');
}
if (total % 100 === 0) {
// Log number that have rows/don't have rows
console.log(haveRows, '/', total, 'have rows');
}
let result = await cassandra.execute(
`select match_id, player_slot, stuns from player_matches where match_id = ?`,
[rand.toString()],
{
prepare: true,
fetchSize: 10,
autoPage: true,
}
);
total += 1;
// Check if there are rows
if (result.rows.length) {
haveRows += 1;
console.log(result.rows[0].match_id.toString(), "has rows");
}

if (total % 100 === 0) {
// Log number that have rows/don't have rows
console.log(haveRows, "/", total, "have rows");
}
}
}

start();
2 changes: 1 addition & 1 deletion processors/performanceOthers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function greevilsGreed(e, container, meta) {

const goldBase = 3;
let goldStack = e.greevils_greed_stack * 3;
goldStack = Math.min(goldStack, 18);
goldStack = Math.min(goldStack, 18);

alchPlayer.performance_others = {
greevils_greed_gold: 0,
Expand Down
34 changes: 19 additions & 15 deletions routes/keyManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function getOpenInvoices(customerId) {
const invoices = await stripe.invoices.list({
customer: customerId,
limit: 100,
status: 'open'
status: "open",
});

return invoices.data;
Expand Down Expand Up @@ -81,13 +81,13 @@ keys
async.parallel(
{
customer: (cb) => {
if(!keyRecord) {
if (!keyRecord) {
return cb();
}

const { api_key, customer_id, subscription_id} = keyRecord;
const { api_key, customer_id, subscription_id } = keyRecord;
const toReturn = {
api_key
api_key,
};

stripe.customers
Expand All @@ -107,23 +107,22 @@ keys
.catch((err) => cb(err));
},
openInvoices: (cb) => {
if(allKeyRecords.length === 0) {
if (allKeyRecords.length === 0) {
return cb();
}

customer_id = allKeyRecords[0].customer_id;

getOpenInvoices(customer_id).then(invoices => {
const processed = invoices.map(i => ({
getOpenInvoices(customer_id).then((invoices) => {
const processed = invoices.map((i) => ({
id: i.id,
amountDue: i.amount_due,
paymentLink: i.hosted_invoice_url,
created: i.created
created: i.created,
}));

return cb(null,processed);
return cb(null, processed);
});

},
usage: (cb) => {
db.raw(
Expand Down Expand Up @@ -218,14 +217,19 @@ keys
return res.sendStatus(200);
}
// returning customer
else if(allKeyRecords.length > 0) {
else if (allKeyRecords.length > 0) {
customer_id = allKeyRecords[0].customer_id;

const invoices = await getOpenInvoices(customer_id);

if (invoices.length > 0) {
console.log("Open invoices exist for", req.user.account_id, "customer", customer_id);
return res.status(402).json({error: "Open invoice"});
console.log(
"Open invoices exist for",
req.user.account_id,
"customer",
customer_id
);
return res.status(402).json({ error: "Open invoice" });
}

try {
Expand All @@ -239,7 +243,7 @@ keys
}
}
// New customer -> create customer first
else {
else {
try {
const customer = await stripe.customers.create({
source: token.id,
Expand All @@ -249,7 +253,7 @@ keys
},
});
customer_id = customer.id;
} catch (err) {
} catch (err) {
// probably insufficient funds
return res.status(402).json(err);
}
Expand Down
14 changes: 10 additions & 4 deletions store/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -1443,15 +1443,20 @@ function insertMatch(match, options, cb) {
}

function decideScenarios(cb) {
if (options.type === 'parsed' &&
match.match_id % 100 < config.SCENARIOS_SAMPLE_PERCENT) {
if (
options.type === "parsed" &&
match.match_id % 100 < config.SCENARIOS_SAMPLE_PERCENT
) {
return redis.rpush("scenariosQueue", match.match_id, cb);
}
return cb();
}

function decideBenchmarks(cb) {
if (options.origin === "scanner" && match.match_id % 100 < config.BENCHMARKS_SAMPLE_PERCENT) {
if (
options.origin === "scanner" &&
match.match_id % 100 < config.BENCHMARKS_SAMPLE_PERCENT
) {
return redis.rpush("parsedBenchmarksQueue", match.match_id, cb);
}
return cb();
Expand Down Expand Up @@ -1573,7 +1578,8 @@ function insertMatch(match, options, cb) {
},
},
{
priority: (match.leagueid || hasTrackedPlayer) ? -1 : options.priority,
priority:
match.leagueid || hasTrackedPlayer ? -1 : options.priority,
attempts: options.attempts || 15,
},
cb
Expand Down
16 changes: 11 additions & 5 deletions svc/apiadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,19 @@ async function updateStripeUsage(cb) {
// but we'd have to make changes to web.js and metrics
await stripe.subscriptionItems.createUsageRecord(sub.items.data[0].id, {
quantity: Math.ceil(usageCount / config.API_BILLING_UNIT),
action: 'set',
action: "set",
timestamp: sub.current_period_end - 1,
});
console.log("updateStripeUsage updated", sub.id, usageCount, Math.ceil(usageCount / config.API_BILLING_UNIT));
console.log(
"updateStripeUsage updated",
sub.id,
usageCount,
Math.ceil(usageCount / config.API_BILLING_UNIT)
);
} else {
// console.log(`updateStripeUsage No usage for ${sub.id}`);
}
}
}
console.log(`updateStripeUsage processed ${num} records`);
cb();
} catch (err) {
Expand All @@ -167,7 +172,6 @@ async function updateStripeUsage(cb) {

invokeInterval(function updateAPIKeysInRedis(cb) {
queries.getAPIKeys(db, (err, rows) => {

if (err) {
cb(err);
} else if (rows.length > 0) {
Expand All @@ -190,5 +194,7 @@ invokeInterval(function updateAPIKeysInRedis(cb) {
});
}, 5 * 60 * 1000); // Update every 5 min

invokeInterval(function runStoreUsageCounts(cb){storeUsageCounts(0, cb);}, 10 * 60 * 1000); // Every 10 minutes
invokeInterval(function runStoreUsageCounts(cb) {
storeUsageCounts(0, cb);
}, 10 * 60 * 1000); // Every 10 minutes
invokeInterval(updateStripeUsage, 5 * 60 * 1000); // Every 5 minutes
104 changes: 66 additions & 38 deletions svc/cassandraDelete.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,79 @@
const cassandra = require('../store/cassandra');
const db = require('../store/db');
const crypto = require('crypto');
const cassandra = require("../store/cassandra");
const db = require("../store/db");
const crypto = require("crypto");

function genRandomNumber(byteCount, radix) {
return BigInt('0x' + crypto.randomBytes(byteCount).toString('hex')).toString(radix)
return BigInt("0x" + crypto.randomBytes(byteCount).toString("hex")).toString(
radix
);
}

const PARSED_DATA_DELETE_ID = 0;

async function start() {
// Get the current max_match_id from postgres, subtract 200000000
let max = (await db.raw(`select max(match_id) from public_matches`))?.rows?.[0]?.max;
let limit = max - 200000000;
while(true) {
// delete older unparsed match/player_match rows
// We can backfill these from Steam API on demand
try {
// Convert to signed bigint
const randomBigint = BigInt.asIntN(64, genRandomNumber(8, 10));
let result = await cassandra.execute(`select match_id, version, token(match_id) from matches where token(match_id) >= ? limit 500 ALLOW FILTERING;`, [randomBigint.toString()], {
prepare: true,
fetchSize: 500,
autoPage: true,
});

// Put the ones that don't have parsed data or are too old into an array
let ids = result.rows.filter(result => (result.version == null || result.match_id < PARSED_DATA_DELETE_ID) && result.match_id < limit).map(result => result.match_id);
console.log(ids.length, 'out of', result.rows.length, 'to delete, ex:', ids[0]?.toString());
// Get the current max_match_id from postgres, subtract 200000000
let max = (await db.raw(`select max(match_id) from public_matches`))
?.rows?.[0]?.max;
let limit = max - 200000000;
while (true) {
// delete older unparsed match/player_match rows
// We can backfill these from Steam API on demand
try {
// Convert to signed bigint
const randomBigint = BigInt.asIntN(64, genRandomNumber(8, 10));
let result = await cassandra.execute(
`select match_id, version, token(match_id) from matches where token(match_id) >= ? limit 500 ALLOW FILTERING;`,
[randomBigint.toString()],
{
prepare: true,
fetchSize: 500,
autoPage: true,
}
);

// Delete matches
await Promise.all(ids.map(id => cassandra.execute(`DELETE from matches where match_id = ?`, [id], {
prepare: true,
})
));
// Delete player_matches
await Promise.all(ids.map(id => cassandra.execute(`DELETE from player_matches where match_id = ?`, [id], {
prepare: true,
})
));
// Put the ones that don't have parsed data or are too old into an array
let ids = result.rows
.filter(
(result) =>
(result.version == null ||
result.match_id < PARSED_DATA_DELETE_ID) &&
result.match_id < limit
)
.map((result) => result.match_id);
console.log(
ids.length,
"out of",
result.rows.length,
"to delete, ex:",
ids[0]?.toString()
);

const parsedIds = result.rows.filter(result => result.version != null).map(result => result.match_id);
await Promise.all(parsedIds.map(id => db.raw(`INSERT INTO parsed_matches(match_id) VALUES(?) ON CONFLICT DO NOTHING`, [Number(id)])));
} catch(e) {
console.log(e);
}
// Delete matches
await Promise.all(
ids.map((id) =>
cassandra.execute(`DELETE from matches where match_id = ?`, [id], {
prepare: true,
})
)
);
// Delete player_matches
await Promise.all(
ids.map((id) =>
cassandra.execute(
`DELETE from player_matches where match_id = ?`,
[id],
{
prepare: true,
}
)
)
);
const parsedIds = result.rows.filter(result => result.version != null).map(result => result.match_id);
await Promise.all(parsedIds.map(id => db.raw(`INSERT INTO parsed_matches(match_id) VALUES(?) ON CONFLICT DO NOTHING`, [Number(id)])));
} catch (e) {
console.log(e);
}

}
}

start();
Loading

0 comments on commit 819a449

Please sign in to comment.