Skip to content

removed redundant api call#33911

Closed
saiprabhu-dandanayak wants to merge 4 commits intoappsmithorg:releasefrom
saiprabhu-dandanayak:bug/redundant-api-call
Closed

removed redundant api call#33911
saiprabhu-dandanayak wants to merge 4 commits intoappsmithorg:releasefrom
saiprabhu-dandanayak:bug/redundant-api-call

Conversation

@saiprabhu-dandanayak
Copy link
Contributor

@saiprabhu-dandanayak saiprabhu-dandanayak commented Jun 3, 2024

Description of this PR

I have raised this PR in order to remove redundant api call , and fetch the the templates from the redux store

Summary by CodeRabbit

  • New Features

    • Added configuration for video recording in Cypress tests.
    • Introduced test cases for forking a template using Redux store.
  • Improvements

    • Enhanced template API with a new response type and improved template retrieval logic.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 3, 2024

Walkthrough

The updates introduce new configurations for Cypress video recording, enhance the functionality of template handling in the TemplatesApi.ts file, and add comprehensive test cases for forking templates using the Redux store. These changes improve the testing setup, refine API responses, and bolster template management capabilities.

Changes

File Path Change Summary
app/client/cypress.config.ts Added configuration options for video recording, including setting the videos folder path and compression level.
app/client/cypress/e2e/...forkTemplateUsingReduxStore_spec.js Added test cases for forking a template using Redux store, including actions, error handling, and success validation.
app/client/src/api/TemplatesApi.ts Reordered imports, added TemplatesResponse type, createTemplateResponse function, and modified return type in getTemplateInformation method. Also, added imports for getTemplatesSelector and store.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant Cypress
    participant TemplatesApi
    participant ReduxStore

    User->>Cypress: Run tests
    Cypress->>TemplatesApi: Fetch template information
    TemplatesApi->>ReduxStore: Check for template in store
    alt Template in store
        ReduxStore-->>TemplatesApi: Return template data
    else Template not in store
        TemplatesApi-->>TemplatesApi: Fetch from API
    end
    TemplatesApi-->>Cypress: Return template data
    Cypress-->>User: Display test results
Loading

Poem

In the world of code, changes bloom,
Cypress records with room to zoom.
Templates fetch with ease and grace,
Redux helps to keep the pace.
Tests ensure our app's delight,
CodeRabbit hops with pure insight. 🐰💻


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@saiprabhu-dandanayak saiprabhu-dandanayak changed the title 🐛fix : removed redundant api call removed redundant api call Jun 3, 2024
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)
app/client/src/api/TemplatesApi.ts (1)

Line range hint 79-143: Consider refactoring the class to a namespace since it contains only static members.

Tools
Biome

[error] 96-96: Unexpected any. Specify a different type.


[error] 99-99: Template literals are preferred over string concatenation.

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 (2)
app/client/src/api/TemplatesApi.ts (2)

Line range hint 79-141: Class contains only static members.

Consider converting TemplatesAPI into a namespace or a module with exported functions, as using a class here does not leverage any OOP features like instances or state.

Tools
Biome

[error] 95-95: Unexpected any. Specify a different type.


[error] 97-97: Template literals are preferred over string concatenation.


Line range hint 86-86: Use template literals for string concatenation.

Using template literals can make the code cleaner and easier to read. Here's an example refactor for one of the methods:

