Skip to content

fix query issue for dashboar widget#92

Merged
theihasan merged 2 commits intomainfrom
fix-dashboard-query
Dec 17, 2025
Merged

fix query issue for dashboar widget#92
theihasan merged 2 commits intomainfrom
fix-dashboard-query

Conversation

@theihasan
Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @theihasan, 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!

This pull request focuses on enhancing the efficiency of the JobsByCategoryChart dashboard widget. It addresses a critical performance issue by overhauling the data retrieval mechanism within the widget's data generation methods. The changes consolidate multiple individual database calls into fewer, more efficient queries, which will result in a more responsive and performant user experience when viewing the dashboard.

Highlights

  • Performance Optimization: The primary goal of this pull request is to significantly improve the performance of the dashboard widget by optimizing its database queries.
  • N+1 Query Resolution: The previous approach, which involved executing a separate database query for each job category to retrieve its data, has been replaced with a single, highly optimized query that fetches all necessary data in one go, effectively resolving the N+1 query problem.
  • Refactored Data Retrieval Logic: Both the getData() and getFallbackData() methods have been refactored to pre-fetch aggregated job data efficiently and then process this data in memory to construct the chart datasets, leading to reduced database load and faster rendering.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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

  1. 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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a 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 refactors queries in the JobsByCategoryChart widget to improve performance by addressing N+1 issues. The strategy of consolidating database calls into a single query is a significant improvement. My review includes two main points: first, a fix for a new N+1 query that was inadvertently introduced in getData(), and second, a correction for a potential bug in getFallbackData() related to the use of a database-specific date function that could lead to incorrect data. Applying these suggestions will help ensure the optimizations are both effective and reliable.

Comment on lines +69 to +97
foreach ($topCategories as $index => $category) {
$categoryData = $jobData->get($category->id, collect());

// Create a map of week -> count for this category
$weekCounts = $categoryData->pluck('count', 'date_week');

// Build data array matching the trend format
$trendData = Trend::query(JobListing::query())
->between(
start: $startDate,
end: $endDate,
)
->perWeek()
->count();

$data = $trendData->map(function (TrendValue $value) use ($weekCounts) {
$week = \Carbon\Carbon::parse($value->date)->format('Y-W');

return $weekCounts->get($week, 0);
})->toArray();

$datasets[] = [
'label' => $category->name,
'data' => $data,
'borderColor' => $colors[$index % count($colors)],
'backgroundColor' => $colors[$index % count($colors)].'20',
'tension' => 0.3,
];
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The Trend query inside the foreach loop causes an N+1 query problem, as it's executed for every category. This query should be moved outside the loop to be executed only once, improving performance significantly.

            // Build data array matching the trend format
            $trendData = Trend::query(JobListing::query())
                ->between(
                    start: $startDate,
                    end: $endDate,
                )
                ->perWeek()
                ->count();

            // Generate datasets for each category using the pre-fetched data
            foreach ($topCategories as $index => $category) {
                $categoryData = $jobData->get($category->id, collect());

                // Create a map of week -> count for this category
                $weekCounts = $categoryData->pluck('count', 'date_week');

                $data = $trendData->map(function (TrendValue $value) use ($weekCounts) {
                    $week = \Carbon\Carbon::parse($value->date)->format('Y-W');

                    return $weekCounts->get($week, 0);
                })->toArray();

                $datasets[] = [
                    'label' => $category->name,
                    'data' => $data,
                    'borderColor' => $colors[$index % count($colors)],
                    'backgroundColor' => $colors[$index % count($colors)].'20',
                    'tension' => 0.3,
                ];
            }


$jobData = JobListing::selectRaw('
job_category,
WEEK(created_at) as week_num,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The WEEK(created_at) function in MySQL has a default behavior that can be dependent on the default_week_format system variable. This can lead to inconsistencies between the week number calculated by the database and the one calculated by Carbon ($weekStart->week), which is ISO-8601 compliant. To ensure correctness and prevent potential bugs, it's best to explicitly specify the mode for the WEEK() function. Using mode 3 makes it compatible with the ISO 8601 standard, which is what Carbon's ->week property uses.

                WEEK(created_at, 3) as week_num,

Use WEEK(created_at, 3) to align SQL week numbers with Trend::perWeek output
@theihasan theihasan merged commit 91def41 into main Dec 17, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant