Skip to content
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

fix(ZMS-3399): refactoring #732

Merged
merged 1 commit into from
Dec 18, 2024
Merged

fix(ZMS-3399): refactoring #732

merged 1 commit into from
Dec 18, 2024

Conversation

manjencic
Copy link
Contributor

@manjencic manjencic commented Dec 13, 2024

Description

Short description or comments

Reference

Issues #XXX

Summary by CodeRabbit

  • New Features
    • Introduced a new method for updating availability based on providers.
  • Improvements
    • Enhanced logging capabilities for the import process.
    • Streamlined the handling of provider lists during imports.
  • Refactor
    • Updated method signatures for better clarity and consistency.

Copy link
Contributor

coderabbitai bot commented Dec 13, 2024

Walkthrough

The changes in this pull request focus on the Dldb class within the zmsdb/src/Zmsdb/Source/Dldb.php file. Key modifications include reformatting string concatenation for consistency, adjusting method braces for style, and altering the handling of the $updateAvailability parameter in the startImport method. A new updateAvailability method has been introduced to encapsulate the logic for updating availability based on provider data, while the writeProviderList method has been simplified to enhance logging and return an array of providers without the $updateAvailability parameter.

Changes

File Path Change Summary
zmsdb/src/Zmsdb/Source/Dldb.php - Reformatted string concatenation in getFixturesImportPath.
- Moved opening brace of setImportPath to a new line.
- Modified startImport to capture $providers from writeProviderList() and pass it to updateAvailability if needed.
- Updated writeProviderList to remove $updateAvailability parameter and return $providers array.
- Introduced updateAvailability method to handle availability updates.

Possibly related PRs

Suggested reviewers

  • ThomasAFink

Poem

🐇 In the code where rabbits play,
Changes hop in a bright array.
Methods refined, logging's a cheer,
Availability's clear, no need to fear.
With each little tweak, we leap with glee,
A smoother path for all to see! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
zmsdb/src/Zmsdb/Source/Dldb.php (1)

33-33: Consider adding error handling for updateAvailability

The refactoring improves separation of concerns by isolating the availability update logic. However, consider adding try-catch block around the updateAvailability call to handle potential errors gracefully.

 if ($updateAvailability) {
+    try {
         $this->updateAvailability($providers);
+    } catch (\Exception $e) {
+        if (self::$verbose) {
+            print("Error updating availability: " . $e->getMessage() . "\n");
+        }
+        throw $e;
+    }
 }

Also applies to: 40-40, 44-47

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 37b3bfd and 0641ac3.

📒 Files selected for processing (1)
  • zmsdb/src/Zmsdb/Source/Dldb.php (3 hunks)
🔇 Additional comments (3)
zmsdb/src/Zmsdb/Source/Dldb.php (3)

17-17: LGTM: Path construction is safe and consistent

The string concatenation change maintains functionality while improving readability.


21-22: LGTM: Consistent brace style

The brace placement change aligns with coding style guidelines.


68-80: LGTM: Improved method responsibility and logging

The method now has a clearer single responsibility and better logging. Let's verify the removal of $updateAvailability parameter doesn't break existing calls.

✅ Verification successful

The previous search might have been too specific. Let's broaden it to find any usage of the method and understand the class hierarchy to verify if there are any inherited calls that might be affected.


Based on the search results, I can now generate the final response:

Verified: Safe to remove $updateAvailability parameter

The method writeProviderList() is only called internally within the Dldb class itself (in the same file) without any parameters. The class extends \BO\Zmsdb\Base and there are no other classes inheriting from it that might be affected by this change. The parameter removal is safe.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any direct calls to writeProviderList with parameters
rg -l "writeProviderList\([^)]+\)" --type php

Length of output: 47


Script:

#!/bin/bash
# Let's try a broader search for writeProviderList usage
rg "writeProviderList" --type php -A 2

# Also search for class usage to understand inheritance
rg "class.*Dldb" --type php

Length of output: 6123

Comment on lines +82 to 101
protected function updateAvailability($providers)
{
foreach ($providers as $provider) {
$providerData = $provider->data;

if (!$updateAvailability) {
continue;
}

$scopes = (new \BO\Zmsdb\Scope())->readByProviderId($provider->getId());
foreach ($scopes as $scope) {
$availabilities = (new \BO\Zmsdb\Availability())->readList($scope->getId());

foreach ($availabilities as $availability) {
if ($availability->slotTimeInMinutes === $providerData['slotTimeInMinutes']) {
if ((int) $availability->slotTimeInMinutes === (int) $providerData['slotTimeInMinutes']) {
continue;
}

$availability->slotTimeInMinutes = $providerData['slotTimeInMinutes'];
$updatedEntity = (new \BO\Zmsdb\Availability())
(new \BO\Zmsdb\Availability())
->updateEntity($availability->getId(), $availability, 2);

//if (isset($providerData['forceSlotTimeUpdate']) && $providerData['forceSlotTimeUpdate']) {
// (new \BO\Zmsdb\Slot)->writeByAvailability($updatedEntity, \App::$now);
// (new \BO\Zmsdb\Helper\CalculateSlots(\App::DEBUG))
// ->writePostProcessingByScope($updatedEntity->scope, \App::$now);
//}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider performance and monitoring improvements

The new method handles availability updates but has some areas for improvement:

  1. Multiple database calls in nested loops could impact performance
  2. No transaction handling for atomic updates
  3. Missing logging for tracking changes

Consider these improvements:

 protected function updateAvailability($providers)
 {
+    $startTime = microtime(true);
+    $updatedCount = 0;
+    \BO\Zmsdb\Connection\Select::setTransaction();
+
     foreach ($providers as $provider) {
         $providerData = $provider->data;
         $scopes = (new \BO\Zmsdb\Scope())->readByProviderId($provider->getId());
+        $availabilityQuery = new \BO\Zmsdb\Availability();
+
         foreach ($scopes as $scope) {
             $availabilities = (new \BO\Zmsdb\Availability())->readList($scope->getId());
 
             foreach ($availabilities as $availability) {
                 if ((int) $availability->slotTimeInMinutes === (int) $providerData['slotTimeInMinutes']) {
                     continue;
                 }
 
                 $availability->slotTimeInMinutes = $providerData['slotTimeInMinutes'];
-                (new \BO\Zmsdb\Availability())
-                    ->updateEntity($availability->getId(), $availability, 2);
+                $availabilityQuery->updateEntity($availability->getId(), $availability, 2);
+                $updatedCount++;
             }
         }
     }
+
+    \BO\Zmsdb\Connection\Select::writeCommit();
+    
+    if (self::$verbose) {
+        $time = round(microtime(true) - $startTime, 3);
+        print("Updated $updatedCount availabilities in $time seconds\n\n");
+    }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
protected function updateAvailability($providers)
{
foreach ($providers as $provider) {
$providerData = $provider->data;
if (!$updateAvailability) {
continue;
}
$scopes = (new \BO\Zmsdb\Scope())->readByProviderId($provider->getId());
foreach ($scopes as $scope) {
$availabilities = (new \BO\Zmsdb\Availability())->readList($scope->getId());
foreach ($availabilities as $availability) {
if ($availability->slotTimeInMinutes === $providerData['slotTimeInMinutes']) {
if ((int) $availability->slotTimeInMinutes === (int) $providerData['slotTimeInMinutes']) {
continue;
}
$availability->slotTimeInMinutes = $providerData['slotTimeInMinutes'];
$updatedEntity = (new \BO\Zmsdb\Availability())
(new \BO\Zmsdb\Availability())
->updateEntity($availability->getId(), $availability, 2);
//if (isset($providerData['forceSlotTimeUpdate']) && $providerData['forceSlotTimeUpdate']) {
// (new \BO\Zmsdb\Slot)->writeByAvailability($updatedEntity, \App::$now);
// (new \BO\Zmsdb\Helper\CalculateSlots(\App::DEBUG))
// ->writePostProcessingByScope($updatedEntity->scope, \App::$now);
//}
}
}
}
protected function updateAvailability($providers)
{
$startTime = microtime(true);
$updatedCount = 0;
\BO\Zmsdb\Connection\Select::setTransaction();
foreach ($providers as $provider) {
$providerData = $provider->data;
$scopes = (new \BO\Zmsdb\Scope())->readByProviderId($provider->getId());
$availabilityQuery = new \BO\Zmsdb\Availability();
foreach ($scopes as $scope) {
$availabilities = (new \BO\Zmsdb\Availability())->readList($scope->getId());
foreach ($availabilities as $availability) {
if ((int) $availability->slotTimeInMinutes === (int) $providerData['slotTimeInMinutes']) {
continue;
}
$availability->slotTimeInMinutes = $providerData['slotTimeInMinutes'];
$availabilityQuery->updateEntity($availability->getId(), $availability, 2);
$updatedCount++;
}
}
}
\BO\Zmsdb\Connection\Select::writeCommit();
if (self::$verbose) {
$time = round(microtime(true) - $startTime, 3);
print("Updated $updatedCount availabilities in $time seconds\n\n");
}
}

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.

2 participants