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

add spaces context to operators #4902

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

add spaces context to operators #4902

wants to merge 1 commit into from

Conversation

imanjra
Copy link
Contributor

@imanjra imanjra commented Oct 7, 2024

What changes are proposed in this pull request?

  • Add spaces and workspace to operator context
  • Add panel events for spaces and workspace change

How is this patch tested? If it is not, please explain why.

Using a test panel and operator

Release Notes

Is this a user-facing change that should be mentioned in the release notes?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release
    notes for FiftyOne users.

See above

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced CustomPanel to handle changes in spaces and workspace contexts.
    • Added new properties to the ExecutionContext for improved context management during operator execution.
    • Introduced optional parameters in the register_panel method to respond to changes in spaces and workspace.
  • Documentation

    • Updated documentation for developing plugins to include new methods for managing spaces and workspace state changes.

These updates improve user interaction and flexibility within the application.

Copy link
Contributor

coderabbitai bot commented Oct 7, 2024

Walkthrough

The changes in this pull request enhance the functionality of the CustomPanel component and related context management within the application. New parameters, on_change_spaces and on_change_workspace, are added to the defineCustomPanel function, allowing the CustomPanel to respond to space and workspace changes. Additionally, modifications are made across multiple files to integrate these parameters into context management, including updates to the ExecutionContext, operator execution functions, and documentation for plugin development.

Changes

File Path Change Summary
app/packages/operators/src/CustomPanel.tsx - Updated defineCustomPanel function to include on_change_spaces and on_change_workspace parameters.
app/packages/operators/src/hooks.ts - Added spaces and workspaceName variables to useOperatorThrottledContextSetter, updating the context object in setThrottledContext.
app/packages/operators/src/operators.ts - Updated RawContext type to include spaces and workspaceName.
- Added getter methods for spaces and workspaceName in ExecutionContext.
- Updated operator execution functions to include new properties.
app/packages/operators/src/state.ts - Enhanced globalContextSelector and useExecutionContext to include spaces and workspaceName.
app/packages/operators/src/useCustomPanelHooks.ts - Modified CustomPanelProps interface to add onChangeSpaces and onChangeWorkspace properties.
docs/source/plugins/developing_plugins.rst - Added on_change_spaces and on_change_workspace methods to ExamplePanel class, with expanded documentation for plugin development.
fiftyone/operators/executor.py - Introduced spaces and workspace properties in ExecutionContext class for improved context management.
fiftyone/operators/operations.py - Updated register_panel method in Operations class to include on_change_spaces and on_change_workspace parameters.
fiftyone/operators/panel.py - Updated on_startup method in Panel class to include on_change_spaces and on_change_workspace events in context change processing.

Possibly related PRs

Suggested reviewers

  • ritch
  • benjaminpkane

Poem

In the burrow where panels play,
New changes hop in bright array.
With spaces and workspaces to explore,
Our CustomPanel opens the door!
Hops of joy, let events flow,
In the world of FiftyOne, watch us grow! 🐇✨


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

@imanjra imanjra requested review from ritch and a team October 7, 2024 04:17
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: 2

🧹 Outside diff range and nitpick comments (8)
app/packages/operators/src/CustomPanel.tsx (2)

118-119: LGTM! Consider adding JSDoc comments for new parameters.

The addition of on_change_spaces and on_change_workspace parameters to the defineCustomPanel function and their subsequent passing to the CustomPanel component is well-implemented and aligns with the PR objectives. The changes follow the existing code structure and naming conventions.

To improve code documentation, consider adding JSDoc comments for these new parameters. For example:

/**
 * @param {Function} on_change_spaces - Callback function triggered when spaces change
 * @param {Function} on_change_workspace - Callback function triggered when workspace changes
 */

Also applies to: 137-138


Line range hint 1-143: Overall changes look good and align with PR objectives.

The modifications to defineCustomPanel function successfully extend its interface to include space and workspace change handlers. These changes are well-integrated and maintain consistency with the existing code structure. The core functionality of the CustomPanel component remains intact, which is a positive aspect of this implementation.

As the application evolves to include more context-related parameters, consider refactoring the parameter list into a configuration object in the future. This would make the function more scalable and easier to maintain. For example:

