-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add report generation for users grouped by completed actions (#…
…1825) * feat: add report generation for users grouped by completed actions * refactor: optimize users grouped by completed actions report generation
- Loading branch information
1 parent
3c1a11f
commit 8a12c1d
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |