-
Notifications
You must be signed in to change notification settings - Fork 2
Add credit transaction totals to daily stats #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
This repository is associated with gianpaj whose free trial has ended. Subscribe at jazzberry.ai. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @gianpaj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
I've updated the daily stats cron route to include the total monetary value of credit transactions. This enhancement fetches transaction descriptions, parses the dollar amounts from them, and then incorporates these daily and weekly totals into the Telegram message, providing a more comprehensive financial overview.
Highlights
- Feature Enhancement: I've added the capability to calculate and display the total dollar amounts from credit transactions for both the previous day and the last seven days within the daily stats report.
- Data Fetching: I've modified the Supabase queries in the daily stats API route to specifically retrieve the 'description' field from 'credit_transactions' for both daily and weekly periods.
- Data Processing: I've implemented a new utility function, 'extractDollarAmount', which uses regular expressions to accurately parse and extract monetary values from the transaction description strings.
- Reporting: I've updated the Telegram message format to include the newly calculated daily and weekly credit transaction dollar totals, alongside the existing transaction counts.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds logic to calculate and report the total dollar amount of credit transactions in the daily stats message. I've identified an opportunity to improve performance by consolidating database queries. I've also suggested improvements to the amount-parsing function for better robustness and readability, and added fallbacks to avoid NaN values.
app/api/daily-stats/route.ts
Outdated
const { data: creditsWeekData } = await supabase | ||
.from('credit_transactions') | ||
.select('description') | ||
.in('type', ['purchase', 'topup']) | ||
.gte('created_at', sevenDaysAgo.toISOString()) | ||
.lt('created_at', today.toISOString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two separate database queries to get the weekly credit transaction count (lines 149-153) and the descriptions (this block). These can be combined into a single, more efficient query. This would reduce the number of round-trips to the database.
You could replace lines 154-159 with a single query like this:
const { data: creditsWeekData } = await supabase
.from('credit_transactions')
.select('description')
.in('type', ['purchase', 'topup'])
.gte('created_at', sevenDaysAgo.toISOString())
.lt('created_at', today.toISOString());
A similar optimization can be applied to the daily credit queries on lines 118-123.
app/api/daily-stats/route.ts
Outdated
function extractDollarAmount(description: string): number { | ||
const dollarMatch = description.match(/\$([\d.]+)/); | ||
if (dollarMatch) return Number.parseFloat(dollarMatch[1]); | ||
const usdMatch = description.match(/^(\d+(?:\.\d+)?)\s*USD/); | ||
return usdMatch ? Number.parseFloat(usdMatch[1]) : 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex /\$([\d.]+)/
is a bit too permissive and could match an invalid number like $1.2.3
. Using /\$(\d+(?:\.\d+)?)/
would be more precise. Adding some more structure and optional chaining can make the function's logic clearer and safer.
function extractDollarAmount(description: string): number {
// Matches formats like "$12.34"
const dollarMatch = description.match(/\$(\d+(?:\.\d+)?)/);
if (dollarMatch?.[1]) {
return Number.parseFloat(dollarMatch[1]);
}
// Matches formats like "12.34 USD"
const usdMatch = description.match(/^(\d+(?:\.\d+)?)\s*USD/);
if (usdMatch?.[1]) {
return Number.parseFloat(usdMatch[1]);
}
return 0;
}
app/api/daily-stats/route.ts
Outdated
@@ -177,8 +201,8 @@ export async function GET(request: NextRequest) { | |||
// ` - Top voices: ${topVoiceList}`, | |||
`Profiles: ${profilesTodayCount} (${formatChange(profilesTodayCount, profilesPrevCount)})`, | |||
` - 7d total ${profilesWeekCount}, avg ${(profilesWeekCount / 7).toFixed(1)}`, | |||
`Credit Transactions: ${creditsTodayCount} (${formatChange(creditsTodayCount, creditsPrevCount)}) ${creditsTodayCount > 0 ? '🤑' : '😿'}`, | |||
` - 7d total ${creditsWeekCount}, avg ${(creditsWeekCount / 7).toFixed(1)}`, | |||
`Credit Transactions: ${creditsTodayCount} (${formatChange(creditsTodayCount, creditsPrevCount)}) - $${creditsTodayAmount.toFixed(2)} ${creditsTodayCount > 0 ? '🤑' : '😿'}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a fallback to 0 if creditsTodayAmount
is null or undefined to avoid potential NaN
values in the Telegram message.
`Credit Transactions: ${creditsTodayCount} (${formatChange(creditsTodayCount, creditsPrevCount)}) - $${(creditsTodayAmount || 0).toFixed(2)} ${creditsTodayCount > 0 ? '🤑' : '😿'}`
app/api/daily-stats/route.ts
Outdated
`Credit Transactions: ${creditsTodayCount} (${formatChange(creditsTodayCount, creditsPrevCount)}) ${creditsTodayCount > 0 ? '🤑' : '😿'}`, | ||
` - 7d total ${creditsWeekCount}, avg ${(creditsWeekCount / 7).toFixed(1)}`, | ||
`Credit Transactions: ${creditsTodayCount} (${formatChange(creditsTodayCount, creditsPrevCount)}) - $${creditsTodayAmount.toFixed(2)} ${creditsTodayCount > 0 ? '🤑' : '😿'}`, | ||
` - 7d total ${creditsWeekCount}, avg ${(creditsWeekCount / 7).toFixed(1)} ($${creditsWeekAmount.toFixed(2)})`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
Testing
pnpm run lint:write
pnpm run format
pnpm run type-check
(fails: Cannot find module 'contentlayer/generated')pnpm run check-translations
https://chatgpt.com/codex/tasks/task_e_688346ee1f3c832c9eadfd1143967e02