Skip to content

Commit

Permalink
feat: add report generation for users grouped by completed actions (#…
Browse files Browse the repository at this point in the history
…1825)

* feat: add report generation for users grouped by completed actions

* refactor: optimize users grouped by completed actions report generation
  • Loading branch information
twistershark authored Feb 7, 2025
1 parent 3c1a11f commit 8a12c1d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/bin/reports/usersGroupedByCompletedActionsNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import xlsx from 'xlsx'

import { runBin } from '@/bin/runBin'
import { prismaClient } from '@/utils/server/prismaClient'

async function usersGroupedByCompletedActionsNumber() {
const result: { actions_completed: number; users: number }[] = await prismaClient.$queryRaw`
SELECT
CAST(action_count AS SIGNED) as actions_completed, CAST(COUNT(user_id) AS SIGNED) as users
FROM (
SELECT user_id, COUNT(action_type) AS action_count
FROM user_action
WHERE action_type != 'OPT_IN'
GROUP BY user_id
)
AS user_actions
GROUP BY action_count
ORDER BY action_count asc;
`

const formattedResult = result.map((row: any) => ({
actions_completed: Number(row.actions_completed),
users: Number(row.users),
}))

const rows = formattedResult.map(row => ({
numberOfActions: row.actions_completed,
numberOfUsers: row.users,
}))

const workbook = xlsx.utils.book_new()
const worksheet = xlsx.utils.json_to_sheet(rows)
xlsx.utils.book_append_sheet(workbook, worksheet, 'Users By Action (No Opt-Ins)')

await xlsx.writeFile(workbook, './src/bin/reports/usersGroupedByCompletedActions.xlsx')

console.table(rows)
}

void runBin(usersGroupedByCompletedActionsNumber)

0 comments on commit 8a12c1d

Please sign in to comment.