defineCustomPanel({
  handlers: {
    onLoad,
    onChange,
    // ... other handlers
  },
  panelConfig: {
    name: panel_name,
    label: panel_label,
  }
})

This structure would be more flexible for future additions and easier to read.

app/packages/operators/src/useCustomPanelHooks.ts (2)

29-30: LGTM with a minor suggestion for improvement

The addition of onChangeSpaces and onChangeWorkspace properties to the CustomPanelProps interface is consistent with the existing pattern and naming convention. This change enhances the flexibility of the custom panel by allowing it to respond to changes in spaces and workspace contexts.

For improved type safety, consider using a more specific type for these properties, such as a function type that matches the expected event handler signature. For example:

onChangeSpaces?: (spaces: SpacesType) => void;
onChangeWorkspace?: (workspace: string) => void;

This would provide better type checking and autocompletion for users of this interface.


141-147: LGTM with a suggestion for improved readability

The addition of useCtxChangePanelEvent calls for ctx.spaces and ctx.workspaceName is consistent with the existing pattern and correctly implements the new functionality for handling changes in spaces and workspace contexts.

To improve readability and reduce repetition, consider refactoring the useCtxChangePanelEvent calls into a more concise format. For example:

const contextChanges = [
  { value: ctx.spaces, handler: props.onChangeSpaces },
  { value: ctx.workspaceName, handler: props.onChangeWorkspace },
  // ... other existing context changes
];

contextChanges.forEach(({ value, handler }) => {
  useCtxChangePanelEvent(isLoaded, panelId, value, handler);
});

This refactoring would make it easier to add or modify context change handlers in the future while keeping the code DRY.

fiftyone/operators/panel.py (1)

129-130: LGTM! Consider grouping related events for better readability.

The addition of "on_change_spaces" and "on_change_workspace" events aligns well with the PR objectives. This change will enable panels to respond to changes in spaces and workspaces, enhancing the operator context as intended.

For improved readability, consider grouping related events together. You could move these new events next to other context-related events like "on_change_ctx". Here's a suggested reordering:

ctx_change_events = [
    "on_change_ctx",
    "on_change_spaces",
    "on_change_workspace",
    "on_change_view",
    "on_change_dataset",
    # ... (other events)
]

This grouping makes it easier to understand the different types of context changes at a glance.

fiftyone/operators/operations.py (1)

359-360: LGTM! Consider updating the params dictionary.

The addition of on_change_spaces and on_change_workspace parameters aligns well with the PR objectives and follows the existing naming conventions for event handlers. The docstring updates clearly describe the new parameters.

Consider updating the params dictionary in the method body to include these new parameters, ensuring they are properly passed along to the trigger function:

 params = {
     "panel_name": name,
     "panel_label": label,
     # ... other existing parameters ...
     "on_change_group_slice": on_change_group_slice,
+    "on_change_spaces": on_change_spaces,
+    "on_change_workspace": on_change_workspace,
     "allow_duplicates": allow_duplicates,
 }

This will ensure that the new parameters are properly utilized when the panel is registered.

app/packages/operators/src/state.ts (1)

193-194: LGTM: Dependency array updated correctly.

The addition of spaces and workspaceName to the dependency array of the useMemo hook ensures that the ExecutionContext is properly updated when these values change. This is crucial for maintaining consistency and is in line with React best practices.

Consider memoizing the spaces and workspaceName values if they are derived from complex computations to optimize performance. For example:

const memoizedSpaces = useMemo(() => spaces, [JSON.stringify(spaces)]);
const memoizedWorkspaceName = useMemo(() => workspaceName, [workspaceName]);

Then use these memoized values in the ExecutionContext constructor and the dependency array.

fiftyone/operators/executor.py (1)

729-735: LGTM: New 'workspace' property added to ExecutionContext

The new 'workspace' property is a valuable addition to the ExecutionContext class. It provides a convenient way to access the current workspace state in the app and correctly handles the case when workspace_name is not present in the request_params.

Consider adding a check for self.dataset before calling get_workspace_info to prevent potential AttributeError if the dataset is not available. Here's a suggested improvement:

 @property
 def workspace(self):
     """The current workspace state in the app."""
     workspace_name = self.request_params.get("workspace_name", None)
     if workspace_name is None:
         return None
-    return self.dataset.get_workspace_info(workspace_name)
+    return self.dataset.get_workspace_info(workspace_name) if self.dataset else None
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c8e6de2 and 2c1e4c5.

📒 Files selected for processing (9)
  • app/packages/operators/src/CustomPanel.tsx (2 hunks)
  • app/packages/operators/src/hooks.ts (3 hunks)
  • app/packages/operators/src/operators.ts (8 hunks)
  • app/packages/operators/src/state.ts (5 hunks)
  • app/packages/operators/src/useCustomPanelHooks.ts (2 hunks)
  • docs/source/plugins/developing_plugins.rst (1 hunks)
  • fiftyone/operators/executor.py (1 hunks)
  • fiftyone/operators/operations.py (1 hunks)
  • fiftyone/operators/panel.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
app/packages/operators/src/CustomPanel.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/operators/src/hooks.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/operators/src/operators.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/operators/src/state.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/operators/src/useCustomPanelHooks.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

🔇 Additional comments (9)
app/packages/operators/src/hooks.ts (1)

31-32: LGTM! Consider adding a comment for workspaceName.

The addition of spaces and workspaceName to the operator context aligns well with the PR objectives. The implementation is consistent with the existing code patterns.

Consider adding a brief comment explaining the derivation of workspaceName from spaces._name for better code clarity.

Verify the usage of new context properties.

Ensure that the new spaces and workspaceName properties added to the context are properly utilized in the components or functions that consume this context.

Run the following script to verify the usage:

Also applies to: 54-55, 68-69

app/packages/operators/src/useCustomPanelHooks.ts (1)

Line range hint 1-247: Summary: Effective implementation of spaces and workspace context support

The changes in this file successfully implement support for handling spaces and workspace context changes in the custom panel functionality. The modifications are well-integrated into the existing code structure and follow established patterns.

Key points:

  1. The CustomPanelProps interface has been extended with new properties for space and workspace change handlers.
  2. The useCustomPanelHooks function now includes logic to trigger events for space and workspace context changes.
  3. The implementation is consistent with existing code and maintains good TypeScript and React practices.

These changes enhance the flexibility and functionality of the custom panel system, allowing it to respond to a broader range of context changes within the application.

app/packages/operators/src/state.ts (2)

98-99: LGTM: New context properties added correctly.

The addition of spaces and workspaceName to the globalContextSelector enhances the context information available to operators. This change aligns well with the PR objectives and follows the existing code pattern.

Also applies to: 112-113


155-156: LGTM: Execution context updated with new properties.

The useExecutionContext function has been correctly updated to include the new spaces and workspaceName properties. This ensures that the execution context has access to the newly added session information, which is crucial for the enhanced operator context functionality.

Also applies to: 175-176

fiftyone/operators/executor.py (2)

721-727: LGTM: New 'spaces' property added to ExecutionContext

The new 'spaces' property is a well-implemented addition to the ExecutionContext class. It correctly handles the case when spaces are not present in the request_params and provides a convenient way to access the current state of spaces in the app. The conversion from dictionary to fo.Space object ensures type consistency.


721-735: Summary: Valuable additions to ExecutionContext for spaces and workspace support

The changes in this file enhance the ExecutionContext class by adding two new properties: 'spaces' and 'workspace'. These additions align well with the PR objectives of incorporating spaces and workspace functionalities into the operator context.

  1. The 'spaces' property provides access to the current state of spaces in the app, converting the data to a fo.Space object for consistency.
  2. The 'workspace' property allows access to the current workspace state, utilizing the dataset object to retrieve workspace information.

These changes will improve the contextual information available to operators, potentially enhancing their functionality and user experience within the FiftyOne application.

docs/source/plugins/developing_plugins.rst (1)

2063-2088: New methods added to handle space and workspace changes

Two new methods have been added to the ExamplePanel class to handle changes in spaces and workspaces:

  1. on_change_spaces(self, ctx): This method is called when the current state of spaces changes in the app.
  2. on_change_workspace(self, ctx): This method is called when the current workspace changes in the app.

Both methods follow the existing pattern of updating the panel's state and data with the new information. This addition enhances the panel's ability to respond to changes in the app's structure and organization.

The new methods are well-documented and consistent with the existing code style. They provide clear examples of how to handle space and workspace changes in a panel.

app/packages/operators/src/operators.ts (2)

2-2: Importing necessary modules from @fiftyone/spaces

The new import statement correctly includes SpaceNode, spaceNodeFromJSON, and SpaceNodeJSON from @fiftyone/spaces, which are utilized in the subsequent code changes.


96-97: Adding spaces and workspaceName to RawContext extends context appropriately

The addition of spaces: SpaceNodeJSON and workspaceName: string to the RawContext type effectively incorporates spaces and workspace information into the execution context.

Comment on lines +146 to +151
public get spaces(): SpaceNode {
return spaceNodeFromJSON(this._currentContext.spaces);
}
public get workspaceName(): string {
return this._currentContext.workspaceName;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle potential undefined spaces and workspaceName in getters

Consider ensuring that this._currentContext.spaces and this._currentContext.workspaceName are defined before usage to prevent possible runtime errors due to undefined values.

You might add default values or null checks as follows:

public get spaces(): SpaceNode {
  return this._currentContext.spaces
    ? spaceNodeFromJSON(this._currentContext.spaces)
    : new SpaceNode(); // Provide an appropriate default or handle the undefined case
}

public get workspaceName(): string {
  return this._currentContext.workspaceName || '';
}

Comment on lines +560 to +561
spaces: currentContext.spaces,
workspace_name: currentContext.workspaceName,
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

Refactor repeated request payload construction into a helper function

The construction of the request payload, including spaces and workspace_name, is duplicated across multiple functions. To enhance maintainability and reduce code duplication, consider abstracting the common parameters into a shared helper function.

For example, you could create a utility function:

function buildRequestPayload(
  context: RawContext,
  additionalParams: object = {}
): object {
  return {
    current_sample: context.currentSample,
    dataset_name: context.datasetName,
    delegation_target: context.delegationTarget,
    extended: context.extended,
    extended_selection: context.extendedSelection,
    filters: context.filters,
    group_slice: context.groupSlice,
    operator_uri: operatorURI,
    params: ctx.params,
    request_delegation: ctx.requestDelegation,
    selected: context.selectedSamples ? Array.from(context.selectedSamples) : [],
    selected_labels: formatSelectedLabels(context.selectedLabels),
    view: context.view,
    view_name: context.viewName,
    query_performance: context.queryPerformance,
    spaces: context.spaces,
    workspace_name: context.workspaceName,
    ...additionalParams,
  };
}

Then, use this helper in your functions:

const payload = buildRequestPayload(currentContext, {
  results: results ? results.result : null,
});

// Use payload in your fetch function

Also applies to: 726-727, 831-832, 907-908, 940-941

Copy link
Contributor

@brimoor brimoor left a comment

Choose a reason for hiding this comment

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

@imanjra thanks for adding this! 🙌

What do you think about deleting ctx.workspace from the Python interface in favor of just ensuring that, if the current spaces are a saved workspace, then ctx.spaces.name will be set to the current workspace name? This is how session.spaces.name works btw. It would also be consistent with how saved views work: we have ctx.view with ctx.view.name populated when the current view is saved.

Can you also add ctx.ops.set_spaces(spaces=spaces, name=name) to programmatically load a spaces configuration or a saved workspace by name? Again for consistency with saved views.

And one more thing: can you update the builtin operators to remove the @todos:

  • @voxel51/save_workspace: remove this and instead just use the current ctx.spaces in execute()
  • @voxel51/edit_workspace_info: use default=ctx.spaces.name for the name prop
  • @voxel51/delete_workspace: use default=ctx.spaces.name for the name prop

@@ -2060,6 +2060,32 @@ subsequent sections.
ctx.panel.set_state("event", "on_change_group_slice")
ctx.panel.set_data("event_data", event)

def on_change_spaces(self, ctx):
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for updating the docs! One more place to document ctx.spaces is the Execution context section 😄

@imanjra
Copy link
Contributor Author

imanjra commented Oct 7, 2024

@brimoor great suggestions! I'll make the changes to merge the two and update the docs. Thank you!

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