- return Api.get(TemplatesAPI.baseUrl + `/app-templates/${templateId}`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates/${templateId}`);

Apply similar changes to other methods where string concatenation is used.

Also applies to: 97-97, 104-104, 112-113, 123-124, 131-131, 137-137

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e316c3f and b3140df.

Files selected for processing (1)
  • app/client/src/api/TemplatesApi.ts (5 hunks)
Additional context used
Biome
app/client/src/api/TemplatesApi.ts

[error] 79-141: Avoid classes that contain only static members.


[error] 86-86: Template literals are preferred over string concatenation.


[error] 86-86: Do not use template literals if interpolation and special-character handling are not needed.


[error] 95-95: Unexpected any. Specify a different type.


[error] 97-97: Template literals are preferred over string concatenation.


[error] 104-104: Template literals are preferred over string concatenation.


[error] 112-113: Template literals are preferred over string concatenation.


[error] 123-124: Template literals are preferred over string concatenation.


[error] 131-131: Template literals are preferred over string concatenation.


[error] 131-131: Do not use template literals if interpolation and special-character handling are not needed.


[error] 137-137: Template literals are preferred over string concatenation.


[error] 137-137: Do not use template literals if interpolation and special-character handling are not needed.

Additional comments not posted (3)
app/client/src/api/TemplatesApi.ts (3)

32-32: Change in type definition from FetchTemplatesResponse to TemplatesResponse.

Ensure that all usages of FetchTemplatesResponse are updated to TemplatesResponse where applicable.


69-78: Addition of createTemplateResponse function.

This function standardizes the response format for template data, which enhances maintainability and consistency across API responses.


90-96: Refactor to fetch template information from Redux store before making an API call.

This change optimizes performance by reducing unnecessary API calls if the data is already available in the store.

Verification successful

The search results indicate that template-related actions are being dispatched, and the TemplatesApi is being imported and used in various files. This suggests that the Redux store is being populated with template data before the function in TemplatesApi.ts is called.

  • app/client/src/pages/Templates/TemplateView.tsx: Dispatches actions like getTemplateInformation and getSimilarTemplatesInit.
  • app/client/src/pages/Templates/ForkTemplate.tsx: Dispatches importTemplateToWorkspace.
  • app/client/src/ce/pages/Applications/CreateNewAppFromTemplateModal/index.tsx: Dispatches setActiveLoadingTemplateId and importTemplateToWorkspace.

These actions ensure that the Redux store is populated with the necessary template data before the function in TemplatesApi.ts is called.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the Redux store is properly populated before this function is called.

# Test: Search for Redux store population logic. Expect: Proper handling and population logic before this function is called.
rg --type python $'store.dispatch'

Length of output: 64



Script:

#!/bin/bash
# Description: Verify that the Redux store is properly populated before this function is called.

# Test: Search for Redux store population logic. Expect: Proper handling and population logic before this function is called.
rg --type js 'store.dispatch'
rg --type ts 'store.dispatch'

Length of output: 2416



Script:

#!/bin/bash
# Description: Verify that template-related actions are being dispatched to populate the Redux store.

# Test: Search for template-related actions being dispatched. Expect: Actions related to templates being dispatched.
rg --type js 'dispatch.*template'
rg --type ts 'dispatch.*template'

Length of output: 679



Script:

#!/bin/bash
# Description: Verify that the function in TemplatesApi.ts is called after template-related actions are dispatched.

# Test: Search for the function call in TemplatesApi.ts. Expect: Function is called after template-related actions.
rg --type ts 'TemplatesApi' --context 10

Length of output: 30695

Tools
Biome

[error] 95-95: Unexpected any. Specify a different type.

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: 3

Outside diff range and nitpick comments (2)
app/client/src/api/TemplatesApi.ts (2)

Line range hint 86-86: Use template literals for string concatenation.

- return Api.get(TemplatesAPI.baseUrl + `/app-templates`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates`);

- return Api.get(TemplatesAPI.baseUrl + `/app-templates/${templateId}`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates/${templateId}`);

- return Api.get(TemplatesAPI.baseUrl + `/app-templates/${templateId}/similar`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates/${templateId}/similar`);

- return Api.post(TemplatesAPI.baseUrl + `/app-templates/${templateId}/import/${workspaceId}`);
+ return Api.post(`${TemplatesAPI.baseUrl}/app-templates/${templateId}/import/${workspaceId}`);

- return Api.post(TemplatesAPI.baseUrl + `/app-templates/${templateId}/merge/${applicationId}/${organizationId}`, body);
+ return Api.post(`${TemplatesAPI.baseUrl}/app-templates/${templateId}/merge/${applicationId}/${organizationId}`, body);

- return Api.get(TemplatesAPI.baseUrl + `/app-templates/filters`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates/filters`);

- return Api.post(TemplatesAPI.baseUrl + `/app-templates/publish/community-template`, body);
+ return Api.post(`${TemplatesAPI.baseUrl}/app-templates/publish/community-template`, body);

This change replaces string concatenation with template literals, which is a more modern and readable approach in JavaScript.

Also applies to: 97-97, 104-104, 112-113, 123-124, 131-131, 137-137


Line range hint 79-141: Consider refactoring the TemplatesAPI class.

The TemplatesAPI class contains only static methods and could be refactored into a simpler, functional module. This would reduce complexity and improve modularity.

Tools
Biome

[error] 97-97: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b3140df and 433541e.

Files selected for processing (1)
  • app/client/src/api/TemplatesApi.ts (5 hunks)
Additional context used
Biome
app/client/src/api/TemplatesApi.ts

[error] 79-141: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.


[error] 86-86: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 86-86: Do not use template literals if interpolation and special-character handling are not needed.

Unsafe fix: Replace with string literal


[error] 97-97: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 104-104: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 112-113: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 123-124: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 131-131: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 131-131: Do not use template literals if interpolation and special-character handling are not needed.

Unsafe fix: Replace with string literal


[error] 137-137: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 137-137: Do not use template literals if interpolation and special-character handling are not needed.

Unsafe fix: Replace with string literal

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: 3

Outside diff range and nitpick comments (2)
app/client/src/api/TemplatesApi.ts (2)

Line range hint 74-136: Consider refactoring the TemplatesAPI class.

The TemplatesAPI class contains only static methods. It might be more appropriate to use a simple object or separate functions instead of a class to hold these static methods. This can improve modularity and potentially reduce overhead.

Tools
Biome

[error] 92-92: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


Line range hint 81-81: Use template literals for string concatenation.

- return Api.get(TemplatesAPI.baseUrl + `/app-templates`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates`);

