Skip to content

Commit

Permalink
Adjust analytics with more stats on active players
Browse files Browse the repository at this point in the history
  • Loading branch information
nhamonin committed Dec 15, 2024
1 parent 6c52ed8 commit 45ade07
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
27 changes: 25 additions & 2 deletions database/repositories/baseRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,35 @@ export class BaseRepository {
});

readBy = withErrorHandling(async (criteria) => {
const result = await this.db(this.tableName).where(criteria).first();
let query = this.db(this.tableName);
for (const [column, condition] of Object.entries(criteria)) {
if (typeof condition === 'object') {
for (const [operator, value] of Object.entries(condition)) {
query = query.where(column, operator, value);
}
} else {
query = query.where(column, condition);
}
}
const result = await query.first();
return result || null;
});

readAllBy = withErrorHandling(async (criteria, options = {}) => {
let query = this.db(this.tableName).where(criteria);
let query = this.db(this.tableName);

// Handle criteria with possible operators
for (const [column, condition] of Object.entries(criteria)) {
if (typeof condition === 'object') {
// Process operator-based conditions
for (const [operator, value] of Object.entries(condition)) {
query = query.where(column, operator, value);
}
} else {
// Process simple equality conditions
query = query.where(column, condition);
}
}

if (options.excludeNull) {
query = query.whereNotNull(options.excludeNull);
Expand Down
21 changes: 15 additions & 6 deletions services/commands/getAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ export async function getAnalytics() {
const avgPredictions = matchPrediction?.avgPredictions || 0;
const winratePrediction = matchPrediction?.winratePredictions || 0;
const webhookListLength = await webhookMgr.getRestrictionsCount();

const actionThresholds = [0, 10, 100];
const [totalActivePlayers, totalActivePlayersGt10, totalActivePlayersGt100] = await Promise.all(
actionThresholds.map((threshold) =>
database.teams
.readAllBy({ actions_used: { '>': threshold } }) // Fetch rows with actions_used > threshold
.then((res) => res?.length || 0)
)
);

const text = [
`winrate predictions: ${(
(winratePrediction / totalMatches || 0) * 100
).toFixed(2)} %`,
`avg predictions: ${((avgPredictions / totalMatches || 0) * 100).toFixed(
2
)} %`,
`winrate predictions: ${((winratePrediction / totalMatches || 0) * 100).toFixed(2)} %`,
`avg predictions: ${((avgPredictions / totalMatches || 0) * 100).toFixed(2)} %`,
'',
`Total matches: ${totalMatches}`,
`Pending matches: ${totalTempPredictions}`,
Expand All @@ -26,6 +32,9 @@ export async function getAnalytics() {
(totalActiveTeams / totalTeams || 0) * 100
).toFixed(2)} %)`,
`Total players: ${totalPlayers}`,
`Active players (>0 actions): ${totalActivePlayers}`,
`Active players (>10 actions): ${totalActivePlayersGt10}`,
`Active players (>100 actions): ${totalActivePlayersGt100}`,
`Webhook static list length: ${webhookListLength}`,
].join('\n');

Expand Down

0 comments on commit 45ade07

Please sign in to comment.