- return Api.get(TemplatesAPI.baseUrl + `/app-templates/${templateId}/similar`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates/${templateId}/similar`);

- return Api.post(TemplatesAPI.baseUrl + `/app-templates/${templateId}/import/${workspaceId}`);
+ return Api.post(`${TemplatesAPI.baseUrl}/app-templates/${templateId}/import/${workspaceId}`);

- return Api.post(TemplatesAPI.baseUrl + `/app-templates/${templateId}/merge/${applicationId}/${organizationId}`, body);
+ return Api.post(`${TemplatesAPI.baseUrl}/app-templates/${templateId}/merge/${applicationId}/${organizationId}`, body);

- return Api.get(TemplatesAPI.baseUrl + `/app-templates/filters`);
+ return Api.get(`${TemplatesAPI.baseUrl}/app-templates/filters`);

- return Api.post(TemplatesAPI.baseUrl + `/app-templates/publish/community-template`, body);
+ return Api.post(`${TemplatesAPI.baseUrl}/app-templates/publish/community-template`, body);

Using template literals simplifies the syntax and improves readability.

Also applies to: 92-92, 99-99, 107-108, 118-119, 126-126, 132-132

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 433541e and 0c153ea.

Files selected for processing (1)
  • app/client/src/api/TemplatesApi.ts (5 hunks)
Additional context used
Learnings (1)
app/client/src/api/TemplatesApi.ts (1)
User: saiprabhu-dandanayak
PR: appsmithorg/appsmith#33911
File: app/client/src/api/TemplatesApi.ts:0-0
Timestamp: 2024-06-04T03:58:07.902Z
Learning: User saiprabhu-dandanayak has confirmed the removal of the `any` type in the TypeScript code for better type safety in the `TemplatesApi.ts` file.
Biome
app/client/src/api/TemplatesApi.ts

[error] 74-136: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.


[error] 81-81: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 81-81: Do not use template literals if interpolation and special-character handling are not needed.

Unsafe fix: Replace with string literal


[error] 92-92: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 99-99: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 107-108: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 118-119: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 126-126: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 126-126: Do not use template literals if interpolation and special-character handling are not needed.

Unsafe fix: Replace with string literal


[error] 132-132: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.


[error] 132-132: Do not use template literals if interpolation and special-character handling are not needed.

Unsafe fix: Replace with string literal

Comment on lines +85 to +91
): Promise<AxiosPromise<TemplatesResponse>> {
const state = store.getState();
const templates = getTemplatesSelector(state);
const template = templates.find((template) => template.id === templateId);
if (template) {
return createTemplateResponse(template) as AxiosPromise<TemplatesResponse>;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Ensure proper error handling in getTemplateInformation.

The getTemplateInformation function currently lacks proper error handling mechanisms. Specifically, it does not handle the scenario where the template is not found in the Redux store or if the API call fails.

  • Add error handling for the case when the template is not found in the Redux store.
  • Add error handling for potential failures in the API call.
Analysis chain

Ensure proper error handling in getTemplateInformation.

While the function now fetches templates from the Redux store, it's crucial to ensure that error scenarios are handled appropriately, especially when the template is not found in the store.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify proper error handling when fetching templates.

# Test: Search for error handling patterns in the TemplatesApi.ts file. Expect: Proper error handling mechanisms.
rg --type ts 'catch|throw|error' app/client/src/api/TemplatesApi.ts

Length of output: 92



Script:

#!/bin/bash
# Description: Locate and inspect the getTemplateInformation function in TemplatesApi.ts for error handling.

# Locate the getTemplateInformation function
ast-grep --lang typescript --pattern 'function getTemplateInformation($_, $_) { $$$ }' app/client/src/api/TemplatesApi.ts

Length of output: 123



Script:

#!/bin/bash
# Description: Search for the getTemplateInformation function by name in TemplatesApi.ts.

# Search for the getTemplateInformation function
rg 'getTemplateInformation' app/client/src/api/TemplatesApi.ts

Length of output: 101



Script:

#!/bin/bash
# Description: Extract the full implementation of the getTemplateInformation function from TemplatesApi.ts.

# Extract the full implementation of the getTemplateInformation function
ast-grep --lang typescript --pattern 'static async getTemplateInformation($_, $_) { $$$ }' app/client/src/api/TemplatesApi.ts

Length of output: 601



Script:

#!/bin/bash
# Description: Extract the full implementation of the getTemplateInformation function from TemplatesApi.ts.

# Corrected pattern to extract the full implementation of the getTemplateInformation function
ast-grep --lang typescript --pattern 'class $_ { static async getTemplateInformation($_, $_) { $$$ } }' app/client/src/api/TemplatesApi.ts

Length of output: 140



Script:

#!/bin/bash
# Description: Search for the getTemplateInformation function definition and surrounding lines in TemplatesApi.ts.

# Search for the getTemplateInformation function definition and surrounding lines
rg -A 20 'static async getTemplateInformation' app/client/src/api/TemplatesApi.ts

Length of output: 815

Copy link
Contributor

@jacquesikot jacquesikot left a comment

Choose a reason for hiding this comment

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

Hi @saiprabhu-dandanayak,
Thank you for contributing to our project. However, I have added some comments on areas where I have concerns. Please go through them and let me know if you need any assistance.

responseTimeout: 60000,
pageLoadTimeout: 60000,
video: true,
videosFolder: "cypress/videos",
Copy link
Contributor

Choose a reason for hiding this comment

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

The video setting is already enabled, which means that Cypress will record videos of our tests. The videosFolder setting is also not necessary, as the default location for video recordings is cypress/videos.
Regarding the videoCompression setting, it's generally recommended to keep the default value of false unless you have specific requirements for video quality. Setting it to 32 can impact the performance of our tests, as the video compression process can slow down the test runtime.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would like to suggest a different approach to solving this problem.
Instead of directly accessing the API and checking the local state, I would recommend identifying the root cause of the redundant API call. This could involve tracing the code path that leads to the API call and understanding why it's being made in the first place and preventing it.
By addressing the root cause, you can ensure that the fix is more robust and less prone to unintended consequences. Additionally, this approach will help us better understand the code and make more informed decisions about how to improve it.
In this specific case, making changes to the TemplateApi could have unintended effects on other parts of the application. It's better to focus on the specific code path that's causing the redundant API call and fix it at that level.
If you're willing, I'd be happy to help you investigate the root cause of the API call and find a more targeted solution.
Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @jacquesikot , Thanks for your suugestion , i will try to find the root cause for this additional API call.

@github-actions
Copy link

This PR has not seen activitiy for a while. It will be closed in 7 days unless further activity is detected.

@github-actions github-actions bot added the Stale label Jun 20, 2024
@github-actions
Copy link

This PR has been closed because of